Class: Ruby2D::Rectangle

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

Overview

A rectangle

Direct Known Subclasses

Square

Constant Summary

Constants included from Interactive

Interactive::OBJECT_EVENTS, Interactive::OBJECT_EVENT_FILTER_PREDICATES

Instance Attribute Summary

Attributes inherited from Quad

#fill, #rotate, #stroke_color, #stroke_width, #x1, #x2, #x3, #x4, #y1, #y2, #y3, #y4

Attributes included from Renderable

#color, #height, #padding_bottom, #padding_left, #padding_right, #padding_top, #visible, #width, #x_align, #y_align, #z

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Quad

#color=, #height, #rx=, #ry=, #width

Methods included from Renderable

#_alignment_anchor_dx, #_alignment_anchor_dy, #_apply_padding, #_extract_alignment, #_point_in_polygon?, #_point_on_segment?, #_require_numeric_position, #_resolve_alignment, #_unrotate, #_validate_dimensions, #add, #colour=, flatten_color, flatten_per_vertex, flatten_points, flatten_resolved_color, #hide, #opacity, #opacity=, #padding=, #remove, resolve_color_or_default, resolve_single_color, #show

Methods included from Interactive

#_fire_event, #interactive?, #off, #on

Constructor Details

#initialize(x: 0, y: 0, width: 200, height: 100, z: 0, rotate: 0, rx: nil, ry: nil, color: nil, colour: nil, opacity: nil, fill: true, stroke_width: 0, stroke_color: nil, stroke_colour: nil, add: true, visible: true, padding: nil, padding_top: nil, padding_right: nil, padding_bottom: nil, padding_left: nil) ⇒ Rectangle

Create a rectangle



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/ruby2d/rectangle.rb', line 7

def initialize(x: 0, y: 0, width: 200, height: 100, z: 0, rotate: 0,
               rx: nil, ry: nil, color: nil, colour: nil, opacity: nil,
               fill: true, stroke_width: 0, stroke_color: nil, stroke_colour: nil,
               add: true, visible: true,
               padding: nil, padding_top: nil, padding_right: nil,
               padding_bottom: nil, padding_left: nil)
  x, y = _extract_alignment(x, y)
  _apply_padding(padding, padding_top, padding_right, padding_bottom, padding_left)
  _validate_dimensions(width: width, height: height)
  @width = width
  @height = height
  super(x1: @x = x, y1: @y = y,
        x2: x + width, y2: y,
        x3: x + width, y3: y + height,
        x4: x, y4: y + height, z: z, rotate: rotate,
        rx: rx, ry: ry,
        color: color, colour: colour, opacity: opacity,
        fill: fill, stroke_width: stroke_width,
        stroke_color: stroke_color, stroke_colour: stroke_colour,
        add: add, visible: visible)
end

Class Method Details

.render(x: 0, y: 0, width: 200, height: 100, rotate: 0, rx: nil, ry: nil, color: nil, colour: nil, opacity: nil, fill: true, stroke_width: 0, stroke_color: nil, stroke_colour: nil) ⇒ Object

Render a rectangle without creating an instance. Calls the positional internal directly rather than super — see Quad.draw_immediate.



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/ruby2d/rectangle.rb', line 88

def self.render(x: 0, y: 0, width: 200, height: 100, rotate: 0,
                rx: nil, ry: nil, color: nil, colour: nil, opacity: nil,
                fill: true, stroke_width: 0, stroke_color: nil, stroke_colour: nil)
  draw_immediate(x, y,
                 x + width, y,
                 x + width, y + height,
                 x, y + height,
                 rotate, rx || x + width / 2.0, ry || y + height / 2.0,
                 color || colour, opacity, fill, stroke_width,
                 stroke_color || stroke_colour)
end

Instance Method Details

#contains?(x, y) ⇒ Boolean

Check if the rectangle contains the given point

Returns:

  • (Boolean)


101
102
103
104
# File 'lib/ruby2d/rectangle.rb', line 101

def contains?(x, y)
  x, y = _unrotate(x, y)
  x >= @x && x <= (@x + @width) && y >= @y && y <= (@y + @height)
end

#height=(height) ⇒ Object

Set the height



80
81
82
83
84
# File 'lib/ruby2d/rectangle.rb', line 80

def height=(height)
  @height = height
  @y3 = @y1 + height
  @y4 = @y1 + height
end

#rxObject

Get the rotation center x coordinate



41
42
43
# File 'lib/ruby2d/rectangle.rb', line 41

def rx
  @_user_rx.nil? ? (@x1 + @width / 2.0) : @_user_rx
end

#ryObject

Get the rotation center y coordinate



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

def ry
  @_user_ry.nil? ? (@y1 + @height / 2.0) : @_user_ry
end

#width=(width) ⇒ Object

Set the width



73
74
75
76
77
# File 'lib/ruby2d/rectangle.rb', line 73

def width=(width)
  @width = width
  @x2 = @x1 + width
  @x3 = @x1 + width
end

#xObject

The x position (top-left). Rectangles are anchored at their top-left corner, not the centroid like a general Quad.



31
32
33
# File 'lib/ruby2d/rectangle.rb', line 31

def x
  @x1
end

#x=(x) ⇒ Object

Set the x position (top-left), updating all four vertices. Pass a symbol (:left, :center, :right) to set alignment intent.



52
53
54
55
56
57
58
59
# File 'lib/ruby2d/rectangle.rb', line 52

def x=(x)
  return self.x_align = x if x.is_a?(Symbol)
  @x_align = nil unless @_resolving_alignment
  @x = @x1 = x
  @x2 = x + @width
  @x3 = x + @width
  @x4 = x
end

#yObject

The y position (top-left).



36
37
38
# File 'lib/ruby2d/rectangle.rb', line 36

def y
  @y1
end

#y=(y) ⇒ Object

Set the y position (top-left), updating all four vertices. Pass a symbol (:top, :center, :bottom) to set alignment intent.



63
64
65
66
67
68
69
70
# File 'lib/ruby2d/rectangle.rb', line 63

def y=(y)
  return self.y_align = y if y.is_a?(Symbol)
  @y_align = nil unless @_resolving_alignment
  @y = @y1 = y
  @y2 = y
  @y3 = y + @height
  @y4 = y + @height
end