geometry_3d 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b6be9663a382709ce88d58a88343d355c8c11889
4
- data.tar.gz: 434c0d1cad5c1474523ecdf2e3d025d06cf25a6a
3
+ metadata.gz: 209509ba9f34482785365903342f230d4bce3fdd
4
+ data.tar.gz: 93fe5d2aa4735fc0f8ad65263e6da246273981cf
5
5
  SHA512:
6
- metadata.gz: f45cfb2296046a337a1628970c8e7b371791b593d1399088f5a7897c7a9468914d61cce976b455fa74866702de54d45a56669e03dcc49dffab943879c7ec4bf4
7
- data.tar.gz: 728cd85b9e419cde91e666a1ba269ef355207eef0fe158a318629b9059f3facebf0917df4af8b52103415c563654421a0a9df6ecfac64eade17c560279051d10
6
+ metadata.gz: 8cd04971fd4daba8edc0c0b46882370afaf967d91c030963c562dab267e0a629a2191100c9bebfab2e54c4d139a343aeb2751516f0c7dd91e5fb95fffad25ccb
7
+ data.tar.gz: 3de679ef382e3f6a838b57858f62d632865c9be5b71723a8e34567330eb3da8db9d804a10e0cc9b649e882e3e29f84235ab9f315b5ba8ddf800af56e21c377fc
@@ -4,4 +4,5 @@ require_relative './geometry_3d/line'
4
4
  require_relative './geometry_3d/plane'
5
5
  require_relative './geometry_3d/triangle'
6
6
  require_relative './geometry_3d/circle'
7
+ require_relative './geometry_3d/sphere.rb'
7
8
  require_relative './geometry_3d/equation.rb'
@@ -3,7 +3,7 @@ class Circle
3
3
  attr_accessor :radius
4
4
  attr_accessor :vector
5
5
 
6
- def initialize(point, vector, radius)
6
+ def initialize(point = Point.new(0, 0, 0), vector = Vector.new(0, 0, 1), radius)
7
7
  @point = point
8
8
  @radius = radius
9
9
  @vector = vector
@@ -34,6 +34,5 @@ class Circle
34
34
  def perimeter
35
35
  2 * Math::PI * radius
36
36
  end
37
-
38
37
  end
39
38
 
@@ -22,19 +22,121 @@ class Line
22
22
  Line.new(first_point, v)
23
23
  end
24
24
 
25
- # Find intersecting point of two lines.
25
+ # Finds the intersecting point of two lines. If the two lines do not intersect returns nil.
26
26
  #
27
27
  # Example:
28
- # >> Line.new(Point.new(1,-2,3), Vector.new(5,2,-1)).find_intersecting_point(Line.new(Point.new(-4,-4,1), Vector.new(-1,3,-1)))
29
- # => Point.new(-4, -4, 4)
28
+ # >> Line.new(Point.new(4, 19, 12), Vector.new(1, 6, 5)).find_intersecting_point_with_line(Line.new(Point.new(-3, -15, -19), Vector.new(2, 8, 8)))
29
+ # => Point.new(1, 1, -3)
30
30
  #
31
31
  # Arguments:
32
32
  # line: (Line)
33
33
 
34
- def find_intersecting_point(line)
35
- eq1 = LinearEquation.new(vector.x, -line.vector.x, point.x - line.point.x)
36
- eq2 = LinearEquation.new(vector.y, -line.vector.y, point.y - line.point.y)
37
- find_point (eq1.solve_system(eq2)[0])
34
+ def find_intersecting_point_with_line(line)
35
+ if (intersect? line)
36
+ eq1 = LinearEquation.new(vector.x, -line.vector.x, point.x - line.point.x)
37
+ eq2 = LinearEquation.new(vector.y, -line.vector.y, point.y - line.point.y)
38
+ find_point (eq1.solve_system(eq2)[0])
39
+ end
40
+ end
41
+
42
+ # Find intersecting point of line and plane. If the line and the plane do not intersect nil is returned.
43
+ #
44
+ # Example:
45
+ # >> Line.new(Point.new(1, 0, 1), Vector.new(3, -2, 1)).find_intersecting_point_with_plane(Plane.new(1, 1, 1, -6))
46
+ # => Point.new(7, -4, 3)
47
+ #
48
+ # Arguments:
49
+ # plane: (Plane)
50
+
51
+ def find_intersecting_point_with_plane(plane)
52
+ if (intersect_plane? plane)
53
+ find_point(- plane.substitute(point) / (plane.substitute(vector) - plane.d).to_f)
54
+ end
55
+ end
56
+
57
+ # Find the angle(degrees) between line and plane. If the line and the plane do not intersect nil is returned.
58
+ #
59
+ # Example:
60
+ # >> Line.new(Point.new(1, 0, 1), Vector.new(2, 1, 2)).find_angle_with_plane(Plane.new(1, 1, 0, -6))
61
+ # => 45
62
+ #
63
+ # Arguments:
64
+ # plane: (Plane)
65
+
66
+ def find_angle_with_plane(plane)
67
+ if (intersect_plane? plane)
68
+ numer = (vector.scalar_product(plane.find_normal_vector)).abs
69
+ denom = (vector.length * plane.find_normal_vector.length)
70
+ Math.asin(numer / denom) * (180 / Math::PI)
71
+ end
72
+ end
73
+
74
+ # Check if two lines are parallel.
75
+ #
76
+ # Example:
77
+ # >> Line.new(Point.new(1, 0, 1), Vector.new(3, -2, 1)).parallel?(Line.new(Point.new(2, 0, 1), Vector.new(6, -4, 2)))
78
+ # => true
79
+ #
80
+ # Arguments:
81
+ # line: (Line)
82
+
83
+ def parallel? (line)
84
+ vector.x * line.vector.y == vector.y * line.vector.x and vector.y * line.vector.z == vector.z * line.vector.y
85
+ end
86
+
87
+ # Check if two lines intersect.
88
+ #
89
+ # Example:
90
+ # >> Line.new(Point.new(4, 19, 12), Vector.new(1, 6, 5)).intersect?(Line.new(Point.new(-3, -15, -19), Vector.new(2, 8, 8)))
91
+ # => true
92
+ #
93
+ # Arguments:
94
+ # line: (Line)
95
+
96
+ def intersect?(line)
97
+ eq1 = LinearEquation.new(vector.x, -line.vector.x, point.x - line.point.x)
98
+ eq2 = LinearEquation.new(vector.y, -line.vector.y, point.y - line.point.y)
99
+ eq3 = LinearEquation.new(vector.z, -line.vector.z, point.z - line.point.z)
100
+ (eq1.solve_system(eq2) == eq2.solve_system(eq3)) and (not parallel? line)
101
+ end
102
+
103
+ # Check if two lines are skew.
104
+ #
105
+ # Example:
106
+ # >> Line.new(Point.new(4, -5, 1), Vector.new(2, 4, 3)).skew?(Line.new(Point.new(2, -1, 0), Vector.new(1, 3, 2)))
107
+ # => true
108
+ #
109
+ # Arguments:
110
+ # line: (Line)
111
+
112
+ def skew?(line)
113
+ not (parallel? line) and not(intersect? line)
114
+ end
115
+
116
+ # Check if a line and a plane are parallel.
117
+ #
118
+ # Example:
119
+ # >> Line.new(Point.new(4, -5, 1), Vector.new(2, 4, 3)).parallel_to_plane?(Plane.new(1, 1, 1, 3))
120
+ # => true
121
+ #
122
+ # Arguments:
123
+ # plane: (Plane)
124
+
125
+ def parallel_to_plane?(plane)
126
+ plane.find_normal_vector.scalar_product(vector) == 0
127
+ end
128
+
129
+ # Check if a line and a plane intersect.
130
+ #
131
+ # Example:
132
+ # >> Line.new(Point.new(4, -5, 1), Vector.new(2, 4, 3)).intersect_plane?(Plane.new(1, 2, 1, 3))
133
+ # => true
134
+ #
135
+ # Arguments:
136
+ # plane: (Plane)
137
+
138
+ def intersect_plane?(plane)
139
+ not parallel_to_plane? plane
38
140
  end
39
141
 
40
142
  def ==(line)
@@ -28,8 +28,6 @@ class Plane
28
28
  construct_with_two_vectors_and_point(vector1, vector2, point1)
29
29
  end
30
30
 
31
-
32
-
33
31
  # Construct plane with normal vector and passing through a point.
34
32
  #
35
33
  # Example:
@@ -92,18 +90,61 @@ class Plane
92
90
  Vector.construct_with_plane(self)
93
91
  end
94
92
 
93
+ # Find intersecting line of two planes. If the two planes do not intersect, returns nil.
94
+ #
95
+ # Example:
96
+ # >> Plane.new(2, -5, 3, -12).find_intersecting_line_of_planes(Plane.new(3, 4, -3, -6))
97
+ # => Line.new(Point.new(0, -18, -26), Vector.new(3, 15, 23))
98
+ # Arguments:
99
+ # plane: (Plane)
100
+
101
+ def find_intersecting_line_of_planes(plane)
102
+ if (intersect? plane)
103
+ vector = find_normal_vector.cross_product(plane.find_normal_vector)
104
+ eq1 = LinearEquation.new(b, c, d)
105
+ eq2 = LinearEquation.new(plane.b, plane.c, plane.d)
106
+ array = eq1.solve_system(eq2)
107
+ point = Point.new(0, array[0], array[1])
108
+ Line.new(point, vector)
109
+ end
110
+ end
111
+
112
+ # Check if two planes are parallel.
113
+ #
114
+ # Example:
115
+ # >> Plane.new(1, 2, 3, 5).parallel?(Plane.new(2, 4, 6, -4))
116
+ # => true
117
+ #
118
+ # Arguments:
119
+ # plane: (Plane)
120
+
121
+ def parallel?(plane)
122
+ a * plane.b == b * plane.a and c * plane.b == b * plane.c
123
+ end
124
+
125
+ # Check if two planes intersect.
126
+ #
127
+ # Example:
128
+ # >> Plane.new(1, 2, 3, 5).parallel?(Plane.new(2, -4, 6, -4))
129
+ # => true
130
+ #
131
+ # Arguments:
132
+ # plane: (Plane)
133
+
134
+ def intersect?(plane)
135
+ not parallel? plane
136
+ end
95
137
 
138
+ def substitute(primitive)
139
+ to_a.take(3).zip(primitive.to_a).map { |a, b| a * b}.reduce(:+) + d
140
+ end
96
141
 
97
142
  def ==(plane)
98
- #a == plane.a and b == plane.b and c == plane.c and d == plane.d
99
143
  to_a.zip(plane.to_a).map { |a, b| a == b}.reduce(:&)
100
144
  end
101
145
 
102
-
103
146
  private
104
147
  def self.find_d(array, point)
105
148
  - array.zip(point.to_a).map { |a, b| a * b }.reduce(:+)
106
149
  end
107
-
108
-
109
150
  end
@@ -32,7 +32,7 @@ class Point
32
32
  # plane: (Plane)
33
33
 
34
34
  def is_on_plane?(plane)
35
- (plane.a * x + plane.b * y + plane.c * z + plane.d) == 0
35
+ plane.substitute(self) == 0
36
36
  end
37
37
 
38
38
  # Check if a point is on a line
@@ -48,20 +48,45 @@ class Point
48
48
  to_a.zip(line.point.to_a, line.vector.to_a).map { |a, b, c| (a - b) / c }.uniq.length == 1
49
49
  end
50
50
 
51
- # Find the distance between two points
51
+ # Finds the distance between two points.
52
52
  #
53
53
  # Example:
54
- # >> Point.new(-7, -4, 3).distance_to(Point.new(17, 6, 2.5))
54
+ # >> Point.new(-7, -4, 3).distance_to_point(Point.new(17, 6, 2.5))
55
55
  # => 26.004807247891687
56
56
  #
57
57
  # Arguments:
58
58
  # point: (Point)
59
59
 
60
- def distance_to(point)
60
+ def distance_to_point(point)
61
61
  to_a.zip(point.to_a).map{|a, b| (b - a) ** 2}.reduce(:+) ** 0.5
62
62
  end
63
63
 
64
- # Find the midpoint between two points
64
+ # Finds the distance between point and line.
65
+ #
66
+ # Example:
67
+ # >> Point.new(0, 2, 3).distance_to_line(Line.new(Point.new(3, 1, -1), Vector.new(2, 1, 2)))
68
+ # => 5
69
+ # Arguments:
70
+ # line: (Line)
71
+
72
+ def distance_to_line(line)
73
+ numer = Vector.construct_with_two_points(self, line.point).cross_product(line.vector)
74
+ numer.length / line.vector.length
75
+ end
76
+
77
+ # Finds the distance between point and plane.
78
+ #
79
+ # Example:
80
+ # >> Point.new(4, -4, 3).distance_to_plane(Plane.new(2, -2, 5, 8))
81
+ # => 6.789028582272215
82
+ # Arguments:
83
+ # plane: (Plane)
84
+
85
+ def distance_to_plane(plane)
86
+ plane.substitute(self).abs / (plane.find_normal_vector.length)
87
+ end
88
+
89
+ # Finds the midpoint between two points.
65
90
  #
66
91
  # Example:
67
92
  # >> Point.new(-7, -4, 3).get_midpoint(Point.new(17, 6, 2.5))
@@ -78,15 +103,12 @@ class Point
78
103
  [x, y, z]
79
104
  end
80
105
 
81
-
82
106
  def ==(point)
83
107
  @x == point.x and @y == point.y and @z == point.z
84
108
  end
85
109
 
86
- def round
87
- Point.new(*to_a.map{|x| x.round(4)})
110
+ def round(number)
111
+ Point.new(*to_a.map{|x| x.round(number)})
88
112
  end
89
-
90
-
91
113
  end
92
114
 
@@ -0,0 +1,62 @@
1
+ class Sphere
2
+ attr_accessor :point
3
+ attr_accessor :radius
4
+
5
+ def initialize(point, radius)
6
+ @point = point
7
+ @radius = radius
8
+ end
9
+
10
+ # Find the surface area of a sphere.
11
+ #
12
+ # Example:
13
+ # >> Sphere.new(Point.new(1, 2, 3), 3).surface_area
14
+ # => 113.09733552923254
15
+ #
16
+ # Arguments:
17
+ # no
18
+
19
+ def surface_area
20
+ 4 * Math::PI * radius ** 2
21
+ end
22
+
23
+ # Find the volume of a sphere.
24
+ #
25
+ # Example:
26
+ # >> Sphere.new(Point.new(1, 2, 3), 3).volume
27
+ # => 113.09733552923254
28
+ #
29
+ # Arguments:
30
+ # no
31
+
32
+ def volume
33
+ (4.0 / 3) * Math::PI * radius ** 3
34
+ end
35
+
36
+ # Find the circumference of a sphere.
37
+ #
38
+ # Example:
39
+ # >> Sphere.new(Point.new(1, 2, 3), 3).circumference
40
+ # => 18.84955592153876
41
+ #
42
+ # Arguments:
43
+ # no
44
+
45
+ def circumference
46
+ Circle.new(radius).perimeter
47
+ end
48
+
49
+ # Checks if two spheres intersect/
50
+ #
51
+ # Example:
52
+ # >> Sphere.new(Point.new(-7, -4, 3), 20).intersect?(Sphere.new(Point.new(17, 6, 2.5), 7))
53
+ # => true
54
+ #
55
+ # Arguments:
56
+ # no
57
+
58
+ def intersect?(sphere)
59
+ point.distance_to_point(sphere.point) <= (radius + sphere.radius)
60
+ end
61
+
62
+ end
@@ -9,6 +9,175 @@ class Triangle
9
9
  @point_c = point_c
10
10
  end
11
11
 
12
+ # Finds the length of the AB side of the triangle.
13
+ #
14
+ # Example:
15
+ # >> Triangle.new(Point.new(1, 1, 1), Point.new(2, 2, 7), Point.new(1, 3, 1)).length_ab
16
+ # => 6.164414002968976
17
+ #
18
+ # Arguments:
19
+ # no
20
+
21
+ def length_ab
22
+ point_a.distance_to_point(point_b)
23
+ end
24
+
25
+ # Finds the length of the BC side of the triangle.
26
+ #
27
+ # Example:
28
+ # >> Triangle.new(Point.new(1, 1, 1), Point.new(2, 2, 7), Point.new(1, 3, 1)).length_bc
29
+ # => 6.164414002968976
30
+ #
31
+ # Arguments:
32
+ # no
33
+
34
+ def length_bc
35
+ point_b.distance_to_point(point_c)
36
+ end
37
+
38
+ # Finds the length of the AC side of the triangle.
39
+ #
40
+ # Example:
41
+ # >> Triangle.new(Point.new(1, 1, 1), Point.new(2, 2, 7), Point.new(1, 3, 1)).length_ac
42
+ # => 2
43
+ #
44
+ # Arguments:
45
+ # no
46
+
47
+ def length_ac
48
+ point_a.distance_to_point(point_c)
49
+ end
50
+
51
+ # Finds the length of the height form point A of the triangle.
52
+ #
53
+ # Example:
54
+ # >> Triangle.new(Point.new(1, 1, 1), Point.new(2, 2, 7), Point.new(1, 3, 1)).length_height_a
55
+ # => 6.082762530298219
56
+ #
57
+ # Arguments:
58
+ # no
59
+
60
+ def length_height_a
61
+ point_a.distance_to_line(get_line_bc)
62
+ end
63
+
64
+ # Finds the length of the height form point B of the triangle.
65
+ #
66
+ # Example:
67
+ # >> Triangle.new(Point.new(1, 1, 1), Point.new(2, 2, 7), Point.new(1, 3, 1)).length_height_b
68
+ # => 1.9735087641318605
69
+ #
70
+ # Arguments:
71
+ # no
72
+
73
+ def length_height_b
74
+ point_b.distance_to_line(get_line_ac)
75
+ end
76
+
77
+ # Finds the length of the height form point C of the triangle.
78
+ #
79
+ # Example:
80
+ # >> Triangle.new(Point.new(1, 1, 1), Point.+new(2, 2, 7), Point.new(1, 3, 1)).length_height_c
81
+ # => 6.082762530298219
82
+ #
83
+ # Arguments:
84
+ # no
85
+
86
+ def length_height_c
87
+ point_c.distance_to_line(get_line_ab)
88
+ end
89
+
90
+ # Finds the length of the median form point A of the triangle.
91
+ #
92
+ # Example:
93
+ # >> Triangle.new(Point.new(1, 1, 1), Point.+new(2, 2, 7), Point.new(1, 3, 1)).length_median_a
94
+ # => 3.391164991562634
95
+ #
96
+ # Arguments:
97
+ # no
98
+
99
+ def length_median_a
100
+ point_a.distance_to_point(point_b.get_midpoint point_c)
101
+ end
102
+
103
+ # Finds the length of the median form point B of the triangle.
104
+ #
105
+ # Example:
106
+ # >> Triangle.new(Point.new(1, 1, 1), Point.+new(2, 2, 7), Point.new(1, 3, 1)).length_median_b
107
+ # => 6.08276253029818
108
+ #
109
+ # Arguments:
110
+ # no
111
+
112
+ def length_median_b
113
+ point_b.distance_to_point(point_a.get_midpoint point_c)
114
+ end
115
+
116
+ # Finds the length of the median form point C of the triangle.
117
+ #
118
+ # Example:
119
+ # >> Triangle.new(Point.new(1, 1, 1), Point.+new(2, 2, 7), Point.new(1, 3, 1)).length_median_c
120
+ # => 3.391164991562634
121
+ #
122
+ # Arguments:
123
+ # no
124
+
125
+ def length_median_c
126
+ point_c.distance_to_point(point_a.get_midpoint point_b)
127
+ end
128
+
129
+ # Finds the area of the triangle.
130
+ #
131
+ # Example:
132
+ # >> Triangle.new(Point.new(1, 1, 1), Point.new(2, 2, 7), Point.new(1, 3, 1)).area
133
+ # => 1.9735087641318605
134
+ #
135
+ # Arguments:
136
+ # no
137
+
138
+ def area
139
+ (length_bc * length_height_a) / 2.0
140
+ end
141
+
142
+ # Finds the perimeter of the triangle.
143
+ #
144
+ # Example:
145
+ # >> Triangle.new(Point.new(1, 1, 1), Point.new(2, 2, 7), Point.new(1, 3, 1)).perimeter
146
+ # => 14.32882800593795
147
+ #
148
+ # Arguments:
149
+ # no
150
+
151
+ def perimeter
152
+ length_ab + length_ac + length_bc
153
+ end
154
+
155
+ # Finds the radius of the inscribed circle in triangle.
156
+ #
157
+ # Example:
158
+ # >> Triangle.new(Point.new(1, 1, 1), Point.new(2, 2, 7), Point.new(1, 3, 1)).radius_of_inscribed_circle
159
+ # => 0.8490244321137063
160
+ #
161
+ # Arguments:
162
+ # no
163
+
164
+ def radius_of_inscribed_circle
165
+ area / (perimeter / 2)
166
+ end
167
+
168
+ # Finds the radius of the circumscribed circle in triangle.
169
+ #
170
+ # Example:
171
+ # >> Triangle.new(Point.new(1, 1, 1), Point.new(2, 2, 7), Point.new(1, 3, 1)).radius_of_circumscribed_circle
172
+ # => 3.123580758801788
173
+ #
174
+ # Arguments:
175
+ # no
176
+
177
+ def radius_of_circumscribed_circle
178
+ (length_ab * length_bc * length_ac) / (4 * area)
179
+ end
180
+
12
181
  # Find the plane in which the triangle lies.
13
182
  #
14
183
  # Example:
@@ -22,6 +191,15 @@ class Triangle
22
191
  Plane.construct_with_three_points(point_a, point_b, point_c)
23
192
  end
24
193
 
194
+ # Find the line in which the height from the point A lies.
195
+ #
196
+ # Example:
197
+ # >> Triangle.new(Point.new(1, 2, 3), Point.new(4, 6, 9), Point.new(12, 11, 9)).get_line_height_a
198
+ # => Line.new(Point.new(1, 2, 3), Vector.new(85, -136, -534))
199
+ #
200
+ # Arguments:
201
+ # no
202
+
25
203
  # Find the line in which the AB side of the triangle lies.
26
204
  #
27
205
  # Example:
@@ -61,42 +239,84 @@ class Triangle
61
239
  Line.construct_with_two_points(point_a, point_c)
62
240
  end
63
241
 
242
+ # Find the line in which the height from the point A lies.
243
+ #
244
+ # Example:
245
+ # >> Triangle.new(Point.new(1, 2, 3), Point.new(4, 6, 9), Point.new(12, 11, 9)).get_line_height_a
246
+ # => Line.new(Point.new(1, 2, 3), Vector.new(85, -136, -534))
247
+ #
248
+ # Arguments:
249
+ # no
250
+
251
+ def get_line_height_a
252
+ l = get_line_bc
253
+ Line.new(point_a, l.vector.cross_product(get_plane.find_normal_vector))
254
+ end
255
+
256
+ # Find the line in which the height from the point B lies.
257
+ #
258
+ # Example:
259
+ # >> Triangle.new(Point.new(1, 2, 3), Point.new(4, 6, 9), Point.new(12, 11, 9)).get_line_height_b
260
+ # => Line.new(Point.new(4, 6, 9), Vector.new(441, -7, -798))
261
+ #
262
+ # Arguments:
263
+ # no
264
+
265
+ def get_line_height_b
266
+ l = get_line_ac
267
+ Line.new(point_b, l.vector.cross_product(get_plane.find_normal_vector))
268
+ end
269
+
270
+ # Find the line in which the height from the point C lies.
271
+ #
272
+ # Example:
273
+ # >> Triangle.new(Point.new(1, 2, 3), Point.new(4, 6, 9), Point.new(12, 11, 9)).get_line_height_c
274
+ # => ine.new(Point.new(12, 11, 9), Vector.new(356, 129, -264))
275
+ #
276
+ # Arguments:
277
+ # no
278
+
279
+ def get_line_height_c
280
+ l = get_line_ab
281
+ Line.new(point_c, l.vector.cross_product(get_plane.find_normal_vector))
282
+ end
283
+
64
284
  # Find the median of the triangle from the point A
65
285
  #
66
286
  # Example:
67
- # >> Triangle.new(Point.new(1, 2, 3), Point.new(4, 6, 9), Point.new(12, 11, 9)).get_median_a
287
+ # >> Triangle.new(Point.new(1, 2, 3), Point.new(4, 6, 9), Point.new(12, 11, 9)).get_line_median_a
68
288
  # => Line.new(Point.new(1, 2, 3), Vector.new(7, 6.5, 6))
69
289
  #
70
290
  # Arguments:
71
291
  # no
72
292
 
73
- def get_median_a
293
+ def get_line_median_a
74
294
  Line.construct_with_two_points(point_a, point_b.get_midpoint(point_c))
75
295
  end
76
296
 
77
297
  # Find the median of the triangle from the point B.
78
298
  #
79
299
  # Example:
80
- # >> Triangle.new(Point.new(1, 2, 3), Point.new(4, 6, 9), Point.new(12, 11, 9)).get_median_b
300
+ # >> Triangle.new(Point.new(1, 2, 3), Point.new(4, 6, 9), Point.new(12, 11, 9)).get_line_median_b
81
301
  # => Line.new(Point.new(4, 6, 9), Vector.new(2.5, 0.5, -3))
82
302
  #
83
303
  # Arguments:
84
304
  # no
85
- #Line.new(Point.new(4, 6, 9), Vector.new(2, 0, -3)).find_intersecting_point(Line.new(Point.new(12, 11, 9), Vector.new(-10, -7, -3)))
86
- def get_median_b
305
+
306
+ def get_line_median_b
87
307
  Line.construct_with_two_points(point_b, point_a.get_midpoint(point_c))
88
308
  end
89
309
 
90
310
  # Find the median of the triangle from the point C.
91
311
  #
92
312
  # Example:
93
- # >> Triangle.new(Point.new(1, 2, 3), Point.new(4, 6, 9), Point.new(12, 11, 9)).get_median_c
313
+ # >> Triangle.new(Point.new(1, 2, 3), Point.new(4, 6, 9), Point.new(12, 11, 9)).get_line_median_c
94
314
  # => Line.new(Point.new(12, 11, 9), Vector.new(-9.5, -7, -3))
95
315
  #
96
316
  # Arguments:
97
317
  # no
98
318
 
99
- def get_median_c
319
+ def get_line_median_c
100
320
  Line.construct_with_two_points(point_c, point_a.get_midpoint(point_b))
101
321
  end
102
322
 
@@ -110,6 +330,6 @@ class Triangle
110
330
  # no
111
331
 
112
332
  def get_centroid
113
- get_median_a.find_intersecting_point(get_median_b)
333
+ get_line_median_a.find_intersecting_point_with_line(get_line_median_b)
114
334
  end
115
335
  end
@@ -50,6 +50,19 @@ class Vector
50
50
  Vector.new(*to_a.zip(vector.to_a).map { |a, b| a + b } )
51
51
  end
52
52
 
53
+ # Finds the scalar product of two vector.
54
+ #
55
+ # Example:
56
+ # >> Vector.new(1, 2, 3).scalar_product(Vector.new(-2, 1, 2))
57
+ # => 6
58
+ #
59
+ # Arguments:
60
+ # vector: (Vector)
61
+
62
+ def scalar_product(vector)
63
+ to_a.zip(vector.to_a).map { |a, b| a * b }.reduce(:+)
64
+ end
65
+
53
66
  # Finds a vector perpendicular to both vectors.
54
67
  #
55
68
  # Example:
@@ -66,6 +79,19 @@ class Vector
66
79
  Vector.new(coordinate_x, coordinate_y, coordinate_z)
67
80
  end
68
81
 
82
+ # Finds the length of the vector.
83
+ #
84
+ # Example:
85
+ # >> Vector.new(4, 0, 3).length
86
+ # => 5
87
+ #
88
+ # Arguments:
89
+ # vector: (Vector)
90
+
91
+ def length
92
+ to_a.map{ |a| a * a}.reduce(:+) ** 0.5
93
+ end
94
+
69
95
  def to_a()
70
96
  [x, y, z]
71
97
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: geometry_3d
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Guldjan Kupen
@@ -24,6 +24,7 @@ files:
24
24
  - lib/geometry_3d/line.rb
25
25
  - lib/geometry_3d/plane.rb
26
26
  - lib/geometry_3d/point.rb
27
+ - lib/geometry_3d/sphere.rb
27
28
  - lib/geometry_3d/triangle.rb
28
29
  - lib/geometry_3d/vector.rb
29
30
  homepage: http://rubygems.org/gems/geometry_3d