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 for all vertices.
-
#setStroke(*args) ⇒ nil
Sets the stroke color.
-
#setStrokeCap(cap) ⇒ nil
Sets the stroke cap.
-
#setStrokeJoin(join) ⇒ nil
Sets the stroke join.
-
#setStrokeWeight(weight) ⇒ nil
Sets the stroke weight.
-
#setVertex(index, point) ⇒ nil
Sets the vertex at the index position.
-
#setVisible(visible) ⇒ nil
Sets whether to display the shape or not.
-
#stroke(*args) ⇒ nil
Sets stroke color.
-
#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.
433 434 435 436 437 438 439 440 441 |
# File 'lib/processing/shape.rb', line 433 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.
117 118 119 120 121 |
# File 'lib/processing/shape.rb', line 117 def beginContour() raise "beginContour() must be called after beginShape()" unless drawingShape__ @contourPoints, @contourColors, @contourTexCoords = [], [], [] nil end |
#beginShape(type = nil) ⇒ nil
Starts shape data definition.
74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/processing/shape.rb', line 74 def beginShape(type = nil) raise "beginShape() cannot be called twice" if drawingShape__ @fill = @stroke = @strokeWeight = @strokeCap = @strokeJoin = nil @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.
202 203 204 205 206 207 208 209 210 |
# File 'lib/processing/shape.rb', line 202 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.
181 182 183 184 185 186 187 188 189 190 |
# File 'lib/processing/shape.rb', line 181 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.
129 130 131 132 133 134 135 136 |
# File 'lib/processing/shape.rb', line 129 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.
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/processing/shape.rb', line 94 def endShape(close = nil) raise "endShape() must be called after beginShape()" unless drawingShape__ painter = @context.getPainter__ @fill ||= painter.fill @stroke ||= painter.stroke @strokeWeight ||= painter.stroke_width @strokeCap ||= painter.stroke_cap @strokeJoin ||= painter.stroke_join @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.
260 261 262 263 |
# File 'lib/processing/shape.rb', line 260 def fill(*args) @fill = @context.toRawColor__(*args) nil end |
#getChild(index) ⇒ nil
Returns a child shape at the index position.
451 452 453 |
# File 'lib/processing/shape.rb', line 451 def getChild(index) @children&.[](index) end |
#getChildCount ⇒ Integer
Returns the number of children.
461 462 463 |
# File 'lib/processing/shape.rb', line 461 def getChildCount() @children&.size || 0 end |
#getVertex(index) ⇒ Vector
Returns the vertex at the index position.
316 317 318 319 320 321 |
# File 'lib/processing/shape.rb', line 316 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.
329 330 331 332 |
# File 'lib/processing/shape.rb', line 329 def getVertexCount() return 0 unless @points @points.size / 2 end |
#height ⇒ Numeric Also known as: h
Gets height of shape.
37 38 39 40 |
# File 'lib/processing/shape.rb', line 37 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.
51 52 53 |
# File 'lib/processing/shape.rb', line 51 def isVisible() @visible end |
#quadraticVertex(cx, cy, x3, y3) ⇒ nil
Append quadratic vertex for shape polygon.
222 223 224 225 226 227 228 229 230 |
# File 'lib/processing/shape.rb', line 222 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.
561 562 563 564 |
# File 'lib/processing/shape.rb', line 561 def resetMatrix() @matrix = nil nil end |
#rotate(angle) ⇒ nil
Applies rotation matrix to the shape.
511 512 513 514 |
# File 'lib/processing/shape.rb', line 511 def rotate(angle) matrix__.rotate! @context.toDegrees__(angle) nil end |
#rotateX(angle) ⇒ nil
Applies rotation around the x-axis to the shape.
524 525 526 527 |
# File 'lib/processing/shape.rb', line 524 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.
537 538 539 540 |
# File 'lib/processing/shape.rb', line 537 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.
550 551 552 553 |
# File 'lib/processing/shape.rb', line 550 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.
498 499 500 501 |
# File 'lib/processing/shape.rb', line 498 def scale(x, y = nil, z = 1) matrix__.scale! x, (y || x), z nil end |
#setFill(gray) ⇒ nil #setFill(gray, alpha) ⇒ nil #setFill(r, g, b) ⇒ nil #setFill(r, g, b, alpha) ⇒ nil
Sets the fill color for all vertices.
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 |
# File 'lib/processing/shape.rb', line 351 def setFill(*args) fill(*args) count = getVertexCount if count > 0 if @colors @colors.fill @fill else @colors = [@fill] * count end clearCache__ elsif @polygon @polygon = @polygon.transform do |polylines| polylines.map {|pl| pl.with colors: pl.points.map {@fill}} end end nil end |
#setStroke(gray) ⇒ nil #setStroke(gray, alpha) ⇒ nil #setStroke(r, g, b) ⇒ nil #setStroke(r, g, b, alpha) ⇒ nil
Sets the stroke color.
386 387 388 389 |
# File 'lib/processing/shape.rb', line 386 def setStroke(*args) stroke(*args) nil end |
#setStrokeCap(cap) ⇒ nil
Sets the stroke cap.
408 409 410 411 |
# File 'lib/processing/shape.rb', line 408 def setStrokeCap(cap) @strokeCap = cap nil end |
#setStrokeJoin(join) ⇒ nil
Sets the stroke join.
419 420 421 422 |
# File 'lib/processing/shape.rb', line 419 def setStrokeJoin(join) @strokeJoin = join nil end |
#setStrokeWeight(weight) ⇒ nil
Sets the stroke weight.
397 398 399 400 |
# File 'lib/processing/shape.rb', line 397 def setStrokeWeight(weight) @strokeWeight = weight nil end |
#setVertex(index, x, y) ⇒ nil #setVertex(index, vec) ⇒ nil
Sets the vertex at the index position.
302 303 304 305 306 |
# File 'lib/processing/shape.rb', line 302 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.
63 64 65 66 |
# File 'lib/processing/shape.rb', line 63 def setVisible(visible) @visible = !!visible nil end |
#stroke(gray) ⇒ nil #stroke(gray, alpha) ⇒ nil #stroke(r, g, b) ⇒ nil #stroke(r, g, b, alpha) ⇒ nil
Sets stroke color.
283 284 285 286 |
# File 'lib/processing/shape.rb', line 283 def stroke(*args) @stroke = @context.toRawColor__(*args) nil end |
#translate(x, y) ⇒ nil #translate(x, y, z) ⇒ nil
Applies translation matrix to the shape.
478 479 480 481 |
# File 'lib/processing/shape.rb', line 478 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.
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/processing/shape.rb', line 153 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.getPainter__.fill 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.
26 27 28 29 |
# File 'lib/processing/shape.rb', line 26 def width() polygon = getInternal__ or return 0 (@bounds ||= polygon.bounds).width end |