Class: Geom2D::Rectangle

Inherits:
Object
  • Object
show all
Defined in:
lib/geom2d/rectangle.rb

Overview

Represents an axis aligned rectangle.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x, y, width, height) ⇒ Rectangle

Creates a new Rectangle object, with (x, y) specifying the bottom-left corner of the rectangle.



38
39
40
41
42
43
# File 'lib/geom2d/rectangle.rb', line 38

def initialize(x, y, width, height)
  @x = x
  @y = y
  @width = width
  @height = height
end

Instance Attribute Details

#heightObject (readonly)

The height of the rectangle.



35
36
37
# File 'lib/geom2d/rectangle.rb', line 35

def height
  @height
end

#widthObject (readonly)

The width of the rectangle.



32
33
34
# File 'lib/geom2d/rectangle.rb', line 32

def width
  @width
end

#xObject (readonly)

The x-coordinate of the bottom-left corner of the rectangle.



26
27
28
# File 'lib/geom2d/rectangle.rb', line 26

def x
  @x
end

#yObject (readonly)

The y-coordinate of the bottom-left corner of the rectangle.



29
30
31
# File 'lib/geom2d/rectangle.rb', line 29

def y
  @y
end

Instance Method Details

#bboxObject

Returns the BoundingBox of this rectangle.



75
76
77
# File 'lib/geom2d/rectangle.rb', line 75

def bbox
  BoundingBox.new(x, y, x + width, y + height)
end

#ccw?Boolean

Returns true since the vertices of the rectangle are always ordered in a counterclockwise fashion.

Returns:

  • (Boolean)


81
82
83
# File 'lib/geom2d/rectangle.rb', line 81

def ccw?
  true
end

#each_segment {|Geom2D::Segment.new(v[-1], v[0])| ... } ⇒ Object

Calls the given block once for each segment of the rectangle.

If no block is given, an Enumerator is returned.

Yields:



66
67
68
69
70
71
72
# File 'lib/geom2d/rectangle.rb', line 66

def each_segment
  return to_enum(__method__) unless block_given?

  v = vertices
  v.each_cons(2) {|v1, v2| yield(Geom2D::Segment.new(v1, v2)) }
  yield(Geom2D::Segment.new(v[-1], v[0]))
end

#each_vertex(&block) ⇒ Object

Calls the given block once for each corner of the rectangle.

If no block is given, an Enumerator is returned.



58
59
60
61
# File 'lib/geom2d/rectangle.rb', line 58

def each_vertex(&block)
  return to_enum(__method__) unless block_given?
  vertices.each(&block)
end

#inspectObject Also known as: to_s

:nodoc:



85
86
87
# File 'lib/geom2d/rectangle.rb', line 85

def inspect # :nodoc:
  "Rectangle[(#{@x},#{@y}),width=#{@width},height=#{@height}]"
end

#nr_of_contoursObject

Returns one since a rectangle object is a single polygon.



46
47
48
# File 'lib/geom2d/rectangle.rb', line 46

def nr_of_contours
  1
end

#nr_of_verticesObject

Returns four since a rectangle has four vertices.



51
52
53
# File 'lib/geom2d/rectangle.rb', line 51

def nr_of_vertices
  4
end

#to_aryObject Also known as: to_a

Returns an array with the vertices of the rectangle.



91
92
93
# File 'lib/geom2d/rectangle.rb', line 91

def to_ary
  vertices
end