easy_geometry 0.1.0 → 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 +4 -4
- data/LICENSE +7 -0
- data/README.md +52 -0
- data/easy_geometry.gemspec +1 -1
- data/lib/easy_geometry.rb +1 -1
- data/lib/easy_geometry/d2/line.rb +12 -6
- data/lib/easy_geometry/d2/linear_entity.rb +12 -6
- data/lib/easy_geometry/d2/point.rb +1 -2
- data/lib/easy_geometry/d2/polygon.rb +20 -6
- data/lib/easy_geometry/d2/ray.rb +13 -6
- data/lib/easy_geometry/d2/segment.rb +7 -6
- data/lib/easy_geometry/d2/triangle.rb +7 -4
- data/lib/easy_geometry/d2/vector.rb +3 -3
- data/spec/d2/line_spec.rb +8 -2
- data/spec/d2/polygon_spec.rb +1 -2
- data/spec/d2/ray_spec.rb +1 -1
- metadata +3 -2
- data/README +0 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: dcb12969d22494d6a789551ae657851411b331e0e91413378a6d9bc64806050a
|
|
4
|
+
data.tar.gz: 7dbcd5ce78baf99a3c8b67a2ea05ee5a3dd50560bf750b6aac4d5026472de2b5
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 660a28139e2b469f9877e8763e63a7f74315a25068a27c6e0bc2d5ed64c60fdbe0e6555fb8011a4ec467aff09a33d713366c682ee7fc426ecdfebf66cb107484
|
|
7
|
+
data.tar.gz: 45b64a55bd38d20985af2055c2ec65c40d6d91150ee4d3023c99629985d7d106925c447cb71115996d0d497e4888f949b5a8250523d6ce88d82d0449b7cc3df9
|
data/LICENSE
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
Copyright (c) 2019 Henry Metlov
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
Geometry for Ruby
|
|
2
|
+
=================
|
|
3
|
+
|
|
4
|
+
[](http://badge.fury.io/rb/easy_geometry)
|
|
5
|
+
|
|
6
|
+
EasyGeometry gem allows one to create two-dimensional geometrical entities, such as lines and polygons, and query for information about these entities. This could include asking the area of an polygon, checking for collinearity of a set of points, or finding the intersection between two lines.
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
Available geometry entities
|
|
10
|
+
----------
|
|
11
|
+
|
|
12
|
+
- Point, Vector
|
|
13
|
+
- Line, Segment, Ray
|
|
14
|
+
- Polygon, Triangle
|
|
15
|
+
|
|
16
|
+
Installation
|
|
17
|
+
----------
|
|
18
|
+
```ruby
|
|
19
|
+
gem install easy_geometry
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
Examples
|
|
23
|
+
--------
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
```ruby
|
|
27
|
+
x = EasyGeometry::D2::Point.new(0, 0)
|
|
28
|
+
y = EasyGeometry::D2::Point.new(1, 1)
|
|
29
|
+
z = EasyGeometry::D2::Point.new(2, 2)
|
|
30
|
+
w = EasyGeometry::D2::Point.new(1, 0)
|
|
31
|
+
|
|
32
|
+
EasyGeometry::D2::Point.is_collinear?(x, y, z) # true
|
|
33
|
+
EasyGeometry::D2::Point.is_collinear?(x, y, w) # false
|
|
34
|
+
|
|
35
|
+
t = EasyGeometry::D2::Triangle.new(x, y, w)
|
|
36
|
+
t.area # 0.5
|
|
37
|
+
t.perimeter # 3.414213562373095
|
|
38
|
+
|
|
39
|
+
s1 = EasyGeometry::D2::Segment.new([0, 0], [1, 1])
|
|
40
|
+
s2 = EasyGeometry::D2::Segment.new([0, 0], [-1, 1])
|
|
41
|
+
s3 = EasyGeometry::D2::Segment.new([0, 0], [1, 0])
|
|
42
|
+
s1.midpoint # Point(1/2, 1/2)
|
|
43
|
+
s3.length # 1
|
|
44
|
+
s1.intersection(s2) # [Point(0, 0)]
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
You can find more examples in [documentetion](https://genrih-metlov.gitbook.io/easy-geometry/) and in `specs` directory.
|
|
48
|
+
|
|
49
|
+
License
|
|
50
|
+
-------
|
|
51
|
+
|
|
52
|
+
2019 Henry Metlov, released under the MIT license
|
data/easy_geometry.gemspec
CHANGED
data/lib/easy_geometry.rb
CHANGED
|
@@ -3,15 +3,18 @@ module EasyGeometry
|
|
|
3
3
|
# An infinite line in 2-dimensional Euclidean space.
|
|
4
4
|
class Line < LinearEntity
|
|
5
5
|
|
|
6
|
+
# Is other GeometryEntity contained in this Line?
|
|
6
7
|
#
|
|
7
8
|
# Parameters:
|
|
8
|
-
# GeometryEntity
|
|
9
|
+
# GeometryEntity or Array of Numeric(coordinates)
|
|
9
10
|
#
|
|
10
11
|
# Returns:
|
|
11
12
|
# true if `other` is on this Line.
|
|
12
13
|
# false otherwise.
|
|
13
14
|
#
|
|
14
15
|
def contains?(other)
|
|
16
|
+
other = Point.new(other[0], other[1]) if other.is_a?(Array)
|
|
17
|
+
|
|
15
18
|
if other.is_a?(Point)
|
|
16
19
|
return Point.is_collinear?(other, self.p1, self.p2)
|
|
17
20
|
end
|
|
@@ -25,9 +28,9 @@ module EasyGeometry
|
|
|
25
28
|
|
|
26
29
|
# Finds the shortest distance between a line and a point.
|
|
27
30
|
#
|
|
28
|
-
#
|
|
29
|
-
#
|
|
30
|
-
#
|
|
31
|
+
# Parameters:
|
|
32
|
+
# Point or Array of Numeric(coordinates)
|
|
33
|
+
#
|
|
31
34
|
def distance(other)
|
|
32
35
|
other = Point.new(other[0], other[1]) if other.is_a?(Array)
|
|
33
36
|
raise TypeError, "Distance between Line and #{ other.class } is not defined" unless other.is_a?(Point)
|
|
@@ -36,10 +39,13 @@ module EasyGeometry
|
|
|
36
39
|
self.perpendicular_segment(other).length
|
|
37
40
|
end
|
|
38
41
|
|
|
39
|
-
# Returns True if self and other are the same mathematical entities
|
|
42
|
+
# Returns True if self and other are the same mathematical entities.
|
|
43
|
+
#
|
|
44
|
+
# Parameters:
|
|
45
|
+
# GeometryEntity
|
|
46
|
+
#
|
|
40
47
|
def ==(other)
|
|
41
48
|
return false unless other.is_a?(Line)
|
|
42
|
-
|
|
43
49
|
Point.is_collinear?(self.p1, other.p1, self.p2, other.p2)
|
|
44
50
|
end
|
|
45
51
|
|
|
@@ -17,7 +17,7 @@ module EasyGeometry
|
|
|
17
17
|
|
|
18
18
|
# The direction vector of the LinearEntity.
|
|
19
19
|
# Returns:
|
|
20
|
-
#
|
|
20
|
+
# Vector; the ray from the origin to this point is the
|
|
21
21
|
# direction of `self`.
|
|
22
22
|
#
|
|
23
23
|
def direction
|
|
@@ -180,7 +180,7 @@ module EasyGeometry
|
|
|
180
180
|
end
|
|
181
181
|
|
|
182
182
|
# Create a new Line parallel to this linear entity which passes
|
|
183
|
-
# through the point
|
|
183
|
+
# through the point p
|
|
184
184
|
#
|
|
185
185
|
# Parameters:
|
|
186
186
|
# Point
|
|
@@ -189,12 +189,14 @@ module EasyGeometry
|
|
|
189
189
|
# Line
|
|
190
190
|
#
|
|
191
191
|
def parallel_line(point)
|
|
192
|
+
point = Point.new(point[0], point[1]) if point.is_a?(Array)
|
|
192
193
|
raise TypeError, 'Must pass only Point.' unless point.is_a?(Point)
|
|
194
|
+
|
|
193
195
|
Line.new(point, point + self.direction.to_point)
|
|
194
196
|
end
|
|
195
197
|
|
|
196
198
|
# Create a new Line perpendicular to this linear entity which passes
|
|
197
|
-
# through the
|
|
199
|
+
# through the `point`.
|
|
198
200
|
#
|
|
199
201
|
# Parameters:
|
|
200
202
|
# Point
|
|
@@ -203,6 +205,7 @@ module EasyGeometry
|
|
|
203
205
|
# Line
|
|
204
206
|
#
|
|
205
207
|
def perpendicular_line(point)
|
|
208
|
+
point = Point.new(point[0], point[1]) if point.is_a?(Array)
|
|
206
209
|
raise TypeError, 'Must pass only Point.' unless point.is_a?(Point)
|
|
207
210
|
|
|
208
211
|
# any two lines in R^2 intersect, so blindly making
|
|
@@ -222,6 +225,7 @@ module EasyGeometry
|
|
|
222
225
|
# Segment or Point (if `point` is on this linear entity.)
|
|
223
226
|
#
|
|
224
227
|
def perpendicular_segment(point)
|
|
228
|
+
point = Point.new(point[0], point[1]) if point.is_a?(Array)
|
|
225
229
|
raise TypeError, 'Must pass only Point.' unless point.is_a?(Point)
|
|
226
230
|
|
|
227
231
|
return point if self.contains?(point)
|
|
@@ -258,12 +262,14 @@ module EasyGeometry
|
|
|
258
262
|
# A point x is 'in front' of a point y if x.dot(y) >= 0.
|
|
259
263
|
#
|
|
260
264
|
# Return
|
|
261
|
-
# -1 if
|
|
262
|
-
# 0 if
|
|
263
|
-
# 1 if
|
|
265
|
+
# -1 if other is behind self.p1
|
|
266
|
+
# 0 if other is self.p1
|
|
267
|
+
# 1 if other is in front of self.p1.
|
|
264
268
|
#
|
|
265
269
|
def span_test(other)
|
|
270
|
+
other = Point.new(other[0], other[1]) if other.is_a?(Array)
|
|
266
271
|
raise TypeError, 'Must pass only Point.' unless other.is_a?(Point)
|
|
272
|
+
|
|
267
273
|
return 0 if self.p1 == other
|
|
268
274
|
|
|
269
275
|
rel_pos = other - self.p1
|
|
@@ -11,7 +11,7 @@ module EasyGeometry
|
|
|
11
11
|
# and any points collinear and between two points will be removed
|
|
12
12
|
# unless they are needed to define an explicit intersection (see specs).
|
|
13
13
|
|
|
14
|
-
# Must be at least
|
|
14
|
+
# Must be at least 3 points
|
|
15
15
|
class Polygon
|
|
16
16
|
attr_reader :vertices
|
|
17
17
|
|
|
@@ -22,9 +22,18 @@ module EasyGeometry
|
|
|
22
22
|
end
|
|
23
23
|
|
|
24
24
|
# Return True/False for cw/ccw orientation.
|
|
25
|
+
#
|
|
26
|
+
# Parameters:
|
|
27
|
+
# a, b, c - EasyGeometry::D2::Point or Array of Numeric(coordinates)
|
|
28
|
+
#
|
|
25
29
|
def self.is_right?(a, b, c)
|
|
30
|
+
a = Point.new(a[0], a[1]) if a.is_a?(Array)
|
|
26
31
|
raise TypeError, 'Must pass only Point objects' unless a.is_a?(Point)
|
|
32
|
+
|
|
33
|
+
b = Point.new(b[0], b[1]) if b.is_a?(Array)
|
|
27
34
|
raise TypeError, 'Must pass only Point objects' unless b.is_a?(Point)
|
|
35
|
+
|
|
36
|
+
c = Point.new(c[0], c[1]) if c.is_a?(Array)
|
|
28
37
|
raise TypeError, 'Must pass only Point objects' unless c.is_a?(Point)
|
|
29
38
|
|
|
30
39
|
ba = b - a
|
|
@@ -114,7 +123,7 @@ module EasyGeometry
|
|
|
114
123
|
# rectangle for the geometric figure.
|
|
115
124
|
#
|
|
116
125
|
# Returns:
|
|
117
|
-
#
|
|
126
|
+
# Array of Numeric
|
|
118
127
|
#
|
|
119
128
|
def bounds
|
|
120
129
|
return @bounds if defined?(@bounds)
|
|
@@ -135,10 +144,14 @@ module EasyGeometry
|
|
|
135
144
|
# False otherwise.
|
|
136
145
|
#
|
|
137
146
|
def is_convex?
|
|
147
|
+
return @is_convex if defined?(@is_convex)
|
|
148
|
+
|
|
149
|
+
@is_convex = false
|
|
150
|
+
|
|
138
151
|
cw = Polygon.is_right?(vertices[-2], vertices[-1], vertices[0])
|
|
139
152
|
(1...vertices.length).each do |i|
|
|
140
153
|
if cw ^ Polygon.is_right?(vertices[i - 2], vertices[i - 1], vertices[i])
|
|
141
|
-
return
|
|
154
|
+
return @is_convex
|
|
142
155
|
end
|
|
143
156
|
end
|
|
144
157
|
|
|
@@ -153,19 +166,20 @@ module EasyGeometry
|
|
|
153
166
|
sj = sides[j]
|
|
154
167
|
if !points.include?(sj.p1) && !points.include?(sj.p2)
|
|
155
168
|
hit = si.intersection(sj)
|
|
156
|
-
|
|
169
|
+
return @is_convex if !hit.empty?
|
|
157
170
|
end
|
|
158
171
|
end
|
|
159
172
|
end
|
|
160
173
|
|
|
161
|
-
|
|
174
|
+
@is_convex = !@is_convex
|
|
175
|
+
@is_convex
|
|
162
176
|
end
|
|
163
177
|
|
|
164
178
|
# Return True if p is enclosed by (is inside of) self, False otherwise.
|
|
165
179
|
# Being on the border of self is considered False.
|
|
166
180
|
#
|
|
167
181
|
# Parameters:
|
|
168
|
-
# Point
|
|
182
|
+
# Point or Array of Numeric(coordinates)
|
|
169
183
|
#
|
|
170
184
|
# Returns:
|
|
171
185
|
# bool
|
data/lib/easy_geometry/d2/ray.rb
CHANGED
|
@@ -6,10 +6,10 @@ module EasyGeometry
|
|
|
6
6
|
# Is other GeometryEntity contained in this Ray?
|
|
7
7
|
#
|
|
8
8
|
# Parameters:
|
|
9
|
-
# GeometryEntity
|
|
9
|
+
# GeometryEntity or Array of Numeric(coordinates)
|
|
10
10
|
#
|
|
11
11
|
# Returns:
|
|
12
|
-
# true if `other` is on this
|
|
12
|
+
# true if `other` is on this Ray.
|
|
13
13
|
# false otherwise.
|
|
14
14
|
#
|
|
15
15
|
def contains?(other)
|
|
@@ -39,10 +39,11 @@ module EasyGeometry
|
|
|
39
39
|
|
|
40
40
|
# Finds the shortest distance between the ray and a point.
|
|
41
41
|
#
|
|
42
|
-
#
|
|
43
|
-
#
|
|
44
|
-
#
|
|
42
|
+
# Parameters:
|
|
43
|
+
# Point or Array of Numeric(coordinates)
|
|
44
|
+
#
|
|
45
45
|
def distance(other)
|
|
46
|
+
other = Point.new(other[0], other[1]) if other.is_a?(Array)
|
|
46
47
|
raise TypeError, "Distance between Ray and #{ other.class } is not defined" unless other.is_a?(Point)
|
|
47
48
|
|
|
48
49
|
return 0 if self.contains?(other)
|
|
@@ -56,6 +57,10 @@ module EasyGeometry
|
|
|
56
57
|
end
|
|
57
58
|
|
|
58
59
|
# Returns True if self and other are the same mathematical entities
|
|
60
|
+
#
|
|
61
|
+
# Parameters:
|
|
62
|
+
# GeometryEntity
|
|
63
|
+
#
|
|
59
64
|
def ==(other)
|
|
60
65
|
return false unless other.is_a?(Ray)
|
|
61
66
|
self.source == other.source && self.contains?(other.p2)
|
|
@@ -72,6 +77,7 @@ module EasyGeometry
|
|
|
72
77
|
# Positive infinity if the ray points in the positive x direction,
|
|
73
78
|
# negative infinity if the ray points in the negative x direction,
|
|
74
79
|
# or 0 if the ray is vertical.
|
|
80
|
+
#
|
|
75
81
|
def xdirection
|
|
76
82
|
return @xdirection if defined?(@xdirection)
|
|
77
83
|
|
|
@@ -91,7 +97,8 @@ module EasyGeometry
|
|
|
91
97
|
# Returns:
|
|
92
98
|
# Positive infinity if the ray points in the positive y direction,
|
|
93
99
|
# negative infinity if the ray points in the negative y direction,
|
|
94
|
-
# or 0 if the ray is
|
|
100
|
+
# or 0 if the ray is horizontal.
|
|
101
|
+
#
|
|
95
102
|
def ydirection
|
|
96
103
|
return @ydirection if defined?(@ydirection)
|
|
97
104
|
|
|
@@ -6,7 +6,7 @@ module EasyGeometry
|
|
|
6
6
|
# Is the other GeometryEntity contained within this Segment?
|
|
7
7
|
#
|
|
8
8
|
# Parameters:
|
|
9
|
-
# GeometryEntity
|
|
9
|
+
# GeometryEntity or Array of Numeric(coordinates)
|
|
10
10
|
#
|
|
11
11
|
# Returns:
|
|
12
12
|
# true if `other` is in this Segment.
|
|
@@ -36,7 +36,11 @@ module EasyGeometry
|
|
|
36
36
|
return false
|
|
37
37
|
end
|
|
38
38
|
|
|
39
|
-
# Returns True if self and other are the same mathematical entities
|
|
39
|
+
# Returns True if self and other are the same mathematical entities.
|
|
40
|
+
#
|
|
41
|
+
# Parameters:
|
|
42
|
+
# GeometryEntity
|
|
43
|
+
#
|
|
40
44
|
def ==(other)
|
|
41
45
|
return false unless other.is_a?(Segment)
|
|
42
46
|
[p1, p2].sort_by {|p| [p.x, p.y]} == [other.p1, other.p2].sort_by {|p| [p.x, p.y]}
|
|
@@ -50,14 +54,11 @@ module EasyGeometry
|
|
|
50
54
|
# Finds the shortest distance between a line segment and a point.
|
|
51
55
|
#
|
|
52
56
|
# Parameters:
|
|
53
|
-
# Point
|
|
57
|
+
# Point or Array of Numeric(coordinates)
|
|
54
58
|
#
|
|
55
59
|
# Returns:
|
|
56
60
|
# Number
|
|
57
61
|
#
|
|
58
|
-
# Raises
|
|
59
|
-
# ======
|
|
60
|
-
# TypeError is raised if `other` is not a Point
|
|
61
62
|
def distance(other)
|
|
62
63
|
other = Point.new(other[0], other[1]) if other.is_a?(Array)
|
|
63
64
|
raise TypeError, "Distance between Segment and #{ other.class } is not defined" unless other.is_a?(Point)
|
|
@@ -47,6 +47,7 @@ module EasyGeometry
|
|
|
47
47
|
end
|
|
48
48
|
|
|
49
49
|
# Are two or more of the sides the same length?
|
|
50
|
+
# Precision - 10e-13
|
|
50
51
|
#
|
|
51
52
|
# Returns:
|
|
52
53
|
# bool
|
|
@@ -56,6 +57,7 @@ module EasyGeometry
|
|
|
56
57
|
end
|
|
57
58
|
|
|
58
59
|
# Are all the sides of the triangle of different lengths?
|
|
60
|
+
# Precision - 10e-13
|
|
59
61
|
#
|
|
60
62
|
# Returns:
|
|
61
63
|
# bool
|
|
@@ -120,12 +122,12 @@ module EasyGeometry
|
|
|
120
122
|
@orthocenter
|
|
121
123
|
end
|
|
122
124
|
|
|
123
|
-
# The circumcenter of the triangle
|
|
125
|
+
# The circumcenter of the triangle.
|
|
124
126
|
#
|
|
125
127
|
# The circumcenter is the center of the circumcircle.
|
|
126
128
|
#
|
|
127
129
|
# Returns:
|
|
128
|
-
# Point
|
|
130
|
+
# Point
|
|
129
131
|
#
|
|
130
132
|
def circumcenter
|
|
131
133
|
return @circumcenter if defined?(@circumcenter)
|
|
@@ -139,7 +141,7 @@ module EasyGeometry
|
|
|
139
141
|
# The radius of the circumcircle of the triangle.
|
|
140
142
|
#
|
|
141
143
|
# Returns:
|
|
142
|
-
#
|
|
144
|
+
# Numeric
|
|
143
145
|
#
|
|
144
146
|
def circumradius
|
|
145
147
|
@circumradius ||= self.vertices[0].distance(self.circumcenter)
|
|
@@ -206,7 +208,7 @@ module EasyGeometry
|
|
|
206
208
|
# The radius of the incircle.
|
|
207
209
|
#
|
|
208
210
|
# Returns:
|
|
209
|
-
#
|
|
211
|
+
# Numeric
|
|
210
212
|
#
|
|
211
213
|
def inradius
|
|
212
214
|
@inradius ||= 2 * self.area / self.perimeter
|
|
@@ -235,6 +237,7 @@ module EasyGeometry
|
|
|
235
237
|
#
|
|
236
238
|
def exradii
|
|
237
239
|
return @exradii if defined?(@exradii)
|
|
240
|
+
|
|
238
241
|
a = self.sides[0].length
|
|
239
242
|
b = self.sides[1].length
|
|
240
243
|
c = self.sides[2].length
|
|
@@ -26,7 +26,7 @@ module EasyGeometry
|
|
|
26
26
|
end
|
|
27
27
|
|
|
28
28
|
# Returns a non-zero vector that is orthogonal to the
|
|
29
|
-
# line containing
|
|
29
|
+
# line containing self and the origin.
|
|
30
30
|
def orthogonal_direction
|
|
31
31
|
# if a coordinate is zero, we can put a 1 there and zeros elsewhere
|
|
32
32
|
return Vector.new(1, 0) if x.zero?
|
|
@@ -38,7 +38,7 @@ module EasyGeometry
|
|
|
38
38
|
end
|
|
39
39
|
|
|
40
40
|
# It is positive if other vector should be turned counter-clockwise in order to superpose them.
|
|
41
|
-
# It is
|
|
41
|
+
# It is negative if other vector should be turned clockwise in order to superpose them.
|
|
42
42
|
# It is zero when vectors are collinear.
|
|
43
43
|
def cross_product(other)
|
|
44
44
|
raise TypeError, "Cross product between Vector and #{ other.class } is not defined" unless other.is_a?(Vector)
|
|
@@ -51,7 +51,7 @@ module EasyGeometry
|
|
|
51
51
|
x * other.x + y * other.y
|
|
52
52
|
end
|
|
53
53
|
|
|
54
|
-
# Converts the vector to a point
|
|
54
|
+
# Converts the vector to a point.
|
|
55
55
|
def to_point
|
|
56
56
|
Point.new(x, y)
|
|
57
57
|
end
|
data/spec/d2/line_spec.rb
CHANGED
|
@@ -215,6 +215,7 @@ RSpec.describe EasyGeometry::D2::Line do
|
|
|
215
215
|
|
|
216
216
|
it 'should return a parallel line' do
|
|
217
217
|
expect(l3.parallel_line(p2).parallel_to?(l3)).to be true
|
|
218
|
+
expect(l3.parallel_line([1, 1]).parallel_to?(l3)).to be true
|
|
218
219
|
end
|
|
219
220
|
end
|
|
220
221
|
|
|
@@ -227,6 +228,7 @@ RSpec.describe EasyGeometry::D2::Line do
|
|
|
227
228
|
|
|
228
229
|
it 'should return a perpendicular line' do
|
|
229
230
|
expect(l3.perpendicular_line(p2).perpendicular_to?(l3)).to be true
|
|
231
|
+
expect(l3.perpendicular_line([1, 1]).perpendicular_to?(l3)).to be true
|
|
230
232
|
end
|
|
231
233
|
end
|
|
232
234
|
|
|
@@ -238,7 +240,8 @@ RSpec.describe EasyGeometry::D2::Line do
|
|
|
238
240
|
end
|
|
239
241
|
|
|
240
242
|
it 'should return the point if line contains this point' do
|
|
241
|
-
expect(l1.perpendicular_segment(p1)).to eq(p1)
|
|
243
|
+
expect(l1.perpendicular_segment(p1)).to eq(p1)
|
|
244
|
+
expect(l1.perpendicular_segment([0, 0])).to eq(p1)
|
|
242
245
|
expect(l2.perpendicular_segment(p1)).to eq(p1)
|
|
243
246
|
expect(l3.perpendicular_segment(p1)).to eq(p1)
|
|
244
247
|
end
|
|
@@ -265,6 +268,7 @@ RSpec.describe EasyGeometry::D2::Line do
|
|
|
265
268
|
|
|
266
269
|
it 'should return correct number' do
|
|
267
270
|
expect(l1.span_test(p1)).to eq(0)
|
|
271
|
+
expect(l1.span_test([0, 0])).to eq(0)
|
|
268
272
|
expect(l1.span_test(p2)).to eq(1)
|
|
269
273
|
expect(l1.span_test(EasyGeometry::D2::Point.new(-1, -1))).to eq(-1)
|
|
270
274
|
end
|
|
@@ -273,6 +277,7 @@ RSpec.describe EasyGeometry::D2::Line do
|
|
|
273
277
|
describe '#contains?' do
|
|
274
278
|
it 'should return true' do
|
|
275
279
|
expect(l1.contains?(p1)).to be true
|
|
280
|
+
expect(l1.contains?([0, 0])).to be true
|
|
276
281
|
expect(l1.contains?(p2)).to be true
|
|
277
282
|
expect(l1.contains?(EasyGeometry::D2::Point.new(100, 100))).to be true
|
|
278
283
|
expect(l1.contains?(EasyGeometry::D2::Segment.new(
|
|
@@ -305,7 +310,8 @@ RSpec.describe EasyGeometry::D2::Line do
|
|
|
305
310
|
end
|
|
306
311
|
|
|
307
312
|
it 'should return zero if line contains point' do
|
|
308
|
-
expect(l1.distance(p1)).to eq(0)
|
|
313
|
+
expect(l1.distance(p1)).to eq(0)
|
|
314
|
+
expect(l1.distance([0, 0])).to eq(0)
|
|
309
315
|
expect(l1.distance(p2)).to eq(0)
|
|
310
316
|
expect(l2.distance(p1)).to eq(0)
|
|
311
317
|
expect(l3.distance(p1)).to eq(0)
|
data/spec/d2/polygon_spec.rb
CHANGED
|
@@ -51,6 +51,7 @@ RSpec.describe EasyGeometry::D2::Polygon do
|
|
|
51
51
|
|
|
52
52
|
it 'should return true' do
|
|
53
53
|
expect(described_class.is_right?(p1, p2, p3)).to be true
|
|
54
|
+
expect(described_class.is_right?([0, 0], [1, 1], [1, 0])).to be true
|
|
54
55
|
end
|
|
55
56
|
|
|
56
57
|
it 'should return false' do
|
|
@@ -265,8 +266,6 @@ RSpec.describe EasyGeometry::D2::Polygon do
|
|
|
265
266
|
let(:polygon4) { described_class.new([1, 1], [6/5r, 1], [1, 6/5r]) }
|
|
266
267
|
let(:polygon5) { described_class.new([0, 0], [4, 4], [0, 4]) }
|
|
267
268
|
|
|
268
|
-
|
|
269
|
-
|
|
270
269
|
it 'should raise TypeError' do
|
|
271
270
|
expect{ poly1.distance(l1) }.to raise_error(TypeError)
|
|
272
271
|
expect{ poly1.distance("") }.to raise_error(TypeError)
|
data/spec/d2/ray_spec.rb
CHANGED
|
@@ -392,7 +392,7 @@ RSpec.describe EasyGeometry::D2::Ray do
|
|
|
392
392
|
end
|
|
393
393
|
|
|
394
394
|
it 'should return correct number' do
|
|
395
|
-
expect(r1.distance(
|
|
395
|
+
expect(r1.distance([-1, -1])).to eq(Math.sqrt(2))
|
|
396
396
|
expect(r1.distance(EasyGeometry::D2::Point.new(-1, 1))).to eq(Math.sqrt(2))
|
|
397
397
|
end
|
|
398
398
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: easy_geometry
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Henry Metlov
|
|
@@ -34,7 +34,8 @@ files:
|
|
|
34
34
|
- ".ruby-version"
|
|
35
35
|
- Gemfile
|
|
36
36
|
- Gemfile.lock
|
|
37
|
-
-
|
|
37
|
+
- LICENSE
|
|
38
|
+
- README.md
|
|
38
39
|
- easy_geometry.gemspec
|
|
39
40
|
- lib/.ruby-version
|
|
40
41
|
- lib/easy_geometry.rb
|
data/README
DELETED