Class: Ruby2D::Triangle

Inherits:
Object
  • Object
show all
Includes:
Renderable
Defined in:
lib/ruby2d/triangle.rb

Overview

A triangle

Constant Summary

Constants included from Interactive

Interactive::OBJECT_EVENTS, Interactive::OBJECT_EVENT_FILTER_PREDICATES

Instance Attribute Summary collapse

Attributes included from Renderable

#padding_bottom, #padding_left, #padding_right, #padding_top, #visible, #x_align, #y_align, #z

Class Method Summary collapse

Instance Method Summary collapse

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: 50, y1: 0, x2: 100, y2: 100, x3: 0, y3: 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) ⇒ Triangle

Create a triangle. Specify vertices via points: or via x1:/y1:/... points: takes precedence if both are given.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/ruby2d/triangle.rb', line 57

def initialize(x1: 50, y1: 0, x2: 100, y2: 100, x3: 0, y3: 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
    Triangle.send(:validate_points, points)
    x1, y1 = points[0]
    x2, y2 = points[1]
    x3, y3 = points[2]
  end
  @x1 = x1
  @y1 = y1
  @x2 = x2
  @y2 = y2
  @x3 = x3
  @y3 = y3
  @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

#colorObject

Returns the value of attribute color.



13
14
15
# File 'lib/ruby2d/triangle.rb', line 13

def color
  @color
end

#fillObject

Returns the value of attribute fill.



8
9
10
# File 'lib/ruby2d/triangle.rb', line 8

def fill
  @fill
end

#rotateObject

Returns the value of attribute rotate.



8
9
10
# File 'lib/ruby2d/triangle.rb', line 8

def rotate
  @rotate
end

#stroke_colorObject Also known as: stroke_colour

Returns the value of attribute stroke_color.



13
14
15
# File 'lib/ruby2d/triangle.rb', line 13

def stroke_color
  @stroke_color
end

#stroke_widthObject

Returns the value of attribute stroke_width.



8
9
10
# File 'lib/ruby2d/triangle.rb', line 8

def stroke_width
  @stroke_width
end

#x1Object

Returns the value of attribute x1.



8
9
10
# File 'lib/ruby2d/triangle.rb', line 8

def x1
  @x1
end

#x2Object

Returns the value of attribute x2.



8
9
10
# File 'lib/ruby2d/triangle.rb', line 8

def x2
  @x2
end

#x3Object

Returns the value of attribute x3.



8
9
10
# File 'lib/ruby2d/triangle.rb', line 8

def x3
  @x3
end

#y1Object

Returns the value of attribute y1.



8
9
10
# File 'lib/ruby2d/triangle.rb', line 8

def y1
  @y1
end

#y2Object

Returns the value of attribute y2.



8
9
10
# File 'lib/ruby2d/triangle.rb', line 8

def y2
  @y2
end

#y3Object

Returns the value of attribute y3.



8
9
10
# File 'lib/ruby2d/triangle.rb', line 8

def y3
  @y3
end

Class Method Details

.render(x1: 50, y1: 0, x2: 100, y2: 100, x3: 0, y3: 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 triangle without creating an instance



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/ruby2d/triangle.rb', line 146

def self.render(x1: 50, y1: 0, x2: 100, y2: 100, x3: 0, y3: 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)
  Window.render_ready_check
  if points
    validate_points(points)
    x1, y1 = points[0]
    x2, y2 = points[1]
    x3, y3 = points[2]
  end
  fill_input = color || colour
  # Resolve the fill color once. A single Color (not a per-vertex Set) with
  # scalar opacity is uniform across all three vertices and draws via the
  # compact `draw_triangle_uniform`; otherwise build the per-vertex color
  # array from the already-resolved color (no second parse).
  # A per-vertex color flattens straight to floats, skipping the
  # `Color::Set` the general path builds — that allocates and parses one
  # `Color` per vertex on every call. `resolved` stays nil in that case
  # and the stroke below resolves for itself if it ends up needing one.
  # The `is_a?(Array)` guard is inline so the common single-color call
  # doesn't pay for a method call that would only decline.
  c = fill_input.is_a?(Array) ? Renderable.flatten_per_vertex(fill_input, 3, opacity) : nil
  if c
    resolved = nil
    uniform = false
  else
    resolved = Color.for_render(fill_input.nil? ? 'white' : fill_input)
    uniform = !resolved.is_a?(Color::Set) && !opacity.is_a?(Array)
    c = uniform ? nil : Renderable.flatten_resolved_color(resolved, 3, opacity, label: self)
  end

  # Rotation is inlined into locals (as in `render` below and Line#render):
  # a helper returning the rotated points would allocate an array per
  # rotated shape per frame — measured 2.9µs vs 1.8µs per quad on wasm.
  if rotate != 0
    cx = rx || ((x1 + x2 + x3) / 3.0)
    cy = ry || ((y1 + y2 + y3) / 3.0)
    rad = rotate * Math::PI / 180.0
    sa = Math.sin(rad)
    ca = Math.cos(rad)
    dx = x1 - cx; dy = y1 - cy
    x1 = dx * ca - dy * sa + cx; y1 = dx * sa + dy * ca + cy
    dx = x2 - cx; dy = y2 - cy
    x2 = dx * ca - dy * sa + cx; y2 = dx * sa + dy * ca + cy
    dx = x3 - cx; dy = y3 - cy
    x3 = dx * ca - dy * sa + cx; y3 = dx * sa + dy * ca + cy
  end

  if fill
    if uniform
      a = opacity || resolved.a
      Ext.draw_triangle_uniform(x1, y1, x2, y2, x3, y3, resolved.r, resolved.g, resolved.b, a)
    else
      c1r, c1g, c1b, c1a, c2r, c2g, c2b, c2a, c3r, c3g, c3b, c3a = c
      Ext.draw_triangle(
        x1, y1, c1r, c1g, c1b, c1a,
        x2, y2, c2r, c2g, c2b, c2a,
        x3, y3, c3r, c3g, c3b, c3a
      )
    end
  end

  # Resolve the stroke color only when actually stroking. A single stroke
  # color draws via the compact `stroke_triangle_uniform`; a per-vertex Set
  # keeps the splatting path. When no explicit stroke is given the stroke
  # reuses the already-resolved fill color. (An explicit stroke color is
  # still validated at stroke_width 0 below, matching the instance constructor.)
  if stroke_width > 0
    stroke_input = stroke_color || stroke_colour || fill_input
    sresolved = if !stroke_input.equal?(fill_input)
                  Color.for_render(stroke_input)
                else
                  resolved || Color.for_render(fill_input.nil? ? 'white' : fill_input)
                end
    if !sresolved.is_a?(Color::Set) && !opacity.is_a?(Array)
      sa = opacity || sresolved.a
      Ext.stroke_triangle_uniform(x1, y1, x2, y2, x3, y3, stroke_width,
                                  sresolved.r, sresolved.g, sresolved.b, sa)
    else
      s1r, s1g, s1b, s1a, s2r, s2g, s2b, s2a, s3r, s3g, s3b, s3a =
        Renderable.flatten_resolved_color(sresolved, 3, opacity, label: self)
      Ext.stroke_triangle(
        x1, y1, x2, y2, x3, y3, stroke_width,
        s1r, s1g, s1b, s1a,
        s2r, s2g, s2b, s2a,
        s3r, s3g, s3b, s3a
      )
    end
  elsif stroke_color || stroke_colour
    Renderable.flatten_color(stroke_color || stroke_colour, 3, opacity, label: self)
  end
end

Instance Method Details

#contains?(x, y) ⇒ Boolean

Check if the triangle contains the given point (inside the rendered fill)

Returns:

  • (Boolean)


120
121
122
123
# File 'lib/ruby2d/triangle.rb', line 120

def contains?(x, y)
  x, y = _unrotate(x, y)
  _point_in_polygon?([@x1, @y1, @x2, @y2, @x3, @y3], x, y)
end

#heightObject



50
51
52
53
# File 'lib/ruby2d/triangle.rb', line 50

def height
  ys = [@y1, @y2, @y3]
  ys.max - ys.min
end

#rxObject

Get the rotation center x coordinate



126
127
128
# File 'lib/ruby2d/triangle.rb', line 126

def rx
  @_user_rx.nil? ? x : @_user_rx
end

#rx=(val) ⇒ Object

Set the rotation center x coordinate



136
137
138
# File 'lib/ruby2d/triangle.rb', line 136

def rx=(val)
  @_user_rx = val
end

#ryObject

Get the rotation center y coordinate



131
132
133
# File 'lib/ruby2d/triangle.rb', line 131

def ry
  @_user_ry.nil? ? y : @_user_ry
end

#ry=(val) ⇒ Object

Set the rotation center y coordinate



141
142
143
# File 'lib/ruby2d/triangle.rb', line 141

def ry=(val)
  @_user_ry = val
end

#widthObject

Bounding-box width and height (extent across the three vertices)



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

def width
  xs = [@x1, @x2, @x3]
  xs.max - xs.min
end

#xObject

The x coordinate of the centroid



17
18
19
# File 'lib/ruby2d/triangle.rb', line 17

def x
  (@x1 + @x2 + @x3) / 3.0
end

#x=(new_x) ⇒ Object

Set the x coordinate of the centroid, translating all vertices



27
28
29
30
31
32
33
# File 'lib/ruby2d/triangle.rb', line 27

def x=(new_x)
  _require_numeric_position(:x, new_x)
  dx = new_x - x
  @x1 += dx
  @x2 += dx
  @x3 += dx
end

#yObject

The y coordinate of the centroid



22
23
24
# File 'lib/ruby2d/triangle.rb', line 22

def y
  (@y1 + @y2 + @y3) / 3.0
end

#y=(new_y) ⇒ Object

Set the y coordinate of the centroid, translating all vertices



36
37
38
39
40
41
42
# File 'lib/ruby2d/triangle.rb', line 36

def y=(new_y)
  _require_numeric_position(:y, new_y)
  dy = new_y - y
  @y1 += dy
  @y2 += dy
  @y3 += dy
end