summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTom Black <[email protected]>2018-12-12 00:10:14 -0800
committerGitHub <[email protected]>2018-12-12 00:10:14 -0800
commit241093f3ea07255af6d36fa9e07c2a1ecb7c712f (patch)
treec9987a2eb4d98820ba5a8845909e4c9430d31307
parent0f52b2115a5f910ae4d3350f7400bb11622ea899 (diff)
downloadruby2d-241093f3ea07255af6d36fa9e07c2a1ecb7c712f.tar.gz
ruby2d-241093f3ea07255af6d36fa9e07c2a1ecb7c712f.zip
Make `contains?` inclusive (#137)
If point is over the visual area, it's true. Also add default rectangle implementation to `renderable.rb`.
-rw-r--r--lib/ruby2d/image.rb4
-rw-r--r--lib/ruby2d/line.rb6
-rw-r--r--lib/ruby2d/rectangle.rb4
-rw-r--r--lib/ruby2d/renderable.rb4
-rw-r--r--lib/ruby2d/text.rb4
-rw-r--r--test/image_spec.rb15
-rw-r--r--test/line_spec.rb11
-rw-r--r--test/quad_spec.rb30
-rw-r--r--test/rectangle_spec.rb20
-rw-r--r--test/renderable_spec.rb49
-rw-r--r--test/triangle_spec.rb19
11 files changed, 80 insertions, 86 deletions
diff --git a/lib/ruby2d/image.rb b/lib/ruby2d/image.rb
index 0516f4f..236df19 100644
--- a/lib/ruby2d/image.rb
+++ b/lib/ruby2d/image.rb
@@ -27,9 +27,5 @@ module Ruby2D
@color = Color.new(c)
end
- def contains?(x, y)
- @x < x and @x + @width > x and @y < y and @y + @height > y
- end
-
end
end
diff --git a/lib/ruby2d/line.rb b/lib/ruby2d/line.rb
index e817acb..8beb8ff 100644
--- a/lib/ruby2d/line.rb
+++ b/lib/ruby2d/line.rb
@@ -32,9 +32,9 @@ module Ruby2D
# the width. For reference:
# https://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line
def contains?(x, y)
- points_distance(x1, y1, x, y) < length and
- points_distance(x2, y2, x, y) < length and
- (((@y2 - @y1) * x - (@x2 - @x1) * y + @x2 * @y1 - @y2 * @x1).abs / length) < 0.5 * @width
+ points_distance(x1, y1, x, y) <= length &&
+ points_distance(x2, y2, x, y) <= length &&
+ (((@y2 - @y1) * x - (@x2 - @x1) * y + @x2 * @y1 - @y2 * @x1).abs / length) <= 0.5 * @width
end
private
diff --git a/lib/ruby2d/rectangle.rb b/lib/ruby2d/rectangle.rb
index 43b0db5..07ee19b 100644
--- a/lib/ruby2d/rectangle.rb
+++ b/lib/ruby2d/rectangle.rb
@@ -40,10 +40,6 @@ module Ruby2D
update_coords(@x, @y, @width, h)
end
- def contains?(x, y)
- @x < x and @x + @width > x and @y < y and @y + @height > y
- end
-
private
def update_coords(x, y, w, h)
diff --git a/lib/ruby2d/renderable.rb b/lib/ruby2d/renderable.rb
index 0e5da50..2599bc4 100644
--- a/lib/ruby2d/renderable.rb
+++ b/lib/ruby2d/renderable.rb
@@ -42,9 +42,9 @@ module Ruby2D
def colour; self.color end
def colour=(c); self.color = c end
- # Add a contains method stub
+ # Check if given point is within a rectangle, by default (unless overridden)
def contains?(x, y)
- raise Error, "\`#contains?\` not implemented for this class yet"
+ x > @x && x < (@x + @width) && y > @y && y < (@y + @height)
end
end
diff --git a/lib/ruby2d/text.rb b/lib/ruby2d/text.rb
index 796cdb6..ea9a2a9 100644
--- a/lib/ruby2d/text.rb
+++ b/lib/ruby2d/text.rb
@@ -32,9 +32,5 @@ module Ruby2D
@color = Color.new(c)
end
- def contains?(x, y)
- @x < x and @x + @width > x and @y < y and @y + @height > y
- end
-
end
end
diff --git a/test/image_spec.rb b/test/image_spec.rb
index e2ef37e..7d4f96d 100644
--- a/test/image_spec.rb
+++ b/test/image_spec.rb
@@ -8,19 +8,4 @@ RSpec.describe Ruby2D::Image do
end
end
- describe "#contains?" do
- it "returns true if point is inside the image" do
- image = Image.new("test/media/image.bmp")
- expect(image.contains?(50, 50)).to be true
- end
-
- it "returns true if point is outside the image" do
- image = Image.new("test/media/image.bmp")
- expect(image.contains?(-50, 50)).to be false
- expect(image.contains?(50, -50)).to be false
- expect(image.contains?(50, 150)).to be false
- expect(image.contains?(150, 50)).to be false
- end
- end
-
end
diff --git a/test/line_spec.rb b/test/line_spec.rb
index ac6d005..2129073 100644
--- a/test/line_spec.rb
+++ b/test/line_spec.rb
@@ -3,15 +3,16 @@ require 'ruby2d'
RSpec.describe Ruby2D::Line do
describe "#contains?" do
+ line = Line.new(x1: 0, y1: 0, x2: 100, y2: 100)
+
it "returns true if point is inside the line" do
- line = Line.new(x1: 0, y1: 0, x2: 100, y2: 100)
- expect(line.contains?(25, 25)).to be true
+ expect(line.contains?( 0, 1)).to be true
+ expect(line.contains?(100, 100)).to be true
end
it "returns false if point is outside the line" do
- line = Line.new(x1: 0, y1: 0, x2: 100, y2: 100)
- expect(line.contains?(0, 10)).to be false
- expect(line.contains?(10, 0)).to be false
+ expect(line.contains?( 0, 2)).to be false
+ expect(line.contains?(101, 0)).to be false
end
end
diff --git a/test/quad_spec.rb b/test/quad_spec.rb
index 1819fc0..8cace25 100644
--- a/test/quad_spec.rb
+++ b/test/quad_spec.rb
@@ -53,27 +53,21 @@ RSpec.describe Ruby2D::Quad do
end
describe "#contains?" do
+ quad = Quad.new(
+ x1: -25, y1: 0,
+ x2: 0, y2: -25,
+ x3: 25, y3: 0,
+ x4: 0, y4: 25
+ )
+
it "returns true if point is inside the quad" do
- quad = Quad.new(
- x1: -25, y1: 0,
- x2: 0, y2: -25,
- x3: 25, y3: 0,
- x4: 0, y4: 25
- )
- expect(quad.contains?(0, 0)).to be true
+ expect(quad.contains?(0 , 0)).to be true
+ expect(quad.contains?(25, 0)).to be true
end
- it "returns true if point is outside the quad" do
- quad = Quad.new(
- x1: -25, y1: 0,
- x2: 0, y2: -25,
- x3: 25, y3: 0,
- x4: 0, y4: 25
- )
- expect(quad.contains?( 20, 20)).to be false
- expect(quad.contains?(-20, 20)).to be false
- expect(quad.contains?( 20, -20)).to be false
- expect(quad.contains?(-20, -20)).to be false
+ it "returns false if point is outside the quad" do
+ expect(quad.contains?(-26, 0)).to be false
+ expect(quad.contains?( 0, 26)).to be false
end
end
diff --git a/test/rectangle_spec.rb b/test/rectangle_spec.rb
deleted file mode 100644
index bbf1616..0000000
--- a/test/rectangle_spec.rb
+++ /dev/null
@@ -1,20 +0,0 @@
-require 'ruby2d'
-
-RSpec.describe Ruby2D::Rectangle do
-
- describe "#contains?" do
- it "returns true if point is inside the rectangle" do
- rectangle = Rectangle.new(x: 0, y: 0, width: 50, height: 50)
- expect(rectangle.contains?(25, 25)).to be true
- end
-
- it "returns true if point is outside the rectangle" do
- rectangle = Rectangle.new(x: 0, y: 0, width: 50, height: 50)
- expect(rectangle.contains?(-25, 25)).to be false
- expect(rectangle.contains?( 25, -25)).to be false
- expect(rectangle.contains?( 25, 50)).to be false
- expect(rectangle.contains?( 50, 25)).to be false
- end
- end
-
-end
diff --git a/test/renderable_spec.rb b/test/renderable_spec.rb
index 56ee521..1352d5b 100644
--- a/test/renderable_spec.rb
+++ b/test/renderable_spec.rb
@@ -63,4 +63,53 @@ RSpec.describe Ruby2D::Renderable do
expect(quad.a).to eq(0.6)
end
+ describe "#contains?" do
+ square = Square.new(x: 1, y: 1, size: 2)
+
+ # Grid looks like this, 2x2 square at point (1, 1):
+ #
+ # 0 1 2 3 4
+ # 0 +--+--+--+--+
+ # | | | | |
+ # 1 +--+--+--+--+
+ # | |XX|XX| |
+ # 2 +--+--+--+--+
+ # | |XX|XX| |
+ # 3 +--+--+--+--+
+ # | | | | |
+ # 4 +--+--+--+--+
+
+ it "returns true if point is inside the rectangle" do
+ expect(square.contains?(1, 1)).to be true
+ expect(square.contains?(2, 1)).to be true
+ expect(square.contains?(3, 1)).to be true
+ expect(square.contains?(1, 2)).to be true
+ expect(square.contains?(2, 2)).to be true
+ expect(square.contains?(3, 2)).to be true
+ expect(square.contains?(1, 3)).to be true
+ expect(square.contains?(2, 3)).to be true
+ expect(square.contains?(3, 3)).to be true
+ end
+
+ it "returns false if point is outside the rectangle" do
+ # Clockwise around the square
+ expect(square.contains?(0, 0)).to be false
+ expect(square.contains?(1, 0)).to be false
+ expect(square.contains?(2, 0)).to be false
+ expect(square.contains?(3, 0)).to be false
+ expect(square.contains?(4, 0)).to be false
+ expect(square.contains?(4, 1)).to be false
+ expect(square.contains?(4, 2)).to be false
+ expect(square.contains?(4, 3)).to be false
+ expect(square.contains?(4, 4)).to be false
+ expect(square.contains?(3, 4)).to be false
+ expect(square.contains?(2, 4)).to be false
+ expect(square.contains?(1, 4)).to be false
+ expect(square.contains?(0, 4)).to be false
+ expect(square.contains?(0, 3)).to be false
+ expect(square.contains?(0, 2)).to be false
+ expect(square.contains?(0, 1)).to be false
+ end
+ end
+
end
diff --git a/test/triangle_spec.rb b/test/triangle_spec.rb
index 1f9dc5d..2600d6f 100644
--- a/test/triangle_spec.rb
+++ b/test/triangle_spec.rb
@@ -52,24 +52,21 @@ RSpec.describe Ruby2D::Triangle do
end
describe "#contains?" do
+ triangle = Triangle.new(
+ x1: 0, y1: 0,
+ x2: 0, y2: 100,
+ x3: 100, y3: 0
+ )
+
it "returns true if point is inside the triangle" do
- triangle = Triangle.new(
- x1: 0, y1: 0,
- x2: 0, y2: 100,
- x3: 100, y3: 0
- )
+ expect(triangle.contains?( 0, 0)).to be true
expect(triangle.contains?(25, 25)).to be true
end
it "returns false if point is outside the triangle" do
- triangle = Triangle.new(
- x1: 0, y1: 0,
- x2: 0, y2: 100,
- x3: 100, y3: 0
- )
expect(triangle.contains?( 25, -25)).to be false
expect(triangle.contains?(-25, 25)).to be false
- expect(triangle.contains?(100, 100)).to be false
+ expect(triangle.contains?(100, 1)).to be false
end
end