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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9a73c24a00cc5b70d91928313b88a92fd4b7b911
4
- data.tar.gz: 90fc4f8ec1974af5a8e7663e89d99564791ad673
3
+ metadata.gz: 46a78d38c196c8397bc3ec3ed43c9a8792c1c0d4
4
+ data.tar.gz: 26630fe11dc5fa52b7bc286b504dd7b30998070c
5
5
  SHA512:
6
- metadata.gz: 5ec8e7d0cc9e81d4d974cd7b3d0a0bef4f636d2e0c17981937ab8fbeb48c8814763af0d61ec426536ffe2e0ffd300b497b12bc437d2aeca68c3f9f06e2db83c8
7
- data.tar.gz: b31be403bbaaa9510a7efe67d3a40427fd7440488445955cc32ec938c18b5c3315f4e2ce3c809c94a4b19ad5d9d26332d4026cd8eb6c9a1c98ea4ed199091d81
6
+ metadata.gz: 8bef479af4f0ca678c8a64de6694f8e4a9555c94b6f2fe9746b72468a118ea52a8b3e8013c82685f58209b8fb30f7eba499914b0c96780467d237777f2036193
7
+ data.tar.gz: 16bd08dc5295ce944e03a99bc3174768cf8a821d3fcbbd18b159579a02bdcb5a1d5bb795583ba4ebcbd4ca96b26468a2dd9fecceebd741e6ab176c5b1e9576b4
@@ -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
@@ -1,3 +1,3 @@
1
1
  module Euclidean
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: euclidean
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jeremy Ward