-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path104intersection
198 lines (171 loc) · 6.24 KB
/
104intersection
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/usr/bin/env python3
##
## EPITECH PROJECT, 2019
## 104intersection
## File description:
## Intersection, project of maths.
##
##Including library.
import math
import sys
##Implementation of the help menu.
def usage():
print("USAGE")
print(" ./104intersection opt xp yp zp xv yv zv p\n")
print("DESCRIPTION")
print(" opt surface option: 1 for a sphere, 2 for a cylinder, 3 for a cone")
print(" (xp, yp, zp) coordinates of a point by which the light ray passes through")
print(" (xv, yv, zv) coordinates of a vector parallel to the light ray")
print(" p parameter: radius of the sphere, radius of the cylinder, or")
print(" angle formed by the cone and the Z-axis")
##Error condition.
len = len(sys.argv)
if (len == 1):
print("No arguments")
sys.exit(84)
if (len == 2):
if (sys.argv[1] == "-h"):
usage()
sys.exit(84)
if (len < 9):
print("not enough arguments")
sys.exit(84)
if (len > 9):
print("too many arguments")
sys.exit(84)
##Circle.
def circle_intersection():
xp = int(sys.argv[2])
yp = int(sys.argv[3])
zp = int(sys.argv[4])
xv = int(sys.argv[5])
yv = int(sys.argv[6])
zv = int(sys.argv[7])
p = int(sys.argv[8])
if (p <= 0 or p >= 360):
print("Radius can't be inferior to 0 or superior to 360°")
sys.exit(84)
print("Sphere of radius " + "%.0f" % p)
print("Line passing through the point (" + "%.0f" % xp + ", " + "%.0f" % yp + ", " + "%.0f" % zp + ") and parallel to the vector ("+ "%.0f" % xv + ", " + "%.0f" % yv + ", " + "%.0f" % zv + ")")
a = math.pow(xv,2) + math.pow(yv,2) + math.pow(zv,2)
b = 2 * xp * xv + 2 * yp * yv + 2 *zp * zv
c = math.pow(xp,2) + math.pow(yp,2) + math.pow(zp,2) - math.pow(p,2)
discri = math.pow(b,2) - 4 * a * c
if (discri == 0):
print("1 intersection point:")
t = (-float(b))/(2 * float(a))
x = xv * t + xp
y = yv * t + yp
z = zv * t + zp
print("(" + "%.3f" % x + ", " + "%.3f" % y + ", " + "%.3f" % z + ")")
if (discri > 0):
x1 = (float(-b) - float(math.sqrt(discri)))/(2 * float(a))
x2 = (float(-b) + float(math.sqrt(discri)))/(2 * float(a))
x = xv * x1 + xp
y = yv * x1 + yp
z = zv * x1 + zp
a = xv * x2 + xp
b = yv * x2 + yp
c = zv * x2 + zp
print("2 intersection points:")
print("(" + "%.3f" % a + ", " + "%.3f" % b + ", " + "%.3f" % c + ")")
print("(" + "%.3f" % x + ", " + "%.3f" % y + ", " + "%.3f" % z + ")")
if (discri < 0):
print("No intersection point.")
##Cylinder.
def cylinder_intersection():
xp = int(sys.argv[2])
yp = int(sys.argv[3])
zp = int(sys.argv[4])
xv = int(sys.argv[5])
yv = int(sys.argv[6])
zv = int(sys.argv[7])
p = int(sys.argv[8])
if (p <= 0 or p >= 360):
print("Radius can't be inferior to 0 or superior to 360°")
sys.exit(84)
print("Cylinder of radius " + "%.0f" % p)
print("Line passing through the point (" + "%.0f" % xp + ", " + "%.0f" % yp + ", " + "%.0f" % zp + ") and parallel to the vector ("+ "%.0f" % xv + ", " + "%.0f" % yv + ", " + "%.0f" % zv + ")")
a = math.pow(xv,2) + math.pow(yv,2)
b = 2 * xp * xv + 2 * yp * yv
c = math.pow(xp,2) + math.pow(yp,2) - math.pow(p,2)
discri = math.pow(b,2) - 4 * a * c
if (discri == 0):
if ((2 * float(a)) != 0):
t = (-float(b))/(2 * float(a))
x = xv * t + xp
y = yv * t + yp
z = zv * t + zp
print("1 intersection point:")
print("(" + "%.3f" % x + ", " + "%.3f" % y + ", " + "%.3f" % z + ")")
else:
print("There is an infinite number of intersection points.")
if (discri > 0):
x1 = (float(-b) - float(math.sqrt(discri)))/(2 * float(a))
x2 = (float(-b) + float(math.sqrt(discri)))/(2 * float(a))
x = xv * x1 + xp
y = yv * x1 + yp
z = zv * x1 + zp
a = xv * x2 + xp
b = yv * x2 + yp
c = zv * x2 + zp
print("2 intersection points:")
print("(" + "%.3f" % a + ", " + "%.3f" % b + ", " + "%.3f" % c + ")")
print("(" + "%.3f" % x + ", " + "%.3f" % y + ", " + "%.3f" % z + ")")
if (discri < 0):
print("No intersection point.")
##Cone.
def cone_intersection():
xp = int(sys.argv[2])
yp = int(sys.argv[3])
zp = int(sys.argv[4])
xv = int(sys.argv[5])
yv = int(sys.argv[6])
zv = int(sys.argv[7])
p = int(sys.argv[8])
if (p <= 0 or p >= 180):
print("Angle can't be inferior to 0 or superior to 180°")
sys.exit(84)
print("Cone with a", p, "degree angle")
print("Line passing through the point (" + "%.0f" % xp + ", " + "%.0f" % yp + ", " + "%.0f" % zp + ") and parallel to the vector ("+ "%.0f" % xv + ", " + "%.0f" % yv + ", " + "%.0f" % zv + ")")
p = math.radians(p)
a = math.pow(xv, 2) + math.pow(yv, 2) - math.pov(zv * math.tan(p), 2)
b = (2 * xp * xv) + (2 * yp * yv) - (2 * zp * zv) * math.pow(math.tan(p), 2)
c = (math.pow(xp, 2) + math.pow(yp, 2) - math.pow(zp * math.tan(p), 2))
discri = math.pow(b, 2) - (4 * a * c)
if (discri == 0):
x1 = -b / (2 * a)
x = xp + x1 * xv
y = yp + x1 * yv
z = zp + x1 * zv
print("1 intersection point:")
print("(" + "%.3f" % x + ", " + "%.3f" % y + ", " + "%.3f" % z + ")")
if (discri > 0):
x1 = (-b - math.sqrt(discri))/(2 * a)
x2 = (-b + math.sqrt(discri))/(2 * a)
x = xp * x1 + xv
y = yp * x1 + yv
z = zp * x1 + zv
a = xp * x2 + xv
b = yp * x2 + yv
c = zp * x2 + zv
print("2 intersection points:")
print("(" + "%.3f" % a + ", " + "%.3f" % b + ", " + "%.3f" % c + ")")
print("(" + "%.3f" % x + ", " + "%.3f" % y + ", " + "%.3f" % z + ")")
if (discri < 0):
print("No intersection point.")
##Main function.
def main():
if (sys.argv[1] == '1'):
circle_intersection()
elif (sys.argv[1] == '2') :
cylinder_intersection()
elif (sys.argv[1] == '3'):
cone_intersection()
else:
exit(84)
try :
if __name__ == "__main__":
main()
except :
exit (84)