Class: RubySketch::Processing1

Inherits:
Object
  • Object
show all
Extended by:
Starter
Includes:
Math
Defined in:
lib/rubysketch/processing.rb

Overview

Processing compatible API v1

Defined Under Namespace

Classes: Font

Constant Summary collapse

HALF_PI =
PI / 2
QUARTER_PI =
PI / 4
TWO_PI =
PI * 2
TAU =
PI * 2
RGB =

RGB mode for colorMode() function.

:RGB
HSB =

HSB mode for colorMode() function.

:HSB
RADIANS =

Radian mode for angleMode() function.

:RADIANS
DEGREES =

Degree mode for angleMode() function.

:DEGREES

Instance Method Summary collapse

Methods included from Starter

start

Instance Method Details

#abs(value) ⇒ Numeric

Returns the absolute number of the value.

Parameters:

  • value (Numeric)

    number

Returns:

  • (Numeric)

    absolute number



73
74
75
# File 'lib/rubysketch/processing.rb', line 73

def abs (value)
  value.abs
end

#angleMode(mode) ⇒ nil

Sets angle mode.

Parameters:

Returns:

  • (nil)

    nil



516
517
518
519
520
521
522
# File 'lib/rubysketch/processing.rb', line 516

def angleMode (mode)
  @angleScale__ = case mode
    when RADIANS then RAD2DEG__
    when DEGREES then 1.0
    else raise ArgumentError, "Invalid angle mode: #{mode}"
  end
end

#arc(x, y, w, h, start, stop) ⇒ nil

Draws an arc.

Parameters:

  • x (Numeric)

    horizontal position

  • y (Numeric)

    vertical position

  • w (Numeric)

    width of the shape

  • h (Numeric)

    height of the shape

  • start (Numeric)

    angle to start the arc

  • stop (Numeric)

    angle to stop the arc

Returns:

  • (nil)

    nil



729
730
731
732
733
734
# File 'lib/rubysketch/processing.rb', line 729

def arc (x, y, w, h, start, stop)
  start = to_angle__ start
  stop  = to_angle__ stop
  @painter__.ellipse x - w / 2, y - h / 2, w, h, from: start, to: stop
  nil
end

#background(str) ⇒ nil #background(str, alpha) ⇒ nil #background(gray) ⇒ nil #background(gray, alpha) ⇒ nil #background(r, g, b) ⇒ nil #background(r, g, b, alpha) ⇒ nil

Clears screen.

Parameters:

  • str (String)

    color code like ‘#00AAFF’

  • 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



547
548
549
550
# File 'lib/rubysketch/processing.rb', line 547

def background (*args)
  @painter__.background(*to_rgba__(*args))
  nil
end

#ceil(value) ⇒ Numeric

Returns the closest integer number greater than or equal to the value.

Parameters:

  • value (Numeric)

    number

Returns:

  • (Numeric)

    rounded up number



83
84
85
# File 'lib/rubysketch/processing.rb', line 83

def ceil (value)
  value.ceil
end

#circle(x, y, extent) ⇒ nil

Draws a circle.

Parameters:

  • x (Numeric)

    horizontal position

  • y (Numeric)

    vertical position

  • extent (Numeric)

    width and height of the shape

Returns:

  • (nil)

    nil



714
715
716
# File 'lib/rubysketch/processing.rb', line 714

def circle (x, y, extent)
  ellipse x, y, extent, extent
end

#colorMode(mode) ⇒ nil #colorMode(mode, max) ⇒ nil #colorMode(mode, max1, max2, max3) ⇒ nil #colorMode(mode, max1, max2, max3, maxA) ⇒ nil

Sets color mode and max color values.

Parameters:

  • mode (RGB, HSB)

    RGB or HSB

  • max (Numeric)

    max values for all color values

  • max1 (Numeric)

    max value for red or hue

  • max2 (Numeric)

    max value for green or saturation

  • max3 (Numeric)

    max value for blue or brightness

  • maxA (Numeric)

    max value for alpha

Returns:

  • (nil)

    nil

Raises:

  • (ArgumentError)


469
470
471
472
473
474
475
476
477
478
479
# File 'lib/rubysketch/processing.rb', line 469

def colorMode (mode, *maxes)
  raise ArgumentError, "Invalid color mode: #{mode}" unless [RGB, HSB].include?(mode)
  raise ArgumentError unless [0, 1, 3, 4].include?(maxes.size)

  @hsbColor__ = mode.upcase == HSB
  case maxes.size
    when 1    then @colorMaxes__                 = [maxes.first.to_f] * 4
    when 3, 4 then @colorMaxes__[0...maxes.size] = maxes.map &:to_f
  end
  nil
end

#constrain(value, min, max) ⇒ Numeric

Constrains the number between min..max.

Parameters:

  • value (Numeric)

    number to be constrained

  • min (Numeric)

    lower bound of the range

  • max (Numeric)

    upper bound of the range

Returns:

  • (Numeric)

    constrained number



256
257
258
# File 'lib/rubysketch/processing.rb', line 256

def constrain (value, min, max)
  value < min ? min : (value > max ? max : value)
end

#degrees(radian) ⇒ Numeric

Converts radian to degree.

Parameters:

  • radian (Numeric)

    radian to convert

Returns:

  • (Numeric)

    degree



276
277
278
# File 'lib/rubysketch/processing.rb', line 276

def degrees (radian)
  radian * RAD2DEG__
end

#displayDensityNumeric

Returns pixel density

Returns:

  • (Numeric)

    pixel density



417
418
419
# File 'lib/rubysketch/processing.rb', line 417

def displayDensity ()
  @painter__.pixel_density
end

#dist(x1, y1, x2, y2) ⇒ Numeric #dist(x1, y1, z1, x2, y2, z2) ⇒ Numeric

Returns distance between 2 points.

Parameters:

  • x1 (Numeric)

    x of first point

  • y1 (Numeric)

    y of first point

  • z1 (Numeric)

    z of first point

  • x2 (Numeric)

    x of second point

  • y2 (Numeric)

    y of second point

  • z2 (Numeric)

    z of second point

Returns:

  • (Numeric)

    distance between 2 points



162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/rubysketch/processing.rb', line 162

def dist (*args)
  case args.size
    when 4
      x1, y1, x2, y2 = *args
      xx, yy = x2 - x1, y2 - y1
      sqrt xx * xx + yy * yy
    when 3
      x1, y1, z1, x2, y2, z2 = *args
      xx, yy, zz = x2 - x1, y2 - y1, z2 - z1
      sqrt xx * xx + yy * yy + zz * zz
    else raise ArgumentError
  end
end

#draw(&block) ⇒ Object



295
296
297
298
# File 'lib/rubysketch/processing.rb', line 295

def draw (&block)
  @drawBlock__ = block if block
  nil
end

#ellipse(x, y, w, h = w) ⇒ nil

Draws a ellipse.

Parameters:

  • x (Numeric)

    horizontal position

  • y (Numeric)

    vertical position

  • w (Numeric)

    width of the shape

  • h (Numeric) (defaults to: w)

    height of the shape

Returns:

  • (nil)

    nil



701
702
703
704
# File 'lib/rubysketch/processing.rb', line 701

def ellipse (x, y, w, h = w)
  @painter__.ellipse (x - w / 2.0), (y - h / 2.0), w, h
  nil
end

#fill(rgb) ⇒ nil #fill(rgb, alpha) ⇒ nil #fill(gray) ⇒ nil #fill(gray, alpha) ⇒ nil #fill(r, g, b) ⇒ nil #fill(r, g, b, alpha) ⇒ nil

Sets fill color.

Parameters:

  • rgb (String)

    color code like ‘#00AAFF’

  • 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



570
571
572
573
# File 'lib/rubysketch/processing.rb', line 570

def fill (*args)
  @painter__.fill(*to_rgba__(*args))
  nil
end

#floor(value) ⇒ Numeric

Returns the closest integer number less than or equal to the value.

Parameters:

  • value (Numeric)

    number

Returns:

  • (Numeric)

    rounded down number



93
94
95
# File 'lib/rubysketch/processing.rb', line 93

def floor (value)
  value.floor
end

#frameCountInteger

Returns number of frames since program started.

Returns:

  • (Integer)

    total number of frames



401
402
403
# File 'lib/rubysketch/processing.rb', line 401

def frameCount ()
  @frameCount__
end

#frameRateFloat

Returns number of frames per second.

Returns:

  • (Float)

    frames per second



409
410
411
# File 'lib/rubysketch/processing.rb', line 409

def frameRate ()
  @window__.event.fps
end

#heightObject



385
386
387
# File 'lib/rubysketch/processing.rb', line 385

def height ()
  @window__.canvas.height
end

#key(&block) ⇒ Object



300
301
302
303
# File 'lib/rubysketch/processing.rb', line 300

def key (&block)
  @window__.key = block
  nil
end

#lerp(start, stop, amount) ⇒ Numeric

Returns the interpolated number between range start..stop.

Parameters:

  • start (Numeric)

    lower bound of the range

  • stop (Numeric)

    upper bound of the range

  • amount (Numeric)

    amount to interpolate

Returns:

  • (Numeric)

    interporated number



196
197
198
# File 'lib/rubysketch/processing.rb', line 196

def lerp (start, stop, amount)
  start + (stop - start) * amount
end

#line(x1, y1, x2, y2) ⇒ nil

Draws a line.

Parameters:

  • x1 (Numeric)

    horizontal position for first point

  • y1 (Numeric)

    vertical position for first point

  • x2 (Numeric)

    horizontal position for second point

  • y2 (Numeric)

    vertical position for second point

Returns:

  • (nil)

    nil



659
660
661
662
# File 'lib/rubysketch/processing.rb', line 659

def line (x1, y1, x2, y2)
  @painter__.line x1, y1, x2, y2
  nil
end

#mag(x, y) ⇒ Numeric #mag(x, y, z) ⇒ Numeric

Returns the magnitude (or length) of a vector.

Parameters:

  • x (Numeric)

    x of point

  • y (Numeric)

    y of point

  • z (Numeric)

    z of point

Returns:

  • (Numeric)

    magnitude



139
140
141
142
143
144
145
146
# File 'lib/rubysketch/processing.rb', line 139

def mag (*args)
  x, y, z = *args
  case args.size
    when 2 then sqrt x * x + y * y
    when 3 then sqrt x * x + y * y + z * z
    else raise ArgumentError
  end
end

#map(value, start1, stop1, start2, stop2) ⇒ Numeric

Maps a number from range start1..stop1 to range start2..stop2.

Parameters:

  • value (Numeric)

    number to be mapped

  • start1 (Numeric)

    lower bound of the range1

  • stop1 (Numeric)

    upper bound of the range1

  • start2 (Numeric)

    lower bound of the range2

  • stop2 (Numeric)

    upper bound of the range2

Returns:

  • (Numeric)

    mapped number



210
211
212
# File 'lib/rubysketch/processing.rb', line 210

def map (value, start1, stop1, start2, stop2)
  lerp start2, stop2, norm(value, start1, stop1)
end

#max(a, b) ⇒ Numeric #max(a, b, c) ⇒ Numeric #max(array) ⇒ Numeric

Returns maximum value.

Parameters:

  • a (Numeric)

    value to compare

  • b (Numeric)

    value to compare

  • c (Numeric)

    value to compare

  • array (Numeric)

    values to compare

Returns:

  • (Numeric)

    maximum value



244
245
246
# File 'lib/rubysketch/processing.rb', line 244

def max (*args)
  args.flatten.max
end

#min(a, b) ⇒ Numeric #min(a, b, c) ⇒ Numeric #min(array) ⇒ Numeric

Returns minimum value.

Parameters:

  • a (Numeric)

    value to compare

  • b (Numeric)

    value to compare

  • c (Numeric)

    value to compare

  • array (Numeric)

    values to compare

Returns:

  • (Numeric)

    minimum value



227
228
229
# File 'lib/rubysketch/processing.rb', line 227

def min (*args)
  args.flatten.min
end

#mouseDragged(&block) ⇒ Object



352
353
354
355
# File 'lib/rubysketch/processing.rb', line 352

def mouseDragged (&block)
  @mouseDraggedBlock__ = block if block
  nil
end

#mouseMoved(&block) ⇒ Object



339
340
341
342
# File 'lib/rubysketch/processing.rb', line 339

def mouseMoved (&block)
  @mouseMovedBlock__ = block if block
  nil
end

#mousePressed(&block) ⇒ Object



313
314
315
316
# File 'lib/rubysketch/processing.rb', line 313

def mousePressed (&block)
  @mousePressedBlock__ = block if block
  @mousePressed__
end

#mouseReleased(&block) ⇒ Object



326
327
328
329
# File 'lib/rubysketch/processing.rb', line 326

def mouseReleased (&block)
  @mouseReleasedBlock__ = block if block
  nil
end

#mouseXNumeric

Returns mouse x position

Returns:

  • (Numeric)

    horizontal position of mouse



425
426
427
# File 'lib/rubysketch/processing.rb', line 425

def mouseX ()
  @mouseX__
end

#mouseYNumeric

Returns mouse y position

Returns:

  • (Numeric)

    vertical position of mouse



433
434
435
# File 'lib/rubysketch/processing.rb', line 433

def mouseY ()
  @mouseY__
end

#noFillnil

Disables filling.

Returns:

  • (nil)

    nil



613
614
615
616
# File 'lib/rubysketch/processing.rb', line 613

def noFill ()
  @painter__.fill nil
  nil
end

#noise(x) ⇒ Numeric #noise(x, y) ⇒ Numeric

Returns the perlin noise value.

Parameters:

  • x (Numeric)

    horizontal point in noise space

  • y (Numeric) (defaults to: 0)

    vertical point in noise space

Returns:

  • (Numeric)

    noise value (0.0..1.0)



840
841
842
# File 'lib/rubysketch/processing.rb', line 840

def noise (x, y = 0)
  Rays.perlin(x, y) / 2.0 + 1.0
end

#norm(value, start, stop) ⇒ Numeric

Normalize the value from range start..stop into 0..1.

Parameters:

  • value (Numeric)

    number to be normalized

  • start (Numeric)

    lower bound of the range

  • stop (Numeric)

    upper bound of the range

Returns:

  • (Numeric)

    normalized value between 0..1



184
185
186
# File 'lib/rubysketch/processing.rb', line 184

def norm (value, start, stop)
  (value.to_f - start.to_f) / (stop.to_f - start.to_f)
end

#noStrokenil

Disables drawing stroke.

Returns:

  • (nil)

    nil



622
623
624
625
# File 'lib/rubysketch/processing.rb', line 622

def noStroke ()
  @painter__.stroke nil
  nil
end

#pmouseXNumeric

Returns mouse x position in previous frame

Returns:

  • (Numeric)

    horizontal position of mouse



441
442
443
# File 'lib/rubysketch/processing.rb', line 441

def pmouseX ()
  @mousePrevX__
end

#pmouseYNumeric

Returns mouse y position in previous frame

Returns:

  • (Numeric)

    vertical position of mouse



449
450
451
# File 'lib/rubysketch/processing.rb', line 449

def pmouseY ()
  @mousePrevY__
end

#point(x, y) ⇒ nil

Draws a point.

Parameters:

  • x (Numeric)

    horizontal position

  • y (Numeric)

    vertical position

Returns:

  • (nil)

    nil



743
744
745
746
# File 'lib/rubysketch/processing.rb', line 743

def point (x, y)
  @painter__.rect x - 0.5, y - 0.5, 1, 1
  nil
end

#popMatrixnil

Pops the current transformation matrix from stack.

Returns:

  • (nil)

    nil



816
817
818
819
# File 'lib/rubysketch/processing.rb', line 816

def popMatrix ()
  @painter__.pop_matrix
  nil
end

#pow(value, exponent) ⇒ Numeric

Returns value raised to the power of exponent.

Parameters:

  • value (Numeric)

    base number

  • exponent (Numeric)

    exponent number

Returns:

  • (Numeric)

    value ** exponent



114
115
116
# File 'lib/rubysketch/processing.rb', line 114

def pow (value, exponent)
  value ** exponent
end

#pushMatrixnil

Pushes the current transformation matrix to stack.

Returns:

  • (nil)

    nil



807
808
809
810
# File 'lib/rubysketch/processing.rb', line 807

def pushMatrix ()
  @painter__.push_matrix
  nil
end

#radians(degree) ⇒ Numeric

Converts degree to radian.

Parameters:

  • degree (Numeric)

    degree to convert

Returns:

  • (Numeric)

    radian



266
267
268
# File 'lib/rubysketch/processing.rb', line 266

def radians (degree)
  degree * DEG2RAD__
end

#rect(x, y, w, h) ⇒ nil #rect(x, y, w, h, r) ⇒ nil #rect(x, y, w, h, tl, tr, br, bl) ⇒ nil

Draws a rectangle.

Parameters:

  • x (Numeric)

    horizontal position

  • y (Numeric)

    vertical position

  • w (Numeric)

    width of the shape

  • h (Numeric)

    height of the shape

  • r (Numeric)

    radius for all corners

  • tl (Numeric)

    radius for top-left corner

  • tr (Numeric)

    radius for top-right corner

  • br (Numeric)

    radius for bottom-right corner

  • bl (Numeric)

    radius for bottom-left corner

Returns:

  • (nil)

    nil



682
683
684
685
686
687
688
689
690
# File 'lib/rubysketch/processing.rb', line 682

def rect (x, y, w, h, *args)
  case args.size
    when 0 then @painter__.rect x, y, w, h
    when 1 then @painter__.rect x, y, w, h, round: args[0]
    when 4 then @painter__.rect x, y, w, h, lt: args[0], rt: args[1], rb: args[2], lb: args[3]
    else raise ArgumentError # ToDo: refine error message
  end
  nil
end

#resetMatrixnil

Reset current transformation matrix with identity matrix.

Returns:

  • (nil)

    nil



825
826
827
828
# File 'lib/rubysketch/processing.rb', line 825

def resetMatrix ()
  @painter__.matrix = 1
  nil
end

#rotate(angle) ⇒ nil

Applies rotation matrix to current transformation matrix.

Parameters:

  • angle (Numeric)

    angle for rotation

Returns:

  • (nil)

    nil



798
799
800
801
# File 'lib/rubysketch/processing.rb', line 798

def rotate (angle)
  @painter__.rotate to_angle__ angle
  nil
end

#round(value) ⇒ Numeric

Returns the closest integer number.

Parameters:

  • value (Numeric)

    number

Returns:

  • (Numeric)

    rounded number



103
104
105
# File 'lib/rubysketch/processing.rb', line 103

def round (value)
  value.round
end

#scale(s) ⇒ nil #scale(x, y) ⇒ nil

Applies scale matrix to current transformation matrix.

Parameters:

  • s (Numeric)

    horizontal and vertical scale

  • x (Numeric)

    horizontal scale

  • y (Numeric)

    vertical scale

Returns:

  • (nil)

    nil



787
788
789
790
# File 'lib/rubysketch/processing.rb', line 787

def scale (x, y)
  @painter__.scale x, y
  nil
end

#setup(&block) ⇒ Object



280
281
282
283
# File 'lib/rubysketch/processing.rb', line 280

def setup (&block)
  @window__.setup = block
  nil
end

#sq(value) ⇒ Numeric

Returns squared value.

Parameters:

  • value (Numeric)

    number

Returns:

  • (Numeric)

    squared value



124
125
126
# File 'lib/rubysketch/processing.rb', line 124

def sq (value)
  value * value
end

#stroke(rgb) ⇒ nil #stroke(rgb, alpha) ⇒ nil #stroke(gray) ⇒ nil #stroke(gray, alpha) ⇒ nil #stroke(r, g, b) ⇒ nil #stroke(r, g, b, alpha) ⇒ nil

Sets stroke color.

Parameters:

  • rgb (String)

    color code like ‘#00AAFF’

  • 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



593
594
595
596
# File 'lib/rubysketch/processing.rb', line 593

def stroke (*args)
  @painter__.stroke(*to_rgba__(*args))
  nil
end

#strokeWeight(weight) ⇒ nil

Sets stroke weight.

Parameters:

  • weight (Numeric)

    width of stroke

Returns:

  • (nil)

    nil



604
605
606
607
# File 'lib/rubysketch/processing.rb', line 604

def strokeWeight (weight)
  @painter__.stroke_width weight
  nil
end

#text(str) ⇒ nil #text(str, x, y) ⇒ nil

Draws a text.

Parameters:

  • str (String)

    text to draw

  • x (Numeric) (defaults to: 0)

    horizontal position

  • y (Numeric) (defaults to: 0)

    vertical position

Returns:

  • (nil)

    nil



759
760
761
762
# File 'lib/rubysketch/processing.rb', line 759

def text (str, x = 0, y = 0)
  @painter__.text str, x, y
  nil
end

#textFont(name = nil, size = nil) ⇒ Font

Sets font.

Parameters:

  • name (String) (defaults to: nil)

    font name

  • size (Numeric) (defaults to: nil)

    font size

Returns:

  • (Font)

    current font



634
635
636
637
# File 'lib/rubysketch/processing.rb', line 634

def textFont (name = nil, size = nil)
  @painter__.font name, size if name || size
  Font.new @painter__.font
end

#textSize(size) ⇒ nil

Sets text size.

Parameters:

  • size (Numeric)

    font size

Returns:

  • (nil)

    nil



645
646
647
648
# File 'lib/rubysketch/processing.rb', line 645

def textSize (size)
  @painter__.font @painter__.font.name, size
  nil
end

#translate(x, y) ⇒ nil

Applies translation matrix to current transformation matrix.

Parameters:

  • x (Numeric)

    horizontal transformation

  • y (Numeric)

    vertical transformation

Returns:

  • (nil)

    nil



771
772
773
774
# File 'lib/rubysketch/processing.rb', line 771

def translate (x, y)
  @painter__.translate x, y
  nil
end

#widthObject



381
382
383
# File 'lib/rubysketch/processing.rb', line 381

def width ()
  @window__.canvas.width
end

#windowHeightObject



393
394
395
# File 'lib/rubysketch/processing.rb', line 393

def windowHeight ()
  @window__.height
end

#windowWidthObject



389
390
391
# File 'lib/rubysketch/processing.rb', line 389

def windowWidth ()
  @window__.width
end