Class: Processing::Shape

Inherits:
Object
  • Object
show all
Defined in:
lib/processing/shape.rb

Overview

Shape object.

Instance Method Summary collapse

Instance Method Details

#addChild(child, index = -1)) ⇒ nil

Adds a new child shape.

Parameters:

  • child (Shape)

    child shape

  • index (Integer) (defaults to: -1))

    insert position

Returns:

  • (nil)

    nil

See Also:



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

#beginContournil

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.

Parameters:

  • x (Numeric)

    x position of vertex

  • y (Numeric)

    y position of vertex

Returns:

  • (nil)

    nil

See Also:



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.

Parameters:

  • x (Numeric)

    x position of vertex

  • y (Numeric)

    y position of vertex

Returns:

  • (nil)

    nil

See Also:



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

#endContournil

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.

Parameters:

  • gray (Integer)

    gray value (0..255)

  • r (Integer)

    red value (0..255)

  • g (Integer)

    green value (0..255)

  • b (Integer)

    blue value (0..255)

  • alpha (Integer)

    alpha value (0..255)

Returns:

  • (nil)

    nil

See Also:



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.

Parameters:

  • index (Integer)

    child index

Returns:

  • (nil)

    nil

See Also:



451
452
453
# File 'lib/processing/shape.rb', line 451

def getChild(index)
  @children&.[](index)
end

#getChildCountInteger

Returns the number of children.

Returns:

  • (Integer)

    child count

See Also:



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.

Parameters:

  • index (Integer)

    the index fo the vertex

Returns:

  • (Vector)

    the vertex position

See Also:



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

#getVertexCountInteger

Returns the total number of vertices.

Returns:

  • (Integer)

    vertex count

See Also:



329
330
331
332
# File 'lib/processing/shape.rb', line 329

def getVertexCount()
  return 0 unless @points
  @points.size / 2
end

#heightNumeric Also known as: h

Gets height of shape.

Returns:

  • (Numeric)

    height of shape

See Also:



37
38
39
40
# File 'lib/processing/shape.rb', line 37

def height()
  polygon = getInternal__ or return 0
  (@bounds ||= polygon.bounds).height
end

#isVisibleBoolean Also known as: visible?

Returns whether the shape is visible or not.

Returns:

  • (Boolean)

    visible or not

See Also:



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.

Parameters:

  • x (Numeric)

    x position of vertex

  • y (Numeric)

    y position of vertex

Returns:

  • (nil)

    nil

See Also:



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

#resetMatrixnil

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.

Parameters:

  • angle (Numeric)

    angle for rotation

Returns:

  • (nil)

    nil

See Also:



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.

Parameters:

  • angle (Numeric)

    angle for rotation

Returns:

  • (nil)

    nil

See Also:



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.

Parameters:

  • angle (Numeric)

    angle for rotation

Returns:

  • (nil)

    nil

See Also:



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.

Parameters:

  • angle (Numeric)

    angle for rotation

Returns:

  • (nil)

    nil

See Also:



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.

Parameters:

  • s (Numeric)

    horizontal and vertical scale

  • x (Numeric)

    horizontal scale

  • y (Numeric) (defaults to: nil)

    vertical scale

  • z (Numeric) (defaults to: 1)

    depth scale

Returns:

  • (nil)

    nil

See Also:



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.

Parameters:

  • gray (Integer)

    gray value (0..255)

  • r (Integer)

    red value (0..255)

  • g (Integer)

    green value (0..255)

  • b (Integer)

    blue value (0..255)

  • alpha (Integer)

    alpha value (0..255)

Returns:

  • (nil)

    nil

See Also:



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.

Parameters:

  • gray (Integer)

    gray value (0..255)

  • r (Integer)

    red value (0..255)

  • g (Integer)

    green value (0..255)

  • b (Integer)

    blue value (0..255)

  • alpha (Integer)

    alpha value (0..255)

Returns:

  • (nil)

    nil

See Also:



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.

Parameters:

  • cap (ROUND, SQUARE, PROJECT)

    stroke cap

Returns:

  • (nil)

    nil



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.

Parameters:

  • join (MITER, BEVEL, ROUND)

    stroke join

Returns:

  • (nil)

    nil



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.

Parameters:

  • weight (Numeric)

    stroke weight

Returns:

  • (nil)

    nil



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.

Parameters:

  • index (Integer)

    the index fo the vertex

  • x (Numeric)

    x position of the vertex

  • y (Numeric)

    y position of the vertex

  • vec (Vector)

    position for the vertex

Returns:

  • (nil)

    nil

See Also:



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.

Parameters:

  • gray (Integer)

    gray value (0..255)

  • r (Integer)

    red value (0..255)

  • g (Integer)

    green value (0..255)

  • b (Integer)

    blue value (0..255)

  • alpha (Integer)

    alpha value (0..255)

Returns:

  • (nil)

    nil

See Also:



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.

Parameters:

  • x (Numeric)

    left/right translation

  • y (Numeric)

    up/down translation

  • z (Numeric) (defaults to: 0)

    forward/backward translation

Returns:

  • (nil)

    nil

See Also:



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.

Parameters:

  • x (Numeric)

    x position of vertex

  • y (Numeric)

    y position of vertex

  • u (Numeric) (defaults to: nil)

    u texture coordinate of vertex

  • v (Numeric) (defaults to: nil)

    v texture coordinate of vertex

Returns:

  • (nil)

    nil

See Also:



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

#widthNumeric Also known as: w

Gets width of shape.

Returns:

  • (Numeric)

    width of shape

See Also:



26
27
28
29
# File 'lib/processing/shape.rb', line 26

def width()
  polygon = getInternal__ or return 0
  (@bounds ||= polygon.bounds).width
end