From 241093f3ea07255af6d36fa9e07c2a1ecb7c712f Mon Sep 17 00:00:00 2001 From: Tom Black Date: Wed, 12 Dec 2018 00:10:14 -0800 Subject: Make `contains?` inclusive (#137) If point is over the visual area, it's true. Also add default rectangle implementation to `renderable.rb`. --- test/image_spec.rb | 15 --------------- test/line_spec.rb | 11 ++++++----- test/quad_spec.rb | 30 ++++++++++++------------------ test/rectangle_spec.rb | 20 -------------------- test/renderable_spec.rb | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ test/triangle_spec.rb | 19 ++++++++----------- 6 files changed, 75 insertions(+), 69 deletions(-) delete mode 100644 test/rectangle_spec.rb (limited to 'test') 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 -- cgit v1.2.3