Logo

Programming-Idioms

History of Idiom 178 > diff from v2 to v3

Edit summary for version 3 by steenslag:
New Ruby implementation by user [steenslag]
ā†·

Version 2

2019-01-08, 17:34:02

Version 3

2019-01-11, 16:57:52

Idiom #178 Check if point is inside rectangle

Set boolean b to true if if the point with coƶrdinates (x,y) is inside the rectangle with coƶrdinates (x1,y1,x2,y2) , or to false otherwise.
Describe if the edges are considered to be inside the rectangle.

Idiom #178 Check if point is inside rectangle

Set boolean b to true if if the point with coƶrdinates (x,y) is inside the rectangle with coƶrdinates (x1,y1,x2,y2) , or to false otherwise.
Describe if the edges are considered to be inside the rectangle.

Code
Point = Struct.new(:x, :y)

Rect  = Struct.new(:x1, :y1, :x2, :y2) do
  def contains?(point)
    point.x.between?(x1,x2) && point.y.between?(y1,y2)
  end
end

b = Rect.new(0,0,2,5).contains?(Point.new(0,0))
Comments bubble
Ruby has no predefined Rect or Point classes. Edges are considered to be inside.