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

Accessors collapse

Instance Method Summary collapse

Methods inherited from Square

new

Methods included from ClusterFactory

included

Constructor Details

#initialize(center, size) ⇒ CenteredSquare

Returns a new instance of CenteredSquare.

Parameters:

  • center (Point)

    The center point

  • size (Numeric)

    The length of each side



117
118
119
120
# File 'lib/geometry/square.rb', line 117

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

Instance Attribute Details

#centerPoint (readonly)

Returns The center of the Square.

Returns:



109
110
111
# File 'lib/geometry/square.rb', line 109

def center
  @center
end

#originPoint (readonly)

Returns The lower left corner.

Returns:

  • (Point)

    The lower left corner



142
143
144
# File 'lib/geometry/square.rb', line 142

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

#pointsArray<Point> (readonly)

Returns The Square‘s four points (counterclockwise).

Returns:

  • (Array<Point>)

    The Square‘s four points (counterclockwise)



148
149
150
151
152
153
154
155
156
# File 'lib/geometry/square.rb', line 148

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

#sizeSize

Returns The Size of the Square.

Returns:



113
114
115
# File 'lib/geometry/square.rb', line 113

def size
  @size
end

Instance Method Details

#heightObject



158
159
160
# File 'lib/geometry/square.rb', line 158

def height
    @size
end

#maxPoint

Returns The upper right corner of the bounding Rectangle.

Returns:



124
125
126
127
# File 'lib/geometry/square.rb', line 124

def max
    half_size = @size/2
    Point[@center.x + half_size, @center.y + half_size]
end

#minPoint

Returns The lower left corner of the bounding Rectangle.

Returns:



130
131
132
133
# File 'lib/geometry/square.rb', line 130

def min
    half_size = @size/2
    Point[@center.x - half_size, @center.y - half_size]
end

#minmaxArray<Point>

Returns The lower left and upper right corners of the bounding Rectangle.

Returns:

  • (Array<Point>)

    The lower left and upper right corners of the bounding Rectangle



136
137
138
# File 'lib/geometry/square.rb', line 136

def minmax
    [self.min, self.max]
end

#widthObject



162
163
164
# File 'lib/geometry/square.rb', line 162

def width
    @size
end