euclidean 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/euclidean/circle.rb +13 -0
- data/lib/euclidean/version.rb +1 -1
- data/spec/euclidean/circle_spec.rb +33 -0
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 46a78d38c196c8397bc3ec3ed43c9a8792c1c0d4
|
4
|
+
data.tar.gz: 26630fe11dc5fa52b7bc286b504dd7b30998070c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8bef479af4f0ca678c8a64de6694f8e4a9555c94b6f2fe9746b72468a118ea52a8b3e8013c82685f58209b8fb30f7eba499914b0c96780467d237777f2036193
|
7
|
+
data.tar.gz: 16bd08dc5295ce944e03a99bc3174768cf8a821d3fcbbd18b159579a02bdcb5a1d5bb795583ba4ebcbd4ca96b26468a2dd9fecceebd741e6ab176c5b1e9576b4
|
data/lib/euclidean/circle.rb
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'mathn'
|
2
|
+
|
1
3
|
module Euclidean
|
2
4
|
=begin rdoc
|
3
5
|
Circles come in all shapes and sizes, but they're usually round.
|
@@ -11,6 +13,7 @@ Circles come in all shapes and sizes, but they're usually round.
|
|
11
13
|
|
12
14
|
class Circle
|
13
15
|
include ClusterFactory
|
16
|
+
include Comparable
|
14
17
|
|
15
18
|
# @return [Point] The {Circle}'s center point
|
16
19
|
attr_reader :center
|
@@ -72,6 +75,16 @@ Circles come in all shapes and sizes, but they're usually round.
|
|
72
75
|
@radius*2
|
73
76
|
end
|
74
77
|
|
78
|
+
# Two circles intersect if, and only if, the distance between their centers is between the sum and the difference of their radii.
|
79
|
+
# Given two circles (x0,y0,R0) and (x1,y1,R1), the formula is as follows:
|
80
|
+
# (R0-R1)^2 <= (x0-x1)^2+(y0-y1)^2 <= (R0+R1)^2
|
81
|
+
# @param [Circle] a {Circle} object
|
82
|
+
# @return [Boolean]
|
83
|
+
def intersects_circle?(other)
|
84
|
+
raise TypeError, "#{other.class} must be a #{self.class}" unless other.kind_of? Euclidean::Circle
|
85
|
+
( (self.center.x - other.center.x)**2 + (self.center.y - other.center.y)**2 ).between?( ((self.radius - other.radius)**2), ((self.radius + other.radius)**2) )
|
86
|
+
end
|
87
|
+
|
75
88
|
# @return [Point] The upper right corner of the bounding {Rectangle}
|
76
89
|
def max
|
77
90
|
@center+radius
|
data/lib/euclidean/version.rb
CHANGED
@@ -112,4 +112,37 @@ describe Euclidean::Circle do
|
|
112
112
|
end
|
113
113
|
end
|
114
114
|
|
115
|
+
describe "#intersects_circle?" do
|
116
|
+
let(:circle_1) { Circle.new [1, 2], 3}
|
117
|
+
let(:circle_2) { Circle.new [3, 0], 3}
|
118
|
+
let(:circle_3) { Circle.new [9, 0], 3}
|
119
|
+
let(:circle_4) { Circle.new diameter: 4 }
|
120
|
+
|
121
|
+
context "When two circles intersect" do
|
122
|
+
it "returns true" do
|
123
|
+
expect(circle_1.intersects_circle?(circle_2)).to be true
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
context "When two circles intersect at only one point" do
|
128
|
+
it "returns true" do
|
129
|
+
expect(circle_2.intersects_circle?(circle_3)).to be true
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
context "When two circles do not intersect" do
|
134
|
+
it "returns false" do
|
135
|
+
expect(circle_1.intersects_circle?(circle_3)).to be false
|
136
|
+
end
|
137
|
+
end
|
138
|
+
|
139
|
+
context "When compared to a non-circle" do
|
140
|
+
it "raises an error" do
|
141
|
+
rectangle = Rectangle.new([-1,0], [3,4])
|
142
|
+
expect{ circle_1.intersects_circle?(rectangle) }.to raise_error TypeError
|
143
|
+
expect{ circle_4.intersects_circle?(rectangle) }.to raise_error TypeError
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
115
148
|
end
|