lib/geometry/square.rb in geometry-6.5 vs lib/geometry/square.rb in geometry-6.6
- old
+ new
@@ -14,14 +14,10 @@
Square.new size:5 # Centered on the origin
=end
class Square
include ClusterFactory
- # @!attribute origin
- # @return [Point] The {Square}'s origin
- attr_reader :origin
-
# @!attribute points
# @return [Array<Point>] the corner {Point}s of the {Square} in counter-clockwise order
attr_reader :points
alias :vertices :points
@@ -99,19 +95,20 @@
# @return [Array<Point>] The lower left and upper right corners of the bounding {Rectangle}
def minmax
[self.min, self.max]
end
- # @attribute [r] origin
- # @return [Point] The lower left corner
+ # @!attribute origin
+ # @return [Point] The {Square}'s origin (lower-left corner)
def origin
@points.first
end
+ # @return [Array<Point>] The {Square}'s four points (clockwise)
def points
p0, p1 = *@points
- [p0, Point[p1.x, p0.y], p1, Point[p0.x, p1.y]]
+ [p0, Point[p0.x, p1.y], p1, Point[p1.x, p0.y]]
end
def height
min, max = @points.minmax {|a,b| a.y <=> b.y}
max.y - min.y
@@ -120,10 +117,15 @@
def width
min, max = @points.minmax {|a,b| a.x <=> b.x}
max.x - min.x
end
# @endgroup
+
+ # @return [Path] A closed {Path} that traces the boundary of the {Square} clockwise, starting from the lower-left
+ def path
+ Path.new(*self.points, self.points.first)
+ end
end
# A {Square} created with a center point and a size
class CenteredSquare < Square
# @attribute [r] center
@@ -164,19 +166,19 @@
def origin
Point[@center.x - size/2, @center.y - size/2]
end
# @attribute [r] points
- # @return [Array<Point>] The {Square}'s four points (counterclockwise)
+ # @return [Array<Point>] The {Square}'s four points (clockwise)
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]]
+ [Point[minx,miny], Point[minx, maxy], Point[maxx, maxy], Point[maxx,miny]]
end
def height
@size
end
@@ -222,17 +224,17 @@
def origin
@origin
end
# @attribute [r] points
- # @return [Array<Point>] The {Square}'s four points (counterclockwise)
+ # @return [Array<Point>] The {Square}'s four points (clockwise)
def points
minx = origin.x
maxx = origin.x + size
miny = origin.y
maxy = origin.y + size
- [origin, Point[maxx, miny], Point[maxx, maxy], Point[minx,maxy]]
+ [origin, Point[minx, maxy], Point[maxx, maxy], Point[maxx,miny]]
end
# @return [Number] The size of the {Square} along the y-axis
def height
@size