Class: Ruby2D::Quad
Overview
A quadrilateral based on four points in clockwise order starting at the top left.
Direct Known Subclasses
Constant Summary
Constants included from Interactive
Interactive::OBJECT_EVENTS, Interactive::OBJECT_EVENT_FILTER_PREDICATES
Instance Attribute Summary collapse
-
#fill ⇒ Object
Coordinates in clockwise order, starting at top left: x1,y1 == top left x2,y2 == top right x3,y3 == bottom right x4,y4 == bottom left.
-
#rotate ⇒ Object
Coordinates in clockwise order, starting at top left: x1,y1 == top left x2,y2 == top right x3,y3 == bottom right x4,y4 == bottom left.
-
#stroke_color ⇒ Object
(also: #stroke_colour)
Returns the value of attribute stroke_color.
-
#stroke_width ⇒ Object
Coordinates in clockwise order, starting at top left: x1,y1 == top left x2,y2 == top right x3,y3 == bottom right x4,y4 == bottom left.
-
#x1 ⇒ Object
Coordinates in clockwise order, starting at top left: x1,y1 == top left x2,y2 == top right x3,y3 == bottom right x4,y4 == bottom left.
-
#x2 ⇒ Object
Coordinates in clockwise order, starting at top left: x1,y1 == top left x2,y2 == top right x3,y3 == bottom right x4,y4 == bottom left.
-
#x3 ⇒ Object
Coordinates in clockwise order, starting at top left: x1,y1 == top left x2,y2 == top right x3,y3 == bottom right x4,y4 == bottom left.
-
#x4 ⇒ Object
Coordinates in clockwise order, starting at top left: x1,y1 == top left x2,y2 == top right x3,y3 == bottom right x4,y4 == bottom left.
-
#y1 ⇒ Object
Coordinates in clockwise order, starting at top left: x1,y1 == top left x2,y2 == top right x3,y3 == bottom right x4,y4 == bottom left.
-
#y2 ⇒ Object
Coordinates in clockwise order, starting at top left: x1,y1 == top left x2,y2 == top right x3,y3 == bottom right x4,y4 == bottom left.
-
#y3 ⇒ Object
Coordinates in clockwise order, starting at top left: x1,y1 == top left x2,y2 == top right x3,y3 == bottom right x4,y4 == bottom left.
-
#y4 ⇒ Object
Coordinates in clockwise order, starting at top left: x1,y1 == top left x2,y2 == top right x3,y3 == bottom right x4,y4 == bottom left.
Attributes included from Renderable
#color, #padding_bottom, #padding_left, #padding_right, #padding_top, #visible, #x_align, #y_align, #z
Class Method Summary collapse
-
.render(x1: 0, y1: 0, x2: 100, y2: 0, x3: 100, y3: 100, x4: 0, y4: 100, points: nil, 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 quad without creating an instance.
Instance Method Summary collapse
-
#color=(color) ⇒ Object
Set the color of the quad.
-
#contains?(x, y) ⇒ Boolean
Check if the quad contains the given point (inside the rendered fill).
- #height ⇒ Object
-
#initialize(x1: 0, y1: 0, x2: 100, y2: 0, x3: 100, y3: 100, x4: 0, y4: 100, points: nil, 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) ⇒ Quad
constructor
Create a quadrilateral.
-
#rx ⇒ Object
Get the rotation center x coordinate.
-
#rx=(val) ⇒ Object
Set the rotation center x coordinate.
-
#ry ⇒ Object
Get the rotation center y coordinate.
-
#ry=(val) ⇒ Object
Set the rotation center y coordinate.
-
#width ⇒ Object
Bounding-box width and height (extent across the four vertices).
-
#x ⇒ Object
The x coordinate of the centroid (average of the four vertex x's).
-
#x=(new_x) ⇒ Object
Set the x coordinate of the centroid, translating all vertices.
-
#y ⇒ Object
The y coordinate of the centroid (average of the four vertex y's).
-
#y=(new_y) ⇒ Object
Set the y coordinate of the centroid, translating all vertices.
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(x1: 0, y1: 0, x2: 100, y2: 0, x3: 100, y3: 100, x4: 0, y4: 100, points: nil, 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) ⇒ Quad
Create a quadrilateral. Specify vertices via points: or via
x1:/y1:/... points: takes precedence if both are given.
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/ruby2d/quad.rb', line 65 def initialize(x1: 0, y1: 0, x2: 100, y2: 0, x3: 100, y3: 100, x4: 0, y4: 100, points: nil, 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) if points Quad.send(:validate_points, points) x1, y1 = points[0] x2, y2 = points[1] x3, y3 = points[2] x4, y4 = points[3] end @x1 = x1 @y1 = y1 @x2 = x2 @y2 = y2 @x3 = x3 @y3 = y3 @x4 = x4 @y4 = y4 @z = z @rotate = rotate @_user_rx = rx @_user_ry = ry self.color = color || colour || 'white' self.opacity = opacity unless opacity.nil? @fill = fill @stroke_width = stroke_width self.stroke_color = stroke_color || stroke_colour || (color || colour) self.stroke_color.opacity = opacity unless opacity.nil? @visible = visible self.add if add end |
Instance Attribute Details
#fill ⇒ Object
Coordinates in clockwise order, starting at top left: x1,y1 == top left x2,y2 == top right x3,y3 == bottom right x4,y4 == bottom left
13 14 15 |
# File 'lib/ruby2d/quad.rb', line 13 def fill @fill end |
#rotate ⇒ Object
Coordinates in clockwise order, starting at top left: x1,y1 == top left x2,y2 == top right x3,y3 == bottom right x4,y4 == bottom left
13 14 15 |
# File 'lib/ruby2d/quad.rb', line 13 def rotate @rotate end |
#stroke_color ⇒ Object Also known as: stroke_colour
Returns the value of attribute stroke_color.
19 20 21 |
# File 'lib/ruby2d/quad.rb', line 19 def stroke_color @stroke_color end |
#stroke_width ⇒ Object
Coordinates in clockwise order, starting at top left: x1,y1 == top left x2,y2 == top right x3,y3 == bottom right x4,y4 == bottom left
13 14 15 |
# File 'lib/ruby2d/quad.rb', line 13 def stroke_width @stroke_width end |
#x1 ⇒ Object
Coordinates in clockwise order, starting at top left: x1,y1 == top left x2,y2 == top right x3,y3 == bottom right x4,y4 == bottom left
13 14 15 |
# File 'lib/ruby2d/quad.rb', line 13 def x1 @x1 end |
#x2 ⇒ Object
Coordinates in clockwise order, starting at top left: x1,y1 == top left x2,y2 == top right x3,y3 == bottom right x4,y4 == bottom left
13 14 15 |
# File 'lib/ruby2d/quad.rb', line 13 def x2 @x2 end |
#x3 ⇒ Object
Coordinates in clockwise order, starting at top left: x1,y1 == top left x2,y2 == top right x3,y3 == bottom right x4,y4 == bottom left
13 14 15 |
# File 'lib/ruby2d/quad.rb', line 13 def x3 @x3 end |
#x4 ⇒ Object
Coordinates in clockwise order, starting at top left: x1,y1 == top left x2,y2 == top right x3,y3 == bottom right x4,y4 == bottom left
13 14 15 |
# File 'lib/ruby2d/quad.rb', line 13 def x4 @x4 end |
#y1 ⇒ Object
Coordinates in clockwise order, starting at top left: x1,y1 == top left x2,y2 == top right x3,y3 == bottom right x4,y4 == bottom left
13 14 15 |
# File 'lib/ruby2d/quad.rb', line 13 def y1 @y1 end |
#y2 ⇒ Object
Coordinates in clockwise order, starting at top left: x1,y1 == top left x2,y2 == top right x3,y3 == bottom right x4,y4 == bottom left
13 14 15 |
# File 'lib/ruby2d/quad.rb', line 13 def y2 @y2 end |
#y3 ⇒ Object
Coordinates in clockwise order, starting at top left: x1,y1 == top left x2,y2 == top right x3,y3 == bottom right x4,y4 == bottom left
13 14 15 |
# File 'lib/ruby2d/quad.rb', line 13 def y3 @y3 end |
#y4 ⇒ Object
Coordinates in clockwise order, starting at top left: x1,y1 == top left x2,y2 == top right x3,y3 == bottom right x4,y4 == bottom left
13 14 15 |
# File 'lib/ruby2d/quad.rb', line 13 def y4 @y4 end |
Class Method Details
.render(x1: 0, y1: 0, x2: 100, y2: 0, x3: 100, y3: 100, x4: 0, y4: 100, points: nil, 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 quad without creating an instance
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/ruby2d/quad.rb', line 161 def self.render(x1: 0, y1: 0, x2: 100, y2: 0, x3: 100, y3: 100, x4: 0, y4: 100, points: nil, rotate: 0, rx: nil, ry: nil, color: nil, colour: nil, opacity: nil, fill: true, stroke_width: 0, stroke_color: nil, stroke_colour: nil) if points validate_points(points) x1, y1 = points[0] x2, y2 = points[1] x3, y3 = points[2] x4, y4 = points[3] end draw_immediate(x1, y1, x2, y2, x3, y3, x4, y4, rotate, rx, ry, color || colour, opacity, fill, stroke_width, stroke_color || stroke_colour) end |
Instance Method Details
#color=(color) ⇒ Object
Set the color of the quad
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/ruby2d/quad.rb', line 113 def color=(color) # convert to Color or Color::Set color = Color.set(color) # require 4 colours if multiple colours provided if color.is_a?(Color::Set) && color.length != 4 raise ArgumentError, "`#{self.class}` requires 4 colors, one for each vertex. #{color.length} were given." end @color = color # converted above # A single Color (not a per-vertex Set) is uniform across all four # vertices, so it takes the compact `draw_quad_uniform` fast path. @_color_uniform = !color.is_a?(Color::Set) @_cc = nil # invalidate cached color components end |
#contains?(x, y) ⇒ Boolean
Check if the quad contains the given point (inside the rendered fill). Ray-cast over the four vertices, so concave quads hit-test against what's actually drawn — unlike the old area method, which only held for convex quads. Self-intersecting (bow-tie) quads aren't fully supported by the fill renderer, so the test can diverge from the drawn pixels there.
135 136 137 138 |
# File 'lib/ruby2d/quad.rb', line 135 def contains?(x, y) x, y = _unrotate(x, y) _point_in_polygon?([@x1, @y1, @x2, @y2, @x3, @y3, @x4, @y4], x, y) end |
#height ⇒ Object
58 59 60 61 |
# File 'lib/ruby2d/quad.rb', line 58 def height ys = [@y1, @y2, @y3, @y4] ys.max - ys.min end |
#rx ⇒ Object
Get the rotation center x coordinate
141 142 143 |
# File 'lib/ruby2d/quad.rb', line 141 def rx @_user_rx.nil? ? (@x1 + @x2 + @x3 + @x4) / 4.0 : @_user_rx end |
#rx=(val) ⇒ Object
Set the rotation center x coordinate
151 152 153 |
# File 'lib/ruby2d/quad.rb', line 151 def rx=(val) @_user_rx = val end |
#ry ⇒ Object
Get the rotation center y coordinate
146 147 148 |
# File 'lib/ruby2d/quad.rb', line 146 def ry @_user_ry.nil? ? (@y1 + @y2 + @y3 + @y4) / 4.0 : @_user_ry end |
#ry=(val) ⇒ Object
Set the rotation center y coordinate
156 157 158 |
# File 'lib/ruby2d/quad.rb', line 156 def ry=(val) @_user_ry = val end |
#width ⇒ Object
Bounding-box width and height (extent across the four vertices)
53 54 55 56 |
# File 'lib/ruby2d/quad.rb', line 53 def width xs = [@x1, @x2, @x3, @x4] xs.max - xs.min end |
#x ⇒ Object
The x coordinate of the centroid (average of the four vertex x's)
23 24 25 |
# File 'lib/ruby2d/quad.rb', line 23 def x (@x1 + @x2 + @x3 + @x4) / 4.0 end |
#x=(new_x) ⇒ Object
Set the x coordinate of the centroid, translating all vertices
33 34 35 36 37 38 39 40 |
# File 'lib/ruby2d/quad.rb', line 33 def x=(new_x) _require_numeric_position(:x, new_x) dx = new_x - x @x1 += dx @x2 += dx @x3 += dx @x4 += dx end |
#y ⇒ Object
The y coordinate of the centroid (average of the four vertex y's)
28 29 30 |
# File 'lib/ruby2d/quad.rb', line 28 def y (@y1 + @y2 + @y3 + @y4) / 4.0 end |
#y=(new_y) ⇒ Object
Set the y coordinate of the centroid, translating all vertices
43 44 45 46 47 48 49 50 |
# File 'lib/ruby2d/quad.rb', line 43 def y=(new_y) _require_numeric_position(:y, new_y) dy = new_y - y @y1 += dy @y2 += dy @y3 += dy @y4 += dy end |