Class: Geometry::Obround

Inherits:
Object
  • Object
show all
Includes:
ClusterFactory
Defined in:
lib/geometry/obround.rb

Overview

The Obround class cluster represents a rectangle with semicircular end caps

http://en.wiktionary.org/wiki/obround

Direct Known Subclasses

CenteredObround, SizedObround

Accessors collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ClusterFactory

included

Constructor Details

#initialize(point0, point1) ⇒ Obround

Create a Geometry::Obround using the given Points

@param [Point0]  point0  The bottom-left corner (closest to the origin)
@param [Point1]  point1  The top-right corner (farthest from the origin)

Raises:

  • (ArgumentError)


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/geometry/obround.rb', line 64

def initialize(point0, point1)
    point0, point1 = Point[point0], Point[point1]
    raise(ArgumentError, "Point sizes must match") unless point0.size == point1.size

    # Reorder the points to get lower-left and upper-right
    if (point0.x > point1.x) && (point0.y > point1.y)
	point0, point1 = point1, point0
    else
	p0x, p1x = [point0.x, point1.x].minmax
	p0y, p1y = [point0.y, point1.y].minmax
	point0 = Point[p0x, p0y]
	point1 = Point[p1x, p1y]
    end
    @points = [point0, point1]
end

Class Method Details

.new(width, height) ⇒ CenteredObround .new(size) ⇒ CenteredObround .new(point0, point1) ⇒ Object .new(origin, size) ⇒ SizedObround .new(left, bottom, right, top) ⇒ Obround

Overloads:

  • .new(width, height) ⇒ CenteredObround

    Creates a Geometry::Obround of the given width and height, centered on the origin

    Parameters:

    • height (Number)

      Height

    • width (Number)

      Width

    Returns:

  • .new(size) ⇒ CenteredObround

    Creates a Geometry::Obround of the given Size centered on the origin

    Parameters:

    • size (Size)

      Width and height

    Returns:

  • .new(point0, point1) ⇒ Object

    Creates a Geometry::Obround using the given Points

    Parameters:

    • point0 (Point)

      A corner

    • point1 (Point)

      The other corner

  • .new(origin, size) ⇒ SizedObround

    Creates a Geometry::Obround from the given origin and size

    Parameters:

    • origin (Point)

      Lower-left corner

    • size (Size)

      Width and height

    Returns:

  • .new(left, bottom, right, top) ⇒ Obround

    Creates a Geometry::Obround from the locations of each side

    Parameters:

    • left (Number)

      X-coordinate of the left side

    • bottom (Number)

      Y-coordinate of the bottom edge

    • right (Number)

      X-coordinate of the right side

    • top (Number)

      Y-coordinate of the top edge

    Returns:



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/geometry/obround.rb', line 40

def self.new(*args)
    case args.size
	when 1
	    CenteredObround.new(args[0])
	when 2
	    if args.all? {|a| a.is_a?(Numeric) }
		CenteredObround.new(Size[*args])
	    elsif args.all? {|a| a.is_a?(Array) || a.is_a?(Point) }
		original_new(*args)
	    elsif (args[0].is_a?(Point) or args[0].is_a?(Array))and args[1].is_a?(Size)
		SizedObround.new(*args)
	    else
		raise ArgumentError, "Invalid arguments #{args}"
	    end
	when 4
	    raise ArgumentError unless args.all? {|a| a.is_a?(Numeric)}
	    left, bottom, right, top = *args
	    original_new(Point[left, bottom], Point[right, top])
    end
end

Instance Method Details

#centerPoint

Returns The Geometry::Obround‘s center.

Returns:



88
89
90
91
# File 'lib/geometry/obround.rb', line 88

def center
    min, max = @points.minmax {|a,b| a.y <=> b.y}
    Point[(max.x+min.x)/2, (max.y+min.y)/2]
end

#eql?(other) ⇒ Boolean Also known as: ==

Returns:

  • (Boolean)


80
81
82
# File 'lib/geometry/obround.rb', line 80

def eql?(other)
    self.points == other.points
end

#heightObject



107
108
109
110
# File 'lib/geometry/obround.rb', line 107

def height
    min, max = @points.minmax {|a,b| a.y <=> b.y}
    max.y - min.y
end

#originObject



101
102
103
104
105
# File 'lib/geometry/obround.rb', line 101

def origin
    minx = @points.min {|a,b| a.x <=> b.x}
    miny = @points.min {|a,b| a.y <=> b.y}
    Point[minx.x, miny.y]
end

#pointsArray<Point>

Returns The Geometry::Obround‘s four points (counterclockwise).

Returns:



94
95
96
97
98
99
# File 'lib/geometry/obround.rb', line 94

def points
    point0, point2 = *@points
    point1 = Point[point2.x, point0.y]
    point3 = Point[point0.x, point2.y]
    [point0, point1, point2, point3]
end

#widthObject



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

def width
    min, max = @points.minmax {|a,b| a.x <=> b.x}
    max.x - min.x
end