Logo

Programming-Idioms

History of Idiom 179 > diff from v2 to v3

Edit summary for version 3 by steenslag:
[Ruby] removed comment

Version 2

2019-01-11, 16:52:52

Version 3

2019-01-11, 16:53:43

Idiom #179 Get center of a rectangle

Return the center c of the rectangle with coördinates(x1,y1,x2,y2)

Idiom #179 Get center of a rectangle

Return the center c of the rectangle with coördinates(x1,y1,x2,y2)

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

Rect  = Struct.new(:x1, :y1, :x2, :y2) do
  def center  #returns a Point
    Point.new((x1+x2)/2.0, (y1+y2)/2.0)
  end
end

c = Rect.new(0,0,2,5).center
Code
Point = Struct.new(:x, :y)

Rect  = Struct.new(:x1, :y1, :x2, :y2) do
  def center  
    Point.new((x1+x2)/2.0, (y1+y2)/2.0)
  end
end

c = Rect.new(0,0,2,5).center
Comments bubble
Ruby has no predefined Point or Rect class. This code returns: <struct Point x=1.0, y=2.5>
Comments bubble
Ruby has no predefined Point or Rect class. This code returns: <struct Point x=1.0, y=2.5>