Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
JavaSamples/lineSphereX.java at master · bacchu/JavaSamples · GitHub
Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Small and medium teams
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
bacchu
JavaSamples
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
JavaSamples
/
lineSphereX.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
183 lines (143 loc) · 5.38 KB
master
Breadcrumbs
JavaSamples
/
lineSphereX.java
Copy path
Top
File metadata and controls
Code
Blame
183 lines (143 loc) · 5.38 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/** Copyright - Trivikram Prasad
Date: May 19, 2018
All Rights Reserved
*/
// Program to calculate the points of intersection of a vector with a Sphere
import
java
.
util
.
Scanner
;
// DEFINE ALL CLASSES
// 3D Point class
class
Point3D
{
double
x
,
y
,
z
;
//Coordinates of a point
public
Point3D
(
double
x
,
double
y
,
double
z
)
{
this
.
x
=
x
;
this
.
y
=
y
;
this
.
z
=
z
;
}
}
// Sphere Class
class
Sphere
{
double
cX
,
cY
,
cZ
;
// Coordinates of the sphere center
double
cR
;
// Radius of sphere
public
Sphere
(
double
cX
,
double
cY
,
double
cZ
,
double
cR
)
{
this
.
cX
=
cX
;
this
.
cY
=
cY
;
this
.
cZ
=
cZ
;
this
.
cR
=
cR
;
}
}
// String class -- only for formatting output
class
StringPoint
{
String
pX
,
pY
,
pZ
;
// Converting coordinate values to string values for formatting
public
StringPoint
(
String
pX
,
String
pY
,
String
pZ
)
{
this
.
pX
=
pX
;
this
.
pY
=
pY
;
this
.
pZ
=
pZ
;
}
}
// The main class. This is where the real s**t happens
public
class
lineSphereX
{
public
static
void
main
(
String
[]
args
){
double
a
,
b
,
c
;
// Coefficients of the quadratic equation
double
discr
;
// Square of the discriminant of the quadratic
double
t1
=
0.0
;
// parameter 1
double
t2
=
0.0
;
// parameter 2
Scanner
in
=
new
Scanner
(
System
.
in
);
// Read coordinates of the first point on the line
System
.
out
.
print
(
"Enter x, y, z coordinates of the 1st point on the line: "
);
Point3D
point1
=
readLineInputCoordinates
(
in
);
// Read coordinates of the second point on the line
System
.
out
.
print
(
"Enter x, y, z coordinates of the 2nd point on the line: "
);
Point3D
point2
=
readLineInputCoordinates
(
in
);
// Read center coordinates and radius of sphere
System
.
out
.
print
(
"Enter x, y, z center coordinates and RADIUS 'r' of the sphere: "
);
Sphere
sphCenter
=
readSphereInputCoordinates
(
in
);
in
.
close
();
//Calculate coefficients: 'a', 'b', 'c' for the equation 'ax^2 + bx + c = 0
a
=
Math
.
pow
(
point2
.
x
-
point1
.
x
,
2
) +
Math
.
pow
(
point2
.
y
-
point1
.
y
,
2
) +
Math
.
pow
(
point2
.
z
-
point1
.
z
,
2
);
b
=
2
* ((
point2
.
x
-
point1
.
x
) * (
point1
.
x
-
sphCenter
.
cX
) + (
point2
.
y
-
point1
.
y
) * (
point1
.
y
-
sphCenter
.
cY
)+ (
point2
.
z
-
point1
.
z
) * (
point1
.
z
-
sphCenter
.
cZ
));
c
=
Math
.
pow
((
point1
.
x
-
sphCenter
.
cX
),
2
) +
Math
.
pow
((
point1
.
y
-
sphCenter
.
cY
),
2
) +
Math
.
pow
((
point1
.
z
-
sphCenter
.
cZ
),
2
) -
Math
.
pow
(
sphCenter
.
cR
,
2
);
discr
=
Math
.
pow
(
b
,
2
) - (
4
*
a
*
c
);
// No intersection points
if
(
discr
<
0
)
{
System
.
out
.
println
(
"
\n
Vector does not intersect the sphere"
);
return
;
}
else
if
(
discr
==
0
)
// Single intersection point
{
System
.
out
.
println
(
"
\n
Vector intersects the sphere at a single point:"
);
if
(
a
>
0
)
{
t1
= -
b
/(
2
*
a
);
StringPoint
XPoint
=
findXPoint
(
point1
,
point2
,
t1
);
System
.
out
.
println
(
"The tangent to sphere is at "
+
XPoint
.
pX
+
","
+
XPoint
.
pY
+
","
+
XPoint
.
pZ
+
"
\n
"
);
}
else
{
System
.
out
.
println
(
"Not a line
\n
"
);
}
}
else
// Two intersection points
{
System
.
out
.
println
(
"
\n
Vector intersects the sphere at two points"
);
t1
= (-
b
-
Math
.
sqrt
(
discr
))/(
2
*
a
);
// 1st Vector equation parameter
t2
= (-
b
+
Math
.
sqrt
(
discr
))/(
2
*
a
);
// 2nd Vector equation parameter
// Find the points of intersection
StringPoint
XPoint1
=
findXPoint
(
point1
,
point2
,
t1
);
StringPoint
XPoint2
=
findXPoint
(
point1
,
point2
,
t2
);
System
.
out
.
println
(
"1st point of vector intersection with sphere: "
+
XPoint1
.
pX
+
","
+
XPoint1
.
pY
+
","
+
XPoint1
.
pZ
);
System
.
out
.
println
(
"2nd point of vector intersection with sphere: "
+
XPoint2
.
pX
+
","
+
XPoint2
.
pY
+
","
+
XPoint2
.
pZ
+
"
\n
"
);
}
System
.
exit
(
0
);
}
// Function for user input of sphere center coordinates and radius
private
static
Sphere
readSphereInputCoordinates
(
Scanner
input
) {
double
sphere_ctr
[] =
new
double
[
4
];
Sphere
input_sphere
=
new
Sphere
(
0
,
0
,
0
,
0
);
for
(
int
i
=
0
;
i
<
4
;
i
++)
{
sphere_ctr
[
i
] =
input
.
nextDouble
();
}
input_sphere
.
cX
=
sphere_ctr
[
0
];
input_sphere
.
cY
=
sphere_ctr
[
1
];
input_sphere
.
cZ
=
sphere_ctr
[
2
];
input_sphere
.
cR
=
sphere_ctr
[
3
];
return
input_sphere
;
}
// Function for user inputs of point coordinates
private
static
Point3D
readLineInputCoordinates
(
Scanner
input
) {
double
line_pt
[] =
new
double
[
3
];
Point3D
input_coords
=
new
Point3D
(
0
,
0
,
0
);
for
(
int
i
=
0
;
i
<
3
;
i
++)
{
line_pt
[
i
] =
input
.
nextDouble
();
}
input_coords
.
x
=
line_pt
[
0
];
input_coords
.
y
=
line_pt
[
1
];
input_coords
.
z
=
line_pt
[
2
];
return
input_coords
;
}
// Function to calculate the point of intersection
private
static
StringPoint
findXPoint
(
Point3D
point1
,
Point3D
point2
,
double
t
) {
Point3D
intPoint
=
new
Point3D
(
0
,
0
,
0
);
StringPoint
XStrPoint
=
new
StringPoint
(
""
,
""
,
""
);
// Parametric equation of the form L = P + tU
// where 'L' is the intersection point, 'P' is the point on the line and
// U is the unit vector (Point2 - Point1)
intPoint
.
x
=
point1
.
x
+
t
* (
point2
.
x
-
point1
.
x
);
intPoint
.
y
=
point1
.
y
+
t
* (
point2
.
y
-
point1
.
y
);
intPoint
.
z
=
point1
.
z
+
t
* (
point2
.
z
-
point1
.
z
);
XStrPoint
=
formatString
(
intPoint
);
return
XStrPoint
;
}
// Function to format the coordinates to string for display/output to console
private
static
StringPoint
formatString
(
Point3D
intPoint
) {
StringPoint
strPoint
=
new
StringPoint
(
""
,
""
,
""
);
strPoint
.
pX
=
String
.
format
(
"%.2f"
,
intPoint
.
x
);
strPoint
.
pY
=
String
.
format
(
"%.2f"
,
intPoint
.
y
);
strPoint
.
pZ
=
String
.
format
(
"%.2f"
,
intPoint
.
z
);
return
strPoint
;
}
}
You can’t perform that action at this time.