Class: Processing::Shape
- Inherits:
-
Object
- Object
- Processing::Shape
- Defined in:
- lib/processing/shape.rb
Overview
Shape object.
Instance Method Summary collapse
-
#addChild(child, index = -1)) ⇒ nil
Adds a new child shape.
-
#beginContour ⇒ nil
Starts a new contour definition.
-
#beginShape(type = nil) ⇒ nil
Starts shape data definition.
-
#bezierVertex(x2, y2, x3, y3, x4, y4) ⇒ nil
Append bezier vertex for shape polygon.
-
#curveVertex(x, y) ⇒ nil
Append curve vertex for shape polygon.
-
#endContour ⇒ nil
Ends contour definition.
-
#endShape(close = nil) ⇒ nil
Ends shape data definition.
-
#fill(*args) ⇒ nil
Sets fill color.
-
#getChild(index) ⇒ nil
Returns a child shape at the index position.
-
#getChildCount ⇒ Integer
Returns the number of children.
-
#getVertex(index) ⇒ Vector
Returns the vertex at the index position.
-
#getVertexCount ⇒ Integer
Returns the total number of vertices.
-
#height ⇒ Numeric
(also: #h)
Gets height of shape.
-
#isVisible ⇒ Boolean
(also: #visible?)
Returns whether the shape is visible or not.
-
#quadraticVertex(cx, cy, x3, y3) ⇒ nil
Append quadratic vertex for shape polygon.
-
#resetMatrix ⇒ nil
Reset the transformation matrix.
-
#rotate(angle) ⇒ nil
Applies rotation matrix to the shape.
-
#rotateX(angle) ⇒ nil
Applies rotation around the x-axis to the shape.
-
#rotateY(angle) ⇒ nil
Applies rotation around the y-axis to the shape.
-
#rotateZ(angle) ⇒ nil
Applies rotation around the z-axis to the shape.
-
#scale(x, y = nil, z = 1) ⇒ nil
Applies scale matrix to the shape.
-
#setFill(*args) ⇒ nil
Sets the fill color.
-
#setVertex(index, point) ⇒ nil
Sets the vertex at the index position.
-
#setVisible(visible) ⇒ nil
Sets whether to display the shape or not.
-
#translate(x, y, z = 0) ⇒ nil
Applies translation matrix to the shape.
-
#vertex(x, y, u = nil, v = nil) ⇒ nil
Append vertex for shape polygon.
-
#width ⇒ Numeric
(also: #w)
Gets width of shape.
Instance Method Details
#addChild(child, index = -1)) ⇒ nil
Adds a new child shape.
352 353 354 355 356 357 358 359 360 |
# File 'lib/processing/shape.rb', line 352 def addChild(child, index = -1) return unless @children if index < 0 @children.push child else @children.insert index, child end nil end |
#beginContour ⇒ nil
Starts a new contour definition.
109 110 111 112 113 |
# File 'lib/processing/shape.rb', line 109 def beginContour() raise "beginContour() must be called after beginShape()" unless drawingShape__ @contourPoints, @contourColors, @contourTexCoords = [], [], [] nil end |
#beginShape(type = nil) ⇒ nil
Starts shape data definition.
73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/processing/shape.rb', line 73 def beginShape(type = nil) raise "beginShape() cannot be called twice" if drawingShape__ @type = type @points ||= [] @curvePoints = [] @colors ||= [] @texcoords ||= [] @close = nil @contours ||= [] clearCache__ nil end |
#bezierVertex(x2, y2, x3, y3, x4, y4) ⇒ nil
Append bezier vertex for shape polygon.
194 195 196 197 198 199 200 201 202 |
# File 'lib/processing/shape.rb', line 194 def bezierVertex(x2, y2, x3, y3, x4, y4) raise "bezierVertex() must be called after beginShape()" unless drawingShape__ x1, y1 = @points[-2, 2] raise "vertex() is required before calling bezierVertex()" unless x1 && y1 Rays::Polygon.bezier(x1, y1, x2, y2, x3, y3, x4, y4) .first.to_a.tap {|a| a.shift} .each {|p| vertex p.x, p.y} nil end |
#curveVertex(x, y) ⇒ nil
Append curve vertex for shape polygon.
173 174 175 176 177 178 179 180 181 182 |
# File 'lib/processing/shape.rb', line 173 def curveVertex(x, y) raise "curveVertex() must be called after beginShape()" unless drawingShape__ @curvePoints << x << y if @curvePoints.size >= 8 Rays::Polygon.curve(*@curvePoints[-8, 8]) .first.to_a.tap {|a| a.shift if @curvePoints.size > 8} .each {|p| vertex p.x, p.y} end nil end |
#endContour ⇒ nil
Ends contour definition.
121 122 123 124 125 126 127 128 |
# File 'lib/processing/shape.rb', line 121 def endContour() raise "endContour() must be called after beginContour()" unless drawingContour__ @contours << Rays::Polyline.new( *@contourPoints, colors: @contourColors, texcoords: @contourTexCoords, loop: true, hole: true) @contoursPoints = @contoursColors = @contoursTexCoords = nil nil end |
#endShape(close = nil) ⇒ nil
Ends shape data definition.
92 93 94 95 96 97 98 99 100 101 |
# File 'lib/processing/shape.rb', line 92 def endShape(close = nil) raise "endShape() must be called after beginShape()" unless drawingShape__ @close = close == GraphicsContext::CLOSE || @contours.size > 0 if @close && @curvePoints.size >= 8 x, y = @curvePoints[0, 2] 2.times {curveVertex x, y} end @curvePoints = nil nil end |
#fill(gray) ⇒ nil #fill(gray, alpha) ⇒ nil #fill(r, g, b) ⇒ nil #fill(r, g, b, alpha) ⇒ nil
Sets fill color.
252 253 254 255 |
# File 'lib/processing/shape.rb', line 252 def fill(*args) @fill = @context.rawColor__(*args) nil end |
#getChild(index) ⇒ nil
Returns a child shape at the index position.
370 371 372 |
# File 'lib/processing/shape.rb', line 370 def getChild(index) @children&.[](index) end |
#getChildCount ⇒ Integer
Returns the number of children.
380 381 382 |
# File 'lib/processing/shape.rb', line 380 def getChildCount() @children&.size || 0 end |
#getVertex(index) ⇒ Vector
Returns the vertex at the index position.
285 286 287 288 289 290 |
# File 'lib/processing/shape.rb', line 285 def getVertex(index) return nil unless @points point = @points[index * 2, 2] return nil unless point&.size == 2 @context.createVector(*point) end |
#getVertexCount ⇒ Integer
Returns the total number of vertices.
298 299 300 301 |
# File 'lib/processing/shape.rb', line 298 def getVertexCount() return 0 unless @points @points.size / 2 end |
#height ⇒ Numeric Also known as: h
Gets height of shape.
36 37 38 39 |
# File 'lib/processing/shape.rb', line 36 def height() polygon = getInternal__ or return 0 (@bounds ||= polygon.bounds).height end |
#isVisible ⇒ Boolean Also known as: visible?
Returns whether the shape is visible or not.
50 51 52 |
# File 'lib/processing/shape.rb', line 50 def isVisible() @visible end |
#quadraticVertex(cx, cy, x3, y3) ⇒ nil
Append quadratic vertex for shape polygon.
214 215 216 217 218 219 220 221 222 |
# File 'lib/processing/shape.rb', line 214 def quadraticVertex(cx, cy, x3, y3) x1, y1 = @points[-2, 2] raise "vertex() is required before calling quadraticVertex()" unless x1 && y1 bezierVertex( x1 + (cx - x1) * 2.0 / 3.0, y1 + (cy - y1) * 2.0 / 3.0, x3 + (cx - x3) * 2.0 / 3.0, y3 + (cy - y3) * 2.0 / 3.0, x3, y3) nil end |
#resetMatrix ⇒ nil
Reset the transformation matrix.
480 481 482 483 |
# File 'lib/processing/shape.rb', line 480 def resetMatrix() @matrix = nil nil end |
#rotate(angle) ⇒ nil
Applies rotation matrix to the shape.
430 431 432 433 |
# File 'lib/processing/shape.rb', line 430 def rotate(angle) matrix__.rotate! @context.toDegrees__(angle) nil end |
#rotateX(angle) ⇒ nil
Applies rotation around the x-axis to the shape.
443 444 445 446 |
# File 'lib/processing/shape.rb', line 443 def rotateX(angle) matrix__.rotate! @context.toDegrees__(angle), 1, 0, 0 nil end |
#rotateY(angle) ⇒ nil
Applies rotation around the y-axis to the shape.
456 457 458 459 |
# File 'lib/processing/shape.rb', line 456 def rotateY(angle) matrix__.rotate! @context.toDegrees__(angle), 0, 1, 0 nil end |
#rotateZ(angle) ⇒ nil
Applies rotation around the z-axis to the shape.
469 470 471 472 |
# File 'lib/processing/shape.rb', line 469 def rotateZ(angle) matrix__.rotate! @context.toDegrees__(angle), 0, 0, 1 nil end |
#scale(s) ⇒ nil #scale(x, y) ⇒ nil #scale(x, y, z) ⇒ nil
Applies scale matrix to the shape.
417 418 419 420 |
# File 'lib/processing/shape.rb', line 417 def scale(x, y = nil, z = 1) matrix__.scale! x, (y || x), z nil end |
#fill(gray) ⇒ nil #fill(gray, alpha) ⇒ nil #fill(r, g, b) ⇒ nil #fill(r, g, b, alpha) ⇒ nil
Sets the fill color.
320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 |
# File 'lib/processing/shape.rb', line 320 def setFill(*args) color = @context.rawColor__(*args) count = getVertexCount if count > 0 if @colors @colors.fill color else @colors = [color] * count end clearCache__ elsif @polygon @polygon = @polygon.transform do |polylines| polylines.map {|pl| pl.with colors: pl.points.map {color}} end end nil end |
#setVertex(index, x, y) ⇒ nil #setVertex(index, vec) ⇒ nil
Sets the vertex at the index position.
271 272 273 274 275 |
# File 'lib/processing/shape.rb', line 271 def setVertex(index, point) return nil unless @points && @points[index * 2, 2]&.size == 2 @points[index * 2, 2] = [point.x, point.y] nil end |
#setVisible(visible) ⇒ nil
Sets whether to display the shape or not.
62 63 64 65 |
# File 'lib/processing/shape.rb', line 62 def setVisible(visible) @visible = !!visible nil end |
#translate(x, y) ⇒ nil #translate(x, y, z) ⇒ nil
Applies translation matrix to the shape.
397 398 399 400 |
# File 'lib/processing/shape.rb', line 397 def translate(x, y, z = 0) matrix__.translate! x, y, z nil end |
#vertex(x, y) ⇒ nil #vertex(x, y, u, v) ⇒ nil
Append vertex for shape polygon.
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/processing/shape.rb', line 145 def vertex(x, y, u = nil, v = nil) raise "vertex() must be called after beginShape()" unless drawingShape__ raise "Either 'u' or 'v' is missing" if (u == nil) != (v == nil) u ||= x v ||= y color = @fill || @context.getFill__ if drawingContour__ @contourPoints << x << y @contourColors << color @contourTexCoords << u << v else @points << x << y @colors << color @texcoords << u << v end nil end |
#width ⇒ Numeric Also known as: w
Gets width of shape.
25 26 27 28 |
# File 'lib/processing/shape.rb', line 25 def width() polygon = getInternal__ or return 0 (@bounds ||= polygon.bounds).width end |