Class: Geometry::CenteredSquare

Inherits:
Square
  • Object
show all
Defined in:
lib/geometry/square.rb

Overview

A Square created with a center point and a size

Accessors collapse

Instance Attribute Summary collapse

Attributes inherited from Square

#options

Accessors collapse

Instance Method Summary collapse

Constructor Details

#initialize(center, size) ⇒ CenteredSquare



75
76
77
78
# File 'lib/geometry/square.rb', line 75

def initialize(center, size)
    @center = Point[center]
    @size = size
end

Instance Attribute Details

#centerPoint (readonly)



71
72
73
# File 'lib/geometry/square.rb', line 71

def center
  @center
end

#originPoint (readonly)



83
84
85
# File 'lib/geometry/square.rb', line 83

def origin
    Point[@center.x - size/2, @center.y - size/2]
end

#pointsArray<Point> (readonly)



89
90
91
92
93
94
95
96
97
# File 'lib/geometry/square.rb', line 89

def points
    half_size = @size/2
    minx = @center.x - half_size
    maxx = @center.x + half_size
    miny = @center.y - half_size
    maxy = @center.y + half_size

    [Point[minx,miny], Point[maxx, miny], Point[maxx, maxy], Point[minx,maxy]]
end

Instance Method Details

#heightObject



99
100
101
# File 'lib/geometry/square.rb', line 99

def height
    @size
end

#widthObject



103
104
105
# File 'lib/geometry/square.rb', line 103

def width
    @size
end