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:



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

#beginContournil

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.

Parameters:

  • x (Numeric)

    x position of vertex

  • y (Numeric)

    y position of vertex

Returns:

  • (nil)

    nil

See Also:



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.

Parameters:

  • x (Numeric)

    x position of vertex

  • y (Numeric)

    y position of vertex

Returns:

  • (nil)

    nil

See Also:



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

#endContournil

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.

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:



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.

Parameters:

  • index (Integer)

    child index

Returns:

  • (nil)

    nil

See Also:



370
371
372
# File 'lib/processing/shape.rb', line 370

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

#getChildCountInteger

Returns the number of children.

Returns:

  • (Integer)

    child count

See Also:



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.

Parameters:

  • index (Integer)

    the index fo the vertex

Returns:

  • (Vector)

    the vertex position

See Also:



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

#getVertexCountInteger

Returns the total number of vertices.

Returns:

  • (Integer)

    vertex count

See Also:



298
299
300
301
# File 'lib/processing/shape.rb', line 298

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:



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

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:



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.

Parameters:

  • x (Numeric)

    x position of vertex

  • y (Numeric)

    y position of vertex

Returns:

  • (nil)

    nil

See Also:



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

#resetMatrixnil

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.

Parameters:

  • angle (Numeric)

    angle for rotation

Returns:

  • (nil)

    nil

See Also:



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.

Parameters:

  • angle (Numeric)

    angle for rotation

Returns:

  • (nil)

    nil

See Also:



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.

Parameters:

  • angle (Numeric)

    angle for rotation

Returns:

  • (nil)

    nil

See Also:



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.

Parameters:

  • angle (Numeric)

    angle for rotation

Returns:

  • (nil)

    nil

See Also:



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.

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:



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.

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:



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.

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:



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.

Parameters:

  • x (Numeric)

    left/right translation

  • y (Numeric)

    up/down translation

  • z (Numeric) (defaults to: 0)

    forward/backward translation

Returns:

  • (nil)

    nil

See Also:



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.

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:



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

#widthNumeric Also known as: w

Gets width of shape.

Returns:

  • (Numeric)

    width of shape

See Also:



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

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