Reference

This is a detailed description of Zajal's built in functionality. For a more general overview, read the guide. For something more technical, read the the YARD documentation.

App

show (Boolean)

true to show the cursor, false to hide

Show or hide the cursor

Details Source

# lib/core/app.rb:85
def cursor show=nil
  @cursor_visible ||= true

  if show.present?
    if show
      @cursor_visible = true
      Native.ofShowCursor
    else
      @cursor_visible = false
      Native.ofHideCursor
    end
  else
    @cursor_visible
  end
end
# Hide on mouse click, show on key press
mouse_down do
  cursor false
end

key_down do
  cursor true
end

Get whether or not the cursor is showing

Details Source

# lib/core/app.rb:85
def cursor show=nil
  @cursor_visible ||= true

  if show.present?
    if show
      @cursor_visible = true
      Native.ofShowCursor
    else
      @cursor_visible = false
      Native.ofHideCursor
    end
  else
    @cursor_visible
  end
end

Get the number of frames rendered since the sketch started.

Details Source

# lib/core/app.rb:55
def frame
  # TODO set frame number?
  Native.ofGetFrameNum
end
# Screenshot every 30 frames
draw do
  # draw sketch
  fill false
  30.times do |i|
    circle width/2, height/2, noise(time, i/10.0) * width
  end

  # save a screenshot if frame evenly divides 30
  grab_screen.save "~/Desktop/screenshot.png" if frame % 30 == 0
end
# Render out numbered sequence
draw do
  # draw sketch
  fill false
  30.times do |i|
    circle width/2, height/2, noise(time, i/10.0) * width
  end

  # save a screenshot every frame
  # use frame number to sequentially number them
  grab_screen.save "~/Desktop/frame-#{frame}.png"
end
new_height (#to_i)

sketch height

Set the sketch’s height

Details Source

# lib/core/app.rb:230
def height new_height=nil
  if new_height.present?
    size width, new_height
  else
    Native.ofGetHeight
  end
end

Get the sketch’s current height Useful for alignment

Details Source

# lib/core/app.rb:230
def height new_height=nil
  if new_height.present?
    size width, new_height
  else
    Native.ofGetHeight
  end
end
# Bottom aligned
circle 20, height, 10
circle 50, height, 10
circle 80, height, 10
# Center aligned
circle 20, height/2, 10
circle 50, height/2, 10
circle 80, height/2, 10
# Displaying height
text height
text height/2

Details Source

# lib/core/app.rb:241
def size new_width=nil, new_height=nil
  if new_height.present?
    Native.ofSetWindowShape new_width.to_i, new_height.to_i
  elsif new_width.present?
    size new_width, new_width
  else
    [width, height]
  end
end

Details Source

# lib/core/app.rb:241
def size new_width=nil, new_height=nil
  if new_height.present?
    Native.ofSetWindowShape new_width.to_i, new_height.to_i
  elsif new_width.present?
    size new_width, new_width
  else
    [width, height]
  end
end

Details Source

# lib/core/app.rb:241
def size new_width=nil, new_height=nil
  if new_height.present?
    Native.ofSetWindowShape new_width.to_i, new_height.to_i
  elsif new_width.present?
    size new_width, new_width
  else
    [width, height]
  end
end
new_title (#to_s)

new window title

Set the title of the sketch window

Details Source

# lib/core/app.rb:171
def title new_title=nil
  @window_title ||= ""

  if new_title.present?
    @window_title = new_title.to_s
    Native.ofSetWindowTitle @window_title.to_ptr
  else
    @window_title
  end
end
title "My Amazing Sketch"

Get the current title of the sketch window

Details Source

# lib/core/app.rb:171
def title new_title=nil
  @window_title ||= ""

  if new_title.present?
    @window_title = new_title.to_s
    Native.ofSetWindowTitle @window_title.to_ptr
  else
    @window_title
  end
end
new_width (#to_i)

sketch width

Set the sketch’s width

Details Source

# lib/core/app.rb:202
def width new_width=nil
  if new_width.present?
    size new_width, height
  else
    Native.ofGetWidth
  end
end

Get the sketch’s current width Useful for alignment

Details Source

# lib/core/app.rb:202
def width new_width=nil
  if new_width.present?
    size new_width, height
  else
    Native.ofGetWidth
  end
end
# Right aligned
circle width, 20, 10
circle width, 50, 10
circle width, 80, 10
# Center aligned
circle width/2, 20, 10
circle width/2, 50, 10
circle width/2, 80, 10
# Displaying width
text width
text width/2

Native

Graphics

This module contains methods that draw basic geometric primitives.

Methods either draw directly to the screen (e.g. circle, square) or affect subsequent drawing (e.g. translate).

Graphics mostly a wrapper for ofGraphics in openFrameworks.

on (Boolean)

true to enable alpha blending, false to disable

Enable or disable alpha blending

Alpha blending allows for transparent colors in colors and images, but can slow down your sketch. You can enable and disable it as needed.

Details Source

# lib/core/graphics.rb:105
def alpha_blending on=nil
  @alpha_blending ||= false

  if on.present?
    @alpha_blending = on.to_bool
    @alpha_blending ? Native.ofEnableAlphaBlending : Native.ofDisableAlphaBlending
  end
  
  @alpha_blending
end
# With and without blending
alpha_blending false
color :white, 128
circle 40, 25, 20
circle 60, 25, 20

alpha_blending true
color :white, 128
circle 40, 75, 20
circle 60, 75, 20

Details Source

# lib/core/graphics.rb:105
def alpha_blending on=nil
  @alpha_blending ||= false

  if on.present?
    @alpha_blending = on.to_bool
    @alpha_blending ? Native.ofEnableAlphaBlending : Native.ofDisableAlphaBlending
  end
  
  @alpha_blending
end
red (0..255)

the amount of red

green (0..255)

the amount of green

blue (0..255)

the amount of blue

Set the background color and clear the screen to that color.

Details Source

# lib/core/graphics.rb:147
def background *args
  unless args.empty?
    @background = Color.new(color_mode, *args)
    r, g, b, a = @background.to_rgb.to_a
    Native.ofClear r.to_f, g.to_f, b.to_f, a.to_f
  end

  @background
end
red (0..255)

the amount of red

green (0..255)

the amount of green

blue (0..255)

the amount of blue

alpha (0..255)

the amount of alpha

Set the background color and clear the screen to that color.

Details Source

# lib/core/graphics.rb:147
def background *args
  unless args.empty?
    @background = Color.new(color_mode, *args)
    r, g, b, a = @background.to_rgb.to_a
    Native.ofClear r.to_f, g.to_f, b.to_f, a.to_f
  end

  @background
end
name (Symbol)

the name of the color to use

Set the background color and clear the screen to that color.

Use a named color

Details Source

# lib/core/graphics.rb:147
def background *args
  unless args.empty?
    @background = Color.new(color_mode, *args)
    r, g, b, a = @background.to_rgb.to_a
    Native.ofClear r.to_f, g.to_f, b.to_f, a.to_f
  end

  @background
end
# Blue background
background :blue
circle 50, 50, 30

Set the background color and clear the screen to that color.

Details Source

# lib/core/graphics.rb:147
def background *args
  unless args.empty?
    @background = Color.new(color_mode, *args)
    r, g, b, a = @background.to_rgb.to_a
    Native.ofClear r.to_f, g.to_f, b.to_f, a.to_f
  end

  @background
end

Set the background color and clear the screen to that color.

Details Source

# lib/core/graphics.rb:147
def background *args
  unless args.empty?
    @background = Color.new(color_mode, *args)
    r, g, b, a = @background.to_rgb.to_a
    Native.ofClear r.to_f, g.to_f, b.to_f, a.to_f
  end

  @background
end

Set the background color and clear the screen to that color.

Details Source

# lib/core/graphics.rb:147
def background *args
  unless args.empty?
    @background = Color.new(color_mode, *args)
    r, g, b, a = @background.to_rgb.to_a
    Native.ofClear r.to_f, g.to_f, b.to_f, a.to_f
  end

  @background
end

Set the background color and clear the screen to that color.

Details Source

# lib/core/graphics.rb:147
def background *args
  unless args.empty?
    @background = Color.new(color_mode, *args)
    r, g, b, a = @background.to_rgb.to_a
    Native.ofClear r.to_f, g.to_f, b.to_f, a.to_f
  end

  @background
end

Details Source

# lib/core/graphics.rb:1036
def begin_shape
  Native.ofBeginShape
end

Details Source

# lib/core/graphics.rb:1008
def bezier *args
  x0 = y0 = z0 = x1 = y1 = z1 = x2 = y2 = z2 = x3 = y3 = z3 = 0

  case args
  when Signature[[:x,:y,:z], [:x,:y,:z], [:x,:y,:z], [:x,:y,:z]]
    a, b, c, d = *args
    x0, y0, z0 = a.x, a.y, a.z
    x1, y1, z1 = b.x, b.y, b.z
    x2, y2, z2 = c.x, c.y, c.z
    x3, y3, z3 = d.x, d.y, d.z

  when Signature[[:x,:y], [:x,:y], [:x,:y], [:x,:y]]
    a, b, c, d = *args
    x0, y0 = a.x, a.y
    x1, y1 = b.x, b.y
    x2, y2 = c.x, c.y
    x3, y3 = d.x, d.y

  when Signature[:to_f, :to_f, :to_f, :to_f, :to_f, :to_f, :to_f, :to_f]
    x0, y0, x1, y1, x2, y2, x3, y3 = *args

  when Signature[:to_f, :to_f, :to_f, :to_f, :to_f, :to_f, :to_f, :to_f, :to_f, :to_f, :to_f, :to_f]
    x0, y0, z0, x1, y1, z1, x2, y2, z2, x3, y3, z3 = *args
  end

  Native.ofBezier x0.to_f, y0.to_f, z0.to_f, x1.to_f, y1.to_f, z1.to_f, x2.to_f, y2.to_f, z2.to_f, x3.to_f, y3.to_f, z3.to_f
end
# Bezier curve
fill false
bezier 10, 10, 10, 50, 90, 50, 90, 90

Details Source

# lib/core/graphics.rb:1008
def bezier *args
  x0 = y0 = z0 = x1 = y1 = z1 = x2 = y2 = z2 = x3 = y3 = z3 = 0

  case args
  when Signature[[:x,:y,:z], [:x,:y,:z], [:x,:y,:z], [:x,:y,:z]]
    a, b, c, d = *args
    x0, y0, z0 = a.x, a.y, a.z
    x1, y1, z1 = b.x, b.y, b.z
    x2, y2, z2 = c.x, c.y, c.z
    x3, y3, z3 = d.x, d.y, d.z

  when Signature[[:x,:y], [:x,:y], [:x,:y], [:x,:y]]
    a, b, c, d = *args
    x0, y0 = a.x, a.y
    x1, y1 = b.x, b.y
    x2, y2 = c.x, c.y
    x3, y3 = d.x, d.y

  when Signature[:to_f, :to_f, :to_f, :to_f, :to_f, :to_f, :to_f, :to_f]
    x0, y0, x1, y1, x2, y2, x3, y3 = *args

  when Signature[:to_f, :to_f, :to_f, :to_f, :to_f, :to_f, :to_f, :to_f, :to_f, :to_f, :to_f, :to_f]
    x0, y0, z0, x1, y1, z1, x2, y2, z2, x3, y3, z3 = *args
  end

  Native.ofBezier x0.to_f, y0.to_f, z0.to_f, x1.to_f, y1.to_f, z1.to_f, x2.to_f, y2.to_f, z2.to_f, x3.to_f, y3.to_f, z3.to_f
end

Details Source

# lib/core/graphics.rb:1008
def bezier *args
  x0 = y0 = z0 = x1 = y1 = z1 = x2 = y2 = z2 = x3 = y3 = z3 = 0

  case args
  when Signature[[:x,:y,:z], [:x,:y,:z], [:x,:y,:z], [:x,:y,:z]]
    a, b, c, d = *args
    x0, y0, z0 = a.x, a.y, a.z
    x1, y1, z1 = b.x, b.y, b.z
    x2, y2, z2 = c.x, c.y, c.z
    x3, y3, z3 = d.x, d.y, d.z

  when Signature[[:x,:y], [:x,:y], [:x,:y], [:x,:y]]
    a, b, c, d = *args
    x0, y0 = a.x, a.y
    x1, y1 = b.x, b.y
    x2, y2 = c.x, c.y
    x3, y3 = d.x, d.y

  when Signature[:to_f, :to_f, :to_f, :to_f, :to_f, :to_f, :to_f, :to_f]
    x0, y0, x1, y1, x2, y2, x3, y3 = *args

  when Signature[:to_f, :to_f, :to_f, :to_f, :to_f, :to_f, :to_f, :to_f, :to_f, :to_f, :to_f, :to_f]
    x0, y0, z0, x1, y1, z1, x2, y2, z2, x3, y3, z3 = *args
  end

  Native.ofBezier x0.to_f, y0.to_f, z0.to_f, x1.to_f, y1.to_f, z1.to_f, x2.to_f, y2.to_f, z2.to_f, x3.to_f, y3.to_f, z3.to_f
end

Details Source

# lib/core/graphics.rb:1131
def bezier_vertex *args
  x1 = y1 = z1 = x2 = y2 = z2 = 0

  case args
  when Signature[[:x,:y,:z], [:x,:y,:z]]
    a, b = *args
    x1, y1, z1 = a.x, a.y, a.z
    x2, y2, z2 = b.x, b.y, b.z

  when Signature[[:x,:y], [:x,:y]]
    a, b = *args
    x1, y1 = a.x, a.y
    x2, y2 = b.x, b.y

  when Signature[:to_f, :to_f, :to_f, :to_f]
    x1, y1, x2, y2 = *args

  when Signature[:to_f, :to_f, :to_f, :to_f, :to_f, :to_f]
    x1, y1, z1, x2, y2, z2 = *args
  end
  
  Native.ofBezierVertex x1.to_f, y1.to_f, z1.to_f, x2.to_f, y2.to_f, z2.to_f
end

Details Source

# lib/core/graphics.rb:1131
def bezier_vertex *args
  x1 = y1 = z1 = x2 = y2 = z2 = 0

  case args
  when Signature[[:x,:y,:z], [:x,:y,:z]]
    a, b = *args
    x1, y1, z1 = a.x, a.y, a.z
    x2, y2, z2 = b.x, b.y, b.z

  when Signature[[:x,:y], [:x,:y]]
    a, b = *args
    x1, y1 = a.x, a.y
    x2, y2 = b.x, b.y

  when Signature[:to_f, :to_f, :to_f, :to_f]
    x1, y1, x2, y2 = *args

  when Signature[:to_f, :to_f, :to_f, :to_f, :to_f, :to_f]
    x1, y1, z1, x2, y2, z2 = *args
  end
  
  Native.ofBezierVertex x1.to_f, y1.to_f, z1.to_f, x2.to_f, y2.to_f, z2.to_f
end

Details Source

# lib/core/graphics.rb:1131
def bezier_vertex *args
  x1 = y1 = z1 = x2 = y2 = z2 = 0

  case args
  when Signature[[:x,:y,:z], [:x,:y,:z]]
    a, b = *args
    x1, y1, z1 = a.x, a.y, a.z
    x2, y2, z2 = b.x, b.y, b.z

  when Signature[[:x,:y], [:x,:y]]
    a, b = *args
    x1, y1 = a.x, a.y
    x2, y2 = b.x, b.y

  when Signature[:to_f, :to_f, :to_f, :to_f]
    x1, y1, x2, y2 = *args

  when Signature[:to_f, :to_f, :to_f, :to_f, :to_f, :to_f]
    x1, y1, z1, x2, y2, z2 = *args
  end
  
  Native.ofBezierVertex x1.to_f, y1.to_f, z1.to_f, x2.to_f, y2.to_f, z2.to_f
end

Details Source

# lib/core/graphics.rb:727
def blend_mode mode=nil
  @blend_mode ||= :disabled

  if mode.present?
    @blend_mode = mode
    Native.ofDisableBlendMode
    Native.ofEnableBlendMode @blend_mode unless @blend_mode == :disabled
  else
    @blend_mode
  end
end
# Blend mode
color :white, 128

blend_mode :add
circle 20, 25, 10
circle 30, 25, 10

blend_mode :subtract
circle 80, 25, 10
circle 70, 25, 10

blend_mode :multiply
circle 80, 75, 10
circle 70, 75, 10

blend_mode :alpha
circle 20, 75, 10
circle 30, 75, 10

Details Source

# lib/core/graphics.rb:727
def blend_mode mode=nil
  @blend_mode ||= :disabled

  if mode.present?
    @blend_mode = mode
    Native.ofDisableBlendMode
    Native.ofEnableBlendMode @blend_mode unless @blend_mode == :disabled
  else
    @blend_mode
  end
end

Details Source

# lib/core/graphics.rb:1189
def box *args
  x = y = z = r = 0

  case args
  when Signature[[:x,:y,:z], :to_f]
    p, r = *args
    x, y, z = p.x, p.y, p.z

  when Signature[[:x,:y], :to_f]
    p, r = *args
    x, y = p.x, p.y

  when Signature[:to_f, :to_f, :to_f]
    x, y, r = *args

  when Signature[:to_f, :to_f, :to_f, :to_f]
    x, y, z, r = *args
  end

  Native.ofBox x.to_f, y.to_f, z.to_f, r.to_f
end
# Boxes in the corners
fill false
box 20, 20, 30
box 20, 80, 30
box 80, 20, 30
box 80, 80, 30

Details Source

# lib/core/graphics.rb:1189
def box *args
  x = y = z = r = 0

  case args
  when Signature[[:x,:y,:z], :to_f]
    p, r = *args
    x, y, z = p.x, p.y, p.z

  when Signature[[:x,:y], :to_f]
    p, r = *args
    x, y = p.x, p.y

  when Signature[:to_f, :to_f, :to_f]
    x, y, r = *args

  when Signature[:to_f, :to_f, :to_f, :to_f]
    x, y, z, r = *args
  end

  Native.ofBox x.to_f, y.to_f, z.to_f, r.to_f
end

Details Source

# lib/core/graphics.rb:1189
def box *args
  x = y = z = r = 0

  case args
  when Signature[[:x,:y,:z], :to_f]
    p, r = *args
    x, y, z = p.x, p.y, p.z

  when Signature[[:x,:y], :to_f]
    p, r = *args
    x, y = p.x, p.y

  when Signature[:to_f, :to_f, :to_f]
    x, y, r = *args

  when Signature[:to_f, :to_f, :to_f, :to_f]
    x, y, z, r = *args
  end

  Native.ofBox x.to_f, y.to_f, z.to_f, r.to_f
end
x (Numeric)

x coordinate of circle’s center

y (Numeric)

y coordinate of circle’s center

radius (Numeric)

radius of the circle

Draw a circle.

Details Source

# lib/core/graphics.rb:63
def circle *args
  x = y = z = r = 0

  case args
  when Signature[[:x, :y], :to_f]
    x, y = args.first.x, args.first.y
    r = args.last

  when Signature[[:x, :y, :z], :to_f]
    x, y, z = args.first.x, args.first.y, args.first.z
    r = args.last

  when Signature[:to_f, :to_f, :to_f]
    x, y, r = *args

  when Signature[:to_f, :to_f, :to_f, :to_f]
    x, y, z, r = *args
  end

  Native.ofCircle x.to_f, y.to_f, z.to_f, r.to_f
end
# Single circle
circle 50, 50, 25
# Four circles
circle 25, 25, 20
circle 75, 25, 20
circle 75, 75, 20
circle 25, 75, 20
x (Numeric)

x coordinate of circle’s center

y (Numeric)

y coordinate of circle’s center

z (Numeric)

z coordinate of circle’s center

radius (Numeric)

radius of the circle

Draw a circle.

Details Source

# lib/core/graphics.rb:63
def circle *args
  x = y = z = r = 0

  case args
  when Signature[[:x, :y], :to_f]
    x, y = args.first.x, args.first.y
    r = args.last

  when Signature[[:x, :y, :z], :to_f]
    x, y, z = args.first.x, args.first.y, args.first.z
    r = args.last

  when Signature[:to_f, :to_f, :to_f]
    x, y, r = *args

  when Signature[:to_f, :to_f, :to_f, :to_f]
    x, y, z, r = *args
  end

  Native.ofCircle x.to_f, y.to_f, z.to_f, r.to_f
end
point (#x,#y)

the circle’s center

radius (Numeric)

radius of the circle

Draw a circle.

point can be any object that responds to x and y messages

Details Source

# lib/core/graphics.rb:63
def circle *args
  x = y = z = r = 0

  case args
  when Signature[[:x, :y], :to_f]
    x, y = args.first.x, args.first.y
    r = args.last

  when Signature[[:x, :y, :z], :to_f]
    x, y, z = args.first.x, args.first.y, args.first.z
    r = args.last

  when Signature[:to_f, :to_f, :to_f]
    x, y, r = *args

  when Signature[:to_f, :to_f, :to_f, :to_f]
    x, y, z, r = *args
  end

  Native.ofCircle x.to_f, y.to_f, z.to_f, r.to_f
end
# Point object
class MyPoint
  def initialize x, y
    @x = x
    @y = y
  end

  def x
    @x
  end

  def y
    @y
  end
end

p = MyPoint.new 25, 75
circle p, 20
# Using OpenStruct
p = OpenStruct.new
p.x = 80
p.y = 20

circle p, 10

Details Source

# lib/core/graphics.rb:634
def circle_resolution new_resolution=nil
  @circle_resolution ||= 22 # TODO what is the default resolution

  if new_resolution.present?
    @circle_resolution = new_resolution.to_i
    Native.ofSetCircleResolution @circle_resolution
  else
    @circle_resolution
  end
end
# Low resolution circle
circle_resolution 10
circle 50, 50, 45
# Medium resolution circle
circle_resolution 20
circle 50, 50, 45
# High resolution circle
circle_resolution 60
circle 50, 50, 45

Details Source

# lib/core/graphics.rb:634
def circle_resolution new_resolution=nil
  @circle_resolution ||= 22 # TODO what is the default resolution

  if new_resolution.present?
    @circle_resolution = new_resolution.to_i
    Native.ofSetCircleResolution @circle_resolution
  else
    @circle_resolution
  end
end
red (0..255)

the amount of red

green (0..255)

the amount of green

blue (0..255)

the amount of blue

Clear the canvas to a color.

Details Source

# lib/core/graphics.rb:582
def clear *args
  r, g, b, a = Color.new(color_mode, *args).to_rgb.to_a
  Native.ofClear r.to_f, g.to_f, b.to_f, a.to_f
end
red (0..255)

the amount of red

green (0..255)

the amount of green

blue (0..255)

the amount of blue

alpha (0..255)

the amount of alpha

Clear the canvas to a color.

Details Source

# lib/core/graphics.rb:582
def clear *args
  r, g, b, a = Color.new(color_mode, *args).to_rgb.to_a
  Native.ofClear r.to_f, g.to_f, b.to_f, a.to_f
end

Clear the canvas to a color.

Details Source

# lib/core/graphics.rb:582
def clear *args
  r, g, b, a = Color.new(color_mode, *args).to_rgb.to_a
  Native.ofClear r.to_f, g.to_f, b.to_f, a.to_f
end

Clear the canvas to a color.

Details Source

# lib/core/graphics.rb:582
def clear *args
  r, g, b, a = Color.new(color_mode, *args).to_rgb.to_a
  Native.ofClear r.to_f, g.to_f, b.to_f, a.to_f
end

Clear the canvas to a color.

Details Source

# lib/core/graphics.rb:582
def clear *args
  r, g, b, a = Color.new(color_mode, *args).to_rgb.to_a
  Native.ofClear r.to_f, g.to_f, b.to_f, a.to_f
end

Clear the canvas to a color.

Details Source

# lib/core/graphics.rb:582
def clear *args
  r, g, b, a = Color.new(color_mode, *args).to_rgb.to_a
  Native.ofClear r.to_f, g.to_f, b.to_f, a.to_f
end

Details Source

# lib/core/graphics.rb:780
def clear_background clear=nil
  if clear.present?
    Native.ofSetBackgroundAuto clear.to_bool
  else
    Native.ofbClearBg
  end
end

Details Source

# lib/core/graphics.rb:780
def clear_background clear=nil
  if clear.present?
    Native.ofSetBackgroundAuto clear.to_bool
  else
    Native.ofbClearBg
  end
end

Set the color that subsequent drawing will be done in.

red, green, and blue are numbers between 0 and 255

Details Source

# lib/core/graphics.rb:527
def color *args
  unless args.empty?
    @color = Color.new(color_mode, *args)
    r, g, b, a = @color.to_rgb.to_a
    Native.ofSetColor r.to_i, g.to_i, b.to_i, a.to_i
  end

  @color
end
# RGB colors
color_mode :rgb
color 200, 128, 64
circle 20, 50, 15

color 30, 128, 200
circle 80, 50, 15

Set the color that subsequent drawing will be done in.

Details Source

# lib/core/graphics.rb:527
def color *args
  unless args.empty?
    @color = Color.new(color_mode, *args)
    r, g, b, a = @color.to_rgb.to_a
    Native.ofSetColor r.to_i, g.to_i, b.to_i, a.to_i
  end

  @color
end

Set the color that subsequent drawing will be done in.

Details Source

# lib/core/graphics.rb:527
def color *args
  unless args.empty?
    @color = Color.new(color_mode, *args)
    r, g, b, a = @color.to_rgb.to_a
    Native.ofSetColor r.to_i, g.to_i, b.to_i, a.to_i
  end

  @color
end
# Common colors
color :yellow
circle 20, 50, 15
color :lime_green
circle 80, 50, 15
# Esotetic colors
color :light_goldenrod_yellow
circle 20, 20, 15

color :medium_aquamarine
circle 80, 20, 15

color :cornflower_blue
circle 20, 80, 15

color :light_slate_gray
circle 80, 80, 15

Set the color that subsequent drawing will be done in.

Details Source

# lib/core/graphics.rb:527
def color *args
  unless args.empty?
    @color = Color.new(color_mode, *args)
    r, g, b, a = @color.to_rgb.to_a
    Native.ofSetColor r.to_i, g.to_i, b.to_i, a.to_i
  end

  @color
end

Set the color that subsequent drawing will be done in.

Details Source

# lib/core/graphics.rb:527
def color *args
  unless args.empty?
    @color = Color.new(color_mode, *args)
    r, g, b, a = @color.to_rgb.to_a
    Native.ofSetColor r.to_i, g.to_i, b.to_i, a.to_i
  end

  @color
end

Set the color that subsequent drawing will be done in.

Details Source

# lib/core/graphics.rb:527
def color *args
  unless args.empty?
    @color = Color.new(color_mode, *args)
    r, g, b, a = @color.to_rgb.to_a
    Native.ofSetColor r.to_i, g.to_i, b.to_i, a.to_i
  end

  @color
end

Set the color that subsequent drawing will be done in.

Details Source

# lib/core/graphics.rb:527
def color *args
  unless args.empty?
    @color = Color.new(color_mode, *args)
    r, g, b, a = @color.to_rgb.to_a
    Native.ofSetColor r.to_i, g.to_i, b.to_i, a.to_i
  end

  @color
end
# HSV colors
color_mode :hsv
background :white

color 0, 200, 200
circle 20, 50, 15

color 200, 200, 200
circle 80, 50, 15
# Splatter
translate width/2, height/2

color_mode :hsv
clear :black

128.times do |i|
  color i*2, 255, 255
  line 0, 0, cos(i/128.0 * PI)*50, sin(i)*50
end

Set the color that subsequent drawing will be done in.

Details Source

# lib/core/graphics.rb:527
def color *args
  unless args.empty?
    @color = Color.new(color_mode, *args)
    r, g, b, a = @color.to_rgb.to_a
    Native.ofSetColor r.to_i, g.to_i, b.to_i, a.to_i
  end

  @color
end

Set the color that subsequent drawing will be done in.

Details Source

# lib/core/graphics.rb:527
def color *args
  unless args.empty?
    @color = Color.new(color_mode, *args)
    r, g, b, a = @color.to_rgb.to_a
    Native.ofSetColor r.to_i, g.to_i, b.to_i, a.to_i
  end

  @color
end
mode (Symbol)

new color mode

Set the way subsequent colors will be interpreted

:rgb and :hsv are recognized

Details Source

# lib/core/graphics.rb:556
def color_mode mode=nil
  @color_mode ||= :rgb

  @color_mode = mode.to_sym if mode.present?
  @color_mode
end
# Splatter
translate width/2, height/2

color_mode :hsv
clear :black

128.times do |i|
  color i*2, 255, 255
  line 0, 0, cos(i/128.0 * PI)*50, sin(i)*50
end

Details Source

# lib/core/graphics.rb:556
def color_mode mode=nil
  @color_mode ||= :rgb

  @color_mode = mode.to_sym if mode.present?
  @color_mode
end

Details Source

# lib/core/graphics.rb:973
def curve *args
  x0 = y0 = z0 = x1 = y1 = z1 = x2 = y2 = z2 = x3 = y3 = z3 = 0

  case args
  when Signature[[:x,:y,:z], [:x,:y,:z], [:x,:y,:z], [:x,:y,:z]]
    a, b, c, d = *args
    x0, y0, z0 = a.x, a.y, a.z
    x1, y1, z1 = b.x, b.y, b.z
    x2, y2, z2 = c.x, c.y, c.z
    x3, y3, z3 = d.x, d.y, d.z

  when Signature[[:x,:y], [:x,:y], [:x,:y], [:x,:y]]
    a, b, c, d = *args
    x0, y0 = a.x, a.y
    x1, y1 = b.x, b.y
    x2, y2 = c.x, c.y
    x3, y3 = d.x, d.y

  when Signature[:to_f, :to_f, :to_f, :to_f, :to_f, :to_f, :to_f, :to_f]
    x0, y0, x1, y1, x2, y2, x3, y3 = *args

  when Signature[:to_f, :to_f, :to_f, :to_f, :to_f, :to_f, :to_f, :to_f, :to_f, :to_f, :to_f, :to_f]
    x0, y0, z0, x1, y1, z1, x2, y2, z2, x3, y3, z3 = *args
  end

  Native.ofCurve x0.to_f, y0.to_f, z0.to_f, x1.to_f, y1.to_f, z1.to_f, x2.to_f, y2.to_f, z2.to_f, x3.to_f, y3.to_f, z3.to_f
end
fill false
curve 10, -100, 10, 50, 90, 50, 90, 200

Details Source

# lib/core/graphics.rb:973
def curve *args
  x0 = y0 = z0 = x1 = y1 = z1 = x2 = y2 = z2 = x3 = y3 = z3 = 0

  case args
  when Signature[[:x,:y,:z], [:x,:y,:z], [:x,:y,:z], [:x,:y,:z]]
    a, b, c, d = *args
    x0, y0, z0 = a.x, a.y, a.z
    x1, y1, z1 = b.x, b.y, b.z
    x2, y2, z2 = c.x, c.y, c.z
    x3, y3, z3 = d.x, d.y, d.z

  when Signature[[:x,:y], [:x,:y], [:x,:y], [:x,:y]]
    a, b, c, d = *args
    x0, y0 = a.x, a.y
    x1, y1 = b.x, b.y
    x2, y2 = c.x, c.y
    x3, y3 = d.x, d.y

  when Signature[:to_f, :to_f, :to_f, :to_f, :to_f, :to_f, :to_f, :to_f]
    x0, y0, x1, y1, x2, y2, x3, y3 = *args

  when Signature[:to_f, :to_f, :to_f, :to_f, :to_f, :to_f, :to_f, :to_f, :to_f, :to_f, :to_f, :to_f]
    x0, y0, z0, x1, y1, z1, x2, y2, z2, x3, y3, z3 = *args
  end

  Native.ofCurve x0.to_f, y0.to_f, z0.to_f, x1.to_f, y1.to_f, z1.to_f, x2.to_f, y2.to_f, z2.to_f, x3.to_f, y3.to_f, z3.to_f
end

Details Source

# lib/core/graphics.rb:973
def curve *args
  x0 = y0 = z0 = x1 = y1 = z1 = x2 = y2 = z2 = x3 = y3 = z3 = 0

  case args
  when Signature[[:x,:y,:z], [:x,:y,:z], [:x,:y,:z], [:x,:y,:z]]
    a, b, c, d = *args
    x0, y0, z0 = a.x, a.y, a.z
    x1, y1, z1 = b.x, b.y, b.z
    x2, y2, z2 = c.x, c.y, c.z
    x3, y3, z3 = d.x, d.y, d.z

  when Signature[[:x,:y], [:x,:y], [:x,:y], [:x,:y]]
    a, b, c, d = *args
    x0, y0 = a.x, a.y
    x1, y1 = b.x, b.y
    x2, y2 = c.x, c.y
    x3, y3 = d.x, d.y

  when Signature[:to_f, :to_f, :to_f, :to_f, :to_f, :to_f, :to_f, :to_f]
    x0, y0, x1, y1, x2, y2, x3, y3 = *args

  when Signature[:to_f, :to_f, :to_f, :to_f, :to_f, :to_f, :to_f, :to_f, :to_f, :to_f, :to_f, :to_f]
    x0, y0, z0, x1, y1, z1, x2, y2, z2, x3, y3, z3 = *args
  end

  Native.ofCurve x0.to_f, y0.to_f, z0.to_f, x1.to_f, y1.to_f, z1.to_f, x2.to_f, y2.to_f, z2.to_f, x3.to_f, y3.to_f, z3.to_f
end

Details Source

# lib/core/graphics.rb:647
def curve_resolution new_resolution=nil
  @curve_resolution ||= 22 # TODO what is the default resolution

  if new_resolution.present?
    @curve_resolution = new_resolution.to_i
    Native.ofSetCurveResolution @curve_resolution
  else
    @curve_resolution
  end
end

Details Source

# lib/core/graphics.rb:647
def curve_resolution new_resolution=nil
  @curve_resolution ||= 22 # TODO what is the default resolution

  if new_resolution.present?
    @curve_resolution = new_resolution.to_i
    Native.ofSetCurveResolution @curve_resolution
  else
    @curve_resolution
  end
end

Details Source

# lib/core/graphics.rb:1114
def curve_vertex *args
  x = y = 0

  case args
  when Signature[[:x,:y]]
    x, y = args.first.x, args.first.y

  when Signature[:to_f, :to_f]
    x, y = *args
  end

  Native.ofCurveVertex x.to_f, y.to_f
end
# Soft star
shape do
  curve_vertex 50, 10
  curve_vertex 50, 10
  curve_vertex 40, 30
  curve_vertex 10, 30
  curve_vertex 40, 45
  curve_vertex 20, 80
  curve_vertex 50, 60
  curve_vertex 80, 80
  curve_vertex 60, 45
  curve_vertex 90, 30
  curve_vertex 60, 30
  curve_vertex 50, 10
end

Details Source

# lib/core/graphics.rb:1114
def curve_vertex *args
  x = y = 0

  case args
  when Signature[[:x,:y]]
    x, y = args.first.x, args.first.y

  when Signature[:to_f, :to_f]
    x, y = *args
  end

  Native.ofCurveVertex x.to_f, y.to_f
end

Reset graphics settings to Zajal’s defaults.

Details Source

# lib/core/graphics.rb:1270
def defaults
  alpha_blending false
  background :ketchup
  blend_mode :disabled
  circle_resolution 22
  clear_background true
  color_mode :rgb
  color :white
  curve_resolution 22
  fill true
  line_width 1
  point_sprites false
  polygon_winding_mode :odd
  rectangle_mode :corner
  smoothing false
  sphere_resolution 8
  Zajal::Graphics::Native.ofSetupScreenPerspective width.to_f, height.to_f, :default, false, 60.0, 0.0, 0.0
end

Details Source

# lib/core/graphics.rb:904
def ellipse *args
  x = y = z = w = h = 0

  case args
  when Signature[[:x,:y], [:width,:height]]
    point, dim = *args
    x, y = point.x, point.y
    w, h = point.width, point.height

  when Signature[[:x,:y], :to_f, :to_f]
    point, w, h = *args
    x, y = point.x, point.y

  when Signature[:to_f, :to_f, [:width,:height]]
    x, y, dim = *args
    w, h = dim.width, dim.height

  when Signature[:to_f, :to_f, :to_f, :to_f]
    x, y, w, h = *args

  when Signature[:to_f, :to_f, :to_f, :to_f, :to_f]
    x, y, z, w, h = *args
  end

  Native.ofEllipse x.to_f, y.to_f, z.to_f, w.to_f, h.to_f
end
# Ellipse
ellipse 50, 50, 80, 40

Details Source

# lib/core/graphics.rb:904
def ellipse *args
  x = y = z = w = h = 0

  case args
  when Signature[[:x,:y], [:width,:height]]
    point, dim = *args
    x, y = point.x, point.y
    w, h = point.width, point.height

  when Signature[[:x,:y], :to_f, :to_f]
    point, w, h = *args
    x, y = point.x, point.y

  when Signature[:to_f, :to_f, [:width,:height]]
    x, y, dim = *args
    w, h = dim.width, dim.height

  when Signature[:to_f, :to_f, :to_f, :to_f]
    x, y, w, h = *args

  when Signature[:to_f, :to_f, :to_f, :to_f, :to_f]
    x, y, z, w, h = *args
  end

  Native.ofEllipse x.to_f, y.to_f, z.to_f, w.to_f, h.to_f
end

Details Source

# lib/core/graphics.rb:904
def ellipse *args
  x = y = z = w = h = 0

  case args
  when Signature[[:x,:y], [:width,:height]]
    point, dim = *args
    x, y = point.x, point.y
    w, h = point.width, point.height

  when Signature[[:x,:y], :to_f, :to_f]
    point, w, h = *args
    x, y = point.x, point.y

  when Signature[:to_f, :to_f, [:width,:height]]
    x, y, dim = *args
    w, h = dim.width, dim.height

  when Signature[:to_f, :to_f, :to_f, :to_f]
    x, y, w, h = *args

  when Signature[:to_f, :to_f, :to_f, :to_f, :to_f]
    x, y, z, w, h = *args
  end

  Native.ofEllipse x.to_f, y.to_f, z.to_f, w.to_f, h.to_f
end

Details Source

# lib/core/graphics.rb:904
def ellipse *args
  x = y = z = w = h = 0

  case args
  when Signature[[:x,:y], [:width,:height]]
    point, dim = *args
    x, y = point.x, point.y
    w, h = point.width, point.height

  when Signature[[:x,:y], :to_f, :to_f]
    point, w, h = *args
    x, y = point.x, point.y

  when Signature[:to_f, :to_f, [:width,:height]]
    x, y, dim = *args
    w, h = dim.width, dim.height

  when Signature[:to_f, :to_f, :to_f, :to_f]
    x, y, w, h = *args

  when Signature[:to_f, :to_f, :to_f, :to_f, :to_f]
    x, y, z, w, h = *args
  end

  Native.ofEllipse x.to_f, y.to_f, z.to_f, w.to_f, h.to_f
end

Details Source

# lib/core/graphics.rb:904
def ellipse *args
  x = y = z = w = h = 0

  case args
  when Signature[[:x,:y], [:width,:height]]
    point, dim = *args
    x, y = point.x, point.y
    w, h = point.width, point.height

  when Signature[[:x,:y], :to_f, :to_f]
    point, w, h = *args
    x, y = point.x, point.y

  when Signature[:to_f, :to_f, [:width,:height]]
    x, y, dim = *args
    w, h = dim.width, dim.height

  when Signature[:to_f, :to_f, :to_f, :to_f]
    x, y, w, h = *args

  when Signature[:to_f, :to_f, :to_f, :to_f, :to_f]
    x, y, z, w, h = *args
  end

  Native.ofEllipse x.to_f, y.to_f, z.to_f, w.to_f, h.to_f
end

Details Source

# lib/core/graphics.rb:1040
def end_shape close=true
  Native.ofEndShape close.to_bool
end

Details Source

# lib/core/graphics.rb:696
def fill filled=nil
  if filled.present?
    filled ? Native.ofFill : Native.ofNoFill
  else
    Native.ofGetFill == :filled
  end
end
# Filled and unfilled circles
fill true
circle 30, 50, 15

fill false
circle 70, 50, 15

Details Source

# lib/core/graphics.rb:696
def fill filled=nil
  if filled.present?
    filled ? Native.ofFill : Native.ofNoFill
  else
    Native.ofGetFill == :filled
  end
end
x1 (Numeric)

the x coordinate of the first point

y1 (Numeric)

the y coordinate of the first point

x2 (Numeric)

the x coordinate of the second point

y2 (Numeric)

the y coordinate of the second point

Draw a line between two points.

Details Source

# lib/core/graphics.rb:259
def line *args
  x1 = y1 = z1 = x2 = y2 = z2 = 0

  case args
  when Signature[[:x,:y] ,[:x,:y]]
    x1, y1 = args.first.x, args.first.y
    x2, y2 = args.last.x, args.last.y

  when Signature[[:x,:y,:z] ,[:x,:y,:z]]
    x1, y1, z1 = args.first.x, args.first.y, args.first.z
    x2, y2, z2 = args.last.x, args.last.y, args.last.z

  when Signature[:to_f ,:to_f ,:to_f ,:to_f]
    x1, y1, x2, y2 = *args

  when Signature[:to_f ,:to_f ,:to_f ,:to_f ,:to_f ,:to_f]
    x1, y1, z1, x2, y2, z2 = *args
  end

  Native.ofLine x1.to_f, y1.to_f, z1.to_f, x2.to_f, y2.to_f, z2.to_f
end
# Single line
line 10, 10, 90, 90
# Multiple lines
line 10, 0, 90, 80
line 10, 10, 90, 90
line 10, 20, 90, 100
# Mesh
10.times do |i|
  line i*10, 100, 100, 100-i*10
end
x1 (Numeric)

the x coordinate of the first point

y1 (Numeric)

the y coordinate of the first point

z1 (Numeric)

the z coordinate of the first point

x2 (Numeric)

the x coordinate of the second point

y2 (Numeric)

the y coordinate of the second point

z2 (Numeric)

the z coordinate of the second point

Draw a line between two points.

Details Source

# lib/core/graphics.rb:259
def line *args
  x1 = y1 = z1 = x2 = y2 = z2 = 0

  case args
  when Signature[[:x,:y] ,[:x,:y]]
    x1, y1 = args.first.x, args.first.y
    x2, y2 = args.last.x, args.last.y

  when Signature[[:x,:y,:z] ,[:x,:y,:z]]
    x1, y1, z1 = args.first.x, args.first.y, args.first.z
    x2, y2, z2 = args.last.x, args.last.y, args.last.z

  when Signature[:to_f ,:to_f ,:to_f ,:to_f]
    x1, y1, x2, y2 = *args

  when Signature[:to_f ,:to_f ,:to_f ,:to_f ,:to_f ,:to_f]
    x1, y1, z1, x2, y2, z2 = *args
  end

  Native.ofLine x1.to_f, y1.to_f, z1.to_f, x2.to_f, y2.to_f, z2.to_f
end

Draw a line between two points.

Details Source

# lib/core/graphics.rb:259
def line *args
  x1 = y1 = z1 = x2 = y2 = z2 = 0

  case args
  when Signature[[:x,:y] ,[:x,:y]]
    x1, y1 = args.first.x, args.first.y
    x2, y2 = args.last.x, args.last.y

  when Signature[[:x,:y,:z] ,[:x,:y,:z]]
    x1, y1, z1 = args.first.x, args.first.y, args.first.z
    x2, y2, z2 = args.last.x, args.last.y, args.last.z

  when Signature[:to_f ,:to_f ,:to_f ,:to_f]
    x1, y1, x2, y2 = *args

  when Signature[:to_f ,:to_f ,:to_f ,:to_f ,:to_f ,:to_f]
    x1, y1, z1, x2, y2, z2 = *args
  end

  Native.ofLine x1.to_f, y1.to_f, z1.to_f, x2.to_f, y2.to_f, z2.to_f
end
# Point objects
a = OpenStruct.new
b = OpenStruct.new

a.x = 10
a.y = 20
b.x = 90
b.y = 70

line a, b
new_width (Numeric)

the width of the lines

Set the width of subsequent lines

Details Source

# lib/core/graphics.rb:301
def line_width new_width=nil
  @line_width ||= 1

  if new_width.present?
    @line_width = new_width.to_f
    Native.ofSetLineWidth @line_width
  else
    @line_width
  end
end
# Two lines
line_width 1
line 10, 5, 90, 85

line_width 5
line 10, 15, 90, 95

Get the current line width

Details Source

# lib/core/graphics.rb:301
def line_width new_width=nil
  @line_width ||= 1

  if new_width.present?
    @line_width = new_width.to_f
    Native.ofSetLineWidth @line_width
  else
    @line_width
  end
end
text line_width
line_width 10
text line_width

Details Source

# lib/core/graphics.rb:595
def matrix
    push_matrix
    yield
    pop_matrix
end

Details Source

# lib/core/graphics.rb:1073
def next_contour close=true
  Native.ofNextContour close.to_bool
end

Details Source

# lib/core/graphics.rb:1265
def point x, y
  shape { vertex x, y; vertex x, y+1 }
end
# Single point
point 50, 50

Details Source

# lib/core/graphics.rb:741
def point_sprites sprites=nil
  @point_sprites ||= false

  if sprites.present?
    @point_sprites = sprites.to_bool
    @point_sprites ? Native.ofEnablePointSprites : Native.ofDisablePointSprites
  else
    @point_sprites
  end
end

Details Source

# lib/core/graphics.rb:741
def point_sprites sprites=nil
  @point_sprites ||= false

  if sprites.present?
    @point_sprites = sprites.to_bool
    @point_sprites ? Native.ofEnablePointSprites : Native.ofDisablePointSprites
  else
    @point_sprites
  end
end

Details Source

# lib/core/graphics.rb:1213
def polygon_winding_mode mode=nil
  @polygon_winding_mode ||= :odd # is this the default?

  if mode.present?
    @polygon_winding_mode = mode
    Native.ofSetPolyMode @polygon_winding_mode
  else
    @polygon_winding_mode
  end
end

Details Source

# lib/core/graphics.rb:1213
def polygon_winding_mode mode=nil
  @polygon_winding_mode ||= :odd # is this the default?

  if mode.present?
    @polygon_winding_mode = mode
    Native.ofSetPolyMode @polygon_winding_mode
  else
    @polygon_winding_mode
  end
end

Details Source

# lib/core/graphics.rb:591
def pop_matrix
    Native.ofPushMatrix
end

Details Source

# lib/core/graphics.rb:756
def pop_style
  Native.ofPopStyle
end

Details Source

# lib/core/graphics.rb:1228
def pop_view
  Native.ofPopView
end

Details Source

# lib/core/graphics.rb:587
def push_matrix
    Native.ofPushMatrix
end

Details Source

# lib/core/graphics.rb:752
def push_style
  Native.ofPushStyle
end

Details Source

# lib/core/graphics.rb:1224
def push_view
  Native.ofPushView
end
x (Numeric)

x coordinate of top left corner

y (Numeric)

y coordinate of top left corner

width (Numeric)

width of rectangle

height (Numeric)

height of rectangle

Draw a rectangle.

Details Source

# lib/core/graphics.rb:189
def rectangle *args
  x, y, z, w, h = 0

  case args
  when Signature[[:x,:y], [:width,:height]]
    corner, dim = *args
    x, y = corner.x, corner.y
    z = corner.z if corner.respond_to? :z
    w, h = dim.width, dim.height

  when Signature[[:x,:y], :to_f, :to_f]
    corner, w, h = *args
    x, y = corner.x, corner.y
    z = corner.z if corner.respond_to? :z

  when Signature[:to_f, :to_f, [:width, :height]]
    x, y, dim = *args
    w, h = dim.width, dim.height

  when Signature[:to_f, :to_f, :to_f, :to_f]
    x, y, w, h = *args

  when Signature[:to_f, :to_f, :to_f, :to_f, :to_f]
    x, y, z, w, h = *args
  end

  Native.ofRect x.to_f, y.to_f, z.to_f, w.to_f, h.to_f
end
# Single thin rectangle
rectangle 45, 10, 10, 80
# Single wide rectangle
rectangle 25, 10, 50, 80
# Carefully placed rectangles
rectangle 10, 10, 1, 80
rectangle 12, 10, 2, 80
rectangle 15, 10, 3, 80
rectangle 19, 10, 4, 80
rectangle 24, 10, 5, 80
rectangle 30, 10, 6, 80
rectangle 37, 10, 7, 80
rectangle 45, 10, 8, 80
rectangle 54, 10, 9, 80
rectangle 64, 10, 10, 80
rectangle 75, 10, 11, 80

Draw a rectangle.

Details Source

# lib/core/graphics.rb:189
def rectangle *args
  x, y, z, w, h = 0

  case args
  when Signature[[:x,:y], [:width,:height]]
    corner, dim = *args
    x, y = corner.x, corner.y
    z = corner.z if corner.respond_to? :z
    w, h = dim.width, dim.height

  when Signature[[:x,:y], :to_f, :to_f]
    corner, w, h = *args
    x, y = corner.x, corner.y
    z = corner.z if corner.respond_to? :z

  when Signature[:to_f, :to_f, [:width, :height]]
    x, y, dim = *args
    w, h = dim.width, dim.height

  when Signature[:to_f, :to_f, :to_f, :to_f]
    x, y, w, h = *args

  when Signature[:to_f, :to_f, :to_f, :to_f, :to_f]
    x, y, z, w, h = *args
  end

  Native.ofRect x.to_f, y.to_f, z.to_f, w.to_f, h.to_f
end

Draw a rectangle.

Details Source

# lib/core/graphics.rb:189
def rectangle *args
  x, y, z, w, h = 0

  case args
  when Signature[[:x,:y], [:width,:height]]
    corner, dim = *args
    x, y = corner.x, corner.y
    z = corner.z if corner.respond_to? :z
    w, h = dim.width, dim.height

  when Signature[[:x,:y], :to_f, :to_f]
    corner, w, h = *args
    x, y = corner.x, corner.y
    z = corner.z if corner.respond_to? :z

  when Signature[:to_f, :to_f, [:width, :height]]
    x, y, dim = *args
    w, h = dim.width, dim.height

  when Signature[:to_f, :to_f, :to_f, :to_f]
    x, y, w, h = *args

  when Signature[:to_f, :to_f, :to_f, :to_f, :to_f]
    x, y, z, w, h = *args
  end

  Native.ofRect x.to_f, y.to_f, z.to_f, w.to_f, h.to_f
end

Draw a rectangle.

Details Source

# lib/core/graphics.rb:189
def rectangle *args
  x, y, z, w, h = 0

  case args
  when Signature[[:x,:y], [:width,:height]]
    corner, dim = *args
    x, y = corner.x, corner.y
    z = corner.z if corner.respond_to? :z
    w, h = dim.width, dim.height

  when Signature[[:x,:y], :to_f, :to_f]
    corner, w, h = *args
    x, y = corner.x, corner.y
    z = corner.z if corner.respond_to? :z

  when Signature[:to_f, :to_f, [:width, :height]]
    x, y, dim = *args
    w, h = dim.width, dim.height

  when Signature[:to_f, :to_f, :to_f, :to_f]
    x, y, w, h = *args

  when Signature[:to_f, :to_f, :to_f, :to_f, :to_f]
    x, y, z, w, h = *args
  end

  Native.ofRect x.to_f, y.to_f, z.to_f, w.to_f, h.to_f
end

Draw a rectangle.

Details Source

# lib/core/graphics.rb:189
def rectangle *args
  x, y, z, w, h = 0

  case args
  when Signature[[:x,:y], [:width,:height]]
    corner, dim = *args
    x, y = corner.x, corner.y
    z = corner.z if corner.respond_to? :z
    w, h = dim.width, dim.height

  when Signature[[:x,:y], :to_f, :to_f]
    corner, w, h = *args
    x, y = corner.x, corner.y
    z = corner.z if corner.respond_to? :z

  when Signature[:to_f, :to_f, [:width, :height]]
    x, y, dim = *args
    w, h = dim.width, dim.height

  when Signature[:to_f, :to_f, :to_f, :to_f]
    x, y, w, h = *args

  when Signature[:to_f, :to_f, :to_f, :to_f, :to_f]
    x, y, z, w, h = *args
  end

  Native.ofRect x.to_f, y.to_f, z.to_f, w.to_f, h.to_f
end

Details Source

# lib/core/graphics.rb:612
def rectangle_mode mode=nil
  if mode.present?
    Native.ofSetRectMode mode
  else
    Native.ofGetRectMode
  end
end
# Rectangle modes
rectangle_mode :corner
color :white
square 50, 50, 20

rectangle_mode :center
color :black
square 50, 50, 20

Details Source

# lib/core/graphics.rb:612
def rectangle_mode mode=nil
  if mode.present?
    Native.ofSetRectMode mode
  else
    Native.ofGetRectMode
  end
end

Rotate all subsequent drawing

Rotating is centered at the top left corner

Details Source

# lib/core/graphics.rb:443
def rotate *args
  degrees = x = y = z = 0

  case args
  when Signature[:to_f]
    degrees = args.first
    z = 1

  when Signature[:to_f, [:x,:y,:z]]
    degrees, v = *args
    x, y, z = v.x, v.y, v.z

  when Signature[:to_f, :to_f, :to_f, :to_f]
    degrees, x, y, z = *args
  end

  Native.ofRotate degrees.to_f, x.to_f, y.to_f, z.to_f
end
# Same square rotated
fill false
rotate 0
square 50, 10, 20

rotate 5
square 50, 10, 20

rotate 10
square 50, 10, 20

rotate 15
square 50, 10, 20

rotate 20
square 50, 10, 20

rotate 25
square 50, 10, 20

Details Source

# lib/core/graphics.rb:443
def rotate *args
  degrees = x = y = z = 0

  case args
  when Signature[:to_f]
    degrees = args.first
    z = 1

  when Signature[:to_f, [:x,:y,:z]]
    degrees, v = *args
    x, y, z = v.x, v.y, v.z

  when Signature[:to_f, :to_f, :to_f, :to_f]
    degrees, x, y, z = *args
  end

  Native.ofRotate degrees.to_f, x.to_f, y.to_f, z.to_f
end

Details Source

# lib/core/graphics.rb:443
def rotate *args
  degrees = x = y = z = 0

  case args
  when Signature[:to_f]
    degrees = args.first
    z = 1

  when Signature[:to_f, [:x,:y,:z]]
    degrees, v = *args
    x, y, z = v.x, v.y, v.z

  when Signature[:to_f, :to_f, :to_f, :to_f]
    degrees, x, y, z = *args
  end

  Native.ofRotate degrees.to_f, x.to_f, y.to_f, z.to_f
end

Details Source

# lib/core/graphics.rb:935
def rounded_rectangle *args
  x = y = z = w = h = r = 0

  case args
  when Signature[[:x,:y,:z], [:width,:height], :to_f]
    point, dim, r = *args
    x, y, z = point.x, point.y, point.z
    w, h = dim.width, dim.height

  when Signature[[:x,:y], [:width,:height], :to_f]
    point, dim, r = *args
    x, y = point.x, point.y
    w, h = dim.width, dim.height

  when Signature[:to_f, :to_f, [:width,:height], :to_f]
    x, y, dim, r = *args
    w, h = dim.width, dim.height

  when Signature[[:x,:y], :to_f, :to_f, :to_f]
    point, w, h, r = *args
    x, y = point.x, point.y

  when Signature[:to_f, :to_f, :to_f, :to_f, :to_f]
    x, y, w, h, r = *args

  when Signature[:to_f, :to_f, :to_f, :to_f, :to_f, :to_f]
    x, y, z, w, h, r = *args
  end

  Native.ofRectRounded x.to_f, y.to_f, z.to_f, w.to_f, h.to_f, r.to_f
end
# Basic rounded rectangle
rounded_rectangle 10, 10, 80, 80, 10

Details Source

# lib/core/graphics.rb:935
def rounded_rectangle *args
  x = y = z = w = h = r = 0

  case args
  when Signature[[:x,:y,:z], [:width,:height], :to_f]
    point, dim, r = *args
    x, y, z = point.x, point.y, point.z
    w, h = dim.width, dim.height

  when Signature[[:x,:y], [:width,:height], :to_f]
    point, dim, r = *args
    x, y = point.x, point.y
    w, h = dim.width, dim.height

  when Signature[:to_f, :to_f, [:width,:height], :to_f]
    x, y, dim, r = *args
    w, h = dim.width, dim.height

  when Signature[[:x,:y], :to_f, :to_f, :to_f]
    point, w, h, r = *args
    x, y = point.x, point.y

  when Signature[:to_f, :to_f, :to_f, :to_f, :to_f]
    x, y, w, h, r = *args

  when Signature[:to_f, :to_f, :to_f, :to_f, :to_f, :to_f]
    x, y, z, w, h, r = *args
  end

  Native.ofRectRounded x.to_f, y.to_f, z.to_f, w.to_f, h.to_f, r.to_f
end
size (Numeric)

amount to scale in all directions

Scale all subsequent drawing.

Details Source

# lib/core/graphics.rb:412
def scale x, y=nil, z=1.0
  y = x unless y.present?
  Native.ofScale x.to_f, y.to_f, z.to_f
end
# Same circle scaled
fill false

scale 1
circle 10, 10, 10

scale 1.5
circle 10, 10, 10

scale 2
circle 10, 10, 10

scale 2.5
circle 10, 10, 10

scale 3
circle 10, 10, 10
x (Numeric)

amount to scale in x

y (Numeric)

amount to scale in y

Scale all subsequent drawing.

Details Source

# lib/core/graphics.rb:412
def scale x, y=nil, z=1.0
  y = x unless y.present?
  Native.ofScale x.to_f, y.to_f, z.to_f
end
x (Numeric)

amount to scale in x

y (Numeric)

amount to scale in y

z (Numeric)

amount to scale in z

Scale all subsequent drawing.

Details Source

# lib/core/graphics.rb:412
def scale x, y=nil, z=1.0
  y = x unless y.present?
  Native.ofScale x.to_f, y.to_f, z.to_f
end

Details Source

# lib/core/graphics.rb:1067
def shape close=:closed
  begin_shape
  yield
  end_shape close==:closed
end
# Star
shape do
  vertex 50, 10
  vertex 40, 30
  vertex 10, 30
  vertex 40, 45
  vertex 20, 80
  vertex 50, 60
  vertex 80, 80
  vertex 60, 45
  vertex 90, 30
  vertex 60, 30
end
# Wave
fill false
shape :open do
  width.to_i.times do |x|
    vertex x, 50 + sin(x / 4.0) * 10
  end
end

Details Source

# lib/core/graphics.rb:1252
def smoothing smooth=nil
  @smoothing_enabled ||= false

  if smooth.present?
    @smoothing_enabled = smooth.to_bool
    @smoothing_enabled ? Native.ofEnableSmoothing : Native.ofDisableSmoothing
  else
    @smoothing_enabled
  end
end

Details Source

# lib/core/graphics.rb:1252
def smoothing smooth=nil
  @smoothing_enabled ||= false

  if smooth.present?
    @smoothing_enabled = smooth.to_bool
    @smoothing_enabled ? Native.ofEnableSmoothing : Native.ofDisableSmoothing
  else
    @smoothing_enabled
  end
end

Details Source

# lib/core/graphics.rb:1158
def sphere *args
  x = y = z = r = 0

  case args
  when Signature[[:x,:y,:z], :to_f]
    p, r = *args
    x, y, z = p.x, p.y, p.z

  when Signature[[:x,:y], :to_f]
    p, r = *args
    x, y = p.x, p.y

  when Signature[:to_f, :to_f, :to_f]
    x, y, r = *args

  when Signature[:to_f, :to_f, :to_f, :to_f]
    x, y, z, r = *args
  end

  Native.ofSphere x.to_f, y.to_f, z.to_f, r.to_f
end
# Wireframe Sphere
fill false
sphere 50, 50, 40

Details Source

# lib/core/graphics.rb:675
def sphere_resolution new_resolution=nil
  @sphere_resolution ||= 22 # TODO what is the default resolution

  if new_resolution.present?
    @sphere_resolution = new_resolution.to_i
    Native.ofSetSphereResolution @sphere_resolution
  else
    @sphere_resolution
  end
end
# Low resolution sphere
fill false
sphere_resolution 5
sphere 50, 50, 40
# Medium resolution sphere
fill false
sphere_resolution 12
sphere 50, 50, 40
# High resolution sphere
fill false
sphere_resolution 16
sphere 50, 50, 40

Details Source

# lib/core/graphics.rb:675
def sphere_resolution new_resolution=nil
  @sphere_resolution ||= 22 # TODO what is the default resolution

  if new_resolution.present?
    @sphere_resolution = new_resolution.to_i
    Native.ofSetSphereResolution @sphere_resolution
  else
    @sphere_resolution
  end
end
x (Numeric)

x coordinate of the top left corner

y (Numeric)

y coordinate of the top left corner

size (Numeric)

the width and height of the square

Draw a square.

Draw a square centered at x,y with size size.

Details Source

# lib/core/graphics.rb:342
def square *args
  x = y = z = s = 0

  case args
  when Signature[[:x,:y,:z], :to_f]
    pos, s = *args
    x, y, z = pos.x, pos.y, pos.z

  when Signature[[:x,:y], :to_f]
    pos, s = *args
    x, y = pos.x, pos.y

  when Signature[:to_f, :to_f, :to_f, :to_f]
    x, y, z, s = *args

  when Signature[:to_f, :to_f, :to_f]
    x, y, s = *args
  end

  rectangle x, y, z, s, s
end
# Single square
square 10, 10, 80
# Corner to corner
square 10, 10, 10
square 20, 20, 20
square 40, 40, 40
# Carefully placed squares
square 10, 50, 1
square 12, 50, 2
square 15, 50, 3
square 19, 50, 4
square 24, 50, 5
square 30, 50, 6
square 37, 50, 7
square 45, 50, 8
square 54, 50, 9
square 64, 50, 10
square 75, 50, 11

Details Source

# lib/core/graphics.rb:772
def style
  push_style
  yield
  pop_style
end
# Isolated style
fill true
color :white
circle 30, 30, 15

style do
  color :yellow
  fill false
  circle 70, 50, 15
end

circle 30, 70, 15
x (Numeric)

amount to move horizontally

y (Numeric)

amount to move vertically

z (Numeric)

amount to move in depth

Move all subsequent drawing.

Details Source

# lib/core/graphics.rb:376
def translate x, y, z=0.0
  Native.ofTranslate x.to_f, y.to_f, z.to_f
end
# Same circle shifted
circle 20, 50, 10
translate 60, 0
circle 20, 50, 10

Details Source

# lib/core/graphics.rb:817
def triangle *args
  x1 = y1 = z1 = x2 = y2 = z2 = x3 = y3 = z3 = 0

  equilateral_algorithm = proc { |x, y, z, r|
    a = Math::PI - Math::PI/3
    x1 = x + cos(Math::PI/2) * r
    y1 = y - sin(Math::PI/2) * r
    z1 = z
    x2 = x + cos(Math::PI/2 + a) * r
    y2 = y - sin(Math::PI/2 + a) * r
    z2 = z
    x3 = x + cos(Math::PI/2 + 2*a) * r
    y3 = y - sin(Math::PI/2 + 2*a) * r
    z3 = z
  }

  isosceles_algorithm = proc { |x, y, z, r, a|
    a = a.to_rad
    h = (r+r/2) / sin(a/2) # sine law, bitches
    x1 = x + cos(Math::PI/2) * r
    y1 = y - sin(Math::PI/2) * r
    z1 = z
    x2 = x1 + cos(Math::PI + (Math::PI - a/2)) * h
    y2 = y1 - sin(Math::PI + (Math::PI - a/2)) * h
    z2 = z
    x3 = x1 + cos(a/2 - Math::PI) * h
    y3 = y1 - sin(a/2 - Math::PI) * h
    z3 = z
  }

  case args
  # equilateral triangle
  when Signature[[:x,:y,:z], :to_f]
    point, r = *args
    equilateral_algorithm.call point.x, point.y, point.z, r
    
  when Signature[[:x,:y], :to_f]
    point, r = *args
    equilateral_algorithm.call point.x, point.y, 0, r
    
  when Signature[:to_f, :to_f, :to_f]
    x, y, r = *args
    equilateral_algorithm.call x, y, 0, r

  # isosceles triangle
  when Signature[[:x,:y,:z], :to_f, :to_f]
    point, r, a = *args
    isosceles_algorithm.call point.x, point.y, point.z, r, a

  when Signature[[:x,:y], :to_f, :to_f]
    point, r, a = *args
    isosceles_algorithm.call point.x, point.y, 0, r, a

  when Signature[:to_f, :to_f, :to_f, :to_f]
    x, y, r, a = *args
    isosceles_algorithm.call x, y, 0, r, a

  # scalene triangle
  when Signature[[:x,:y,:z], [:x,:y,:z], [:x,:y,:z]]
    a, b, c = *args
    x1, y1, z1 = a.x, a.y, a.z
    x2, y2, z2 = b.x, b.y, b.z
    x3, y3, z3 = c.x, c.y, c.z

  when Signature[[:x,:y], [:x,:y], [:x,:y]]
    a, b, c = *args
    x1, y1 = a.x, a.y
    x2, y2 = b.x, b.y
    x3, y3 = c.x, c.y

  when Signature[:to_f, :to_f, :to_f, :to_f, :to_f, :to_f, :to_f, :to_f, :to_f]
    x1, y1, z1, x2, y2, z2, x3, y3, z3 = *args

  when Signature[:to_f, :to_f, :to_f, :to_f, :to_f, :to_f]
    x1, y1, x2, y2, x3, y3 = *args
  end

  Native.ofTriangle x1.to_f, y1.to_f, z1.to_f, x2.to_f, y2.to_f, z2.to_f, x3.to_f, y3.to_f, z3.to_f
end
# Triangle
triangle 50, 50, 30
# Equilateral triangles
fill false
triangle 50, 50, 50
triangle 50, 50, 40
triangle 50, 50, 30
triangle 50, 50, 20
triangle 50, 50, 10

Details Source

# lib/core/graphics.rb:817
def triangle *args
  x1 = y1 = z1 = x2 = y2 = z2 = x3 = y3 = z3 = 0

  equilateral_algorithm = proc { |x, y, z, r|
    a = Math::PI - Math::PI/3
    x1 = x + cos(Math::PI/2) * r
    y1 = y - sin(Math::PI/2) * r
    z1 = z
    x2 = x + cos(Math::PI/2 + a) * r
    y2 = y - sin(Math::PI/2 + a) * r
    z2 = z
    x3 = x + cos(Math::PI/2 + 2*a) * r
    y3 = y - sin(Math::PI/2 + 2*a) * r
    z3 = z
  }

  isosceles_algorithm = proc { |x, y, z, r, a|
    a = a.to_rad
    h = (r+r/2) / sin(a/2) # sine law, bitches
    x1 = x + cos(Math::PI/2) * r
    y1 = y - sin(Math::PI/2) * r
    z1 = z
    x2 = x1 + cos(Math::PI + (Math::PI - a/2)) * h
    y2 = y1 - sin(Math::PI + (Math::PI - a/2)) * h
    z2 = z
    x3 = x1 + cos(a/2 - Math::PI) * h
    y3 = y1 - sin(a/2 - Math::PI) * h
    z3 = z
  }

  case args
  # equilateral triangle
  when Signature[[:x,:y,:z], :to_f]
    point, r = *args
    equilateral_algorithm.call point.x, point.y, point.z, r
    
  when Signature[[:x,:y], :to_f]
    point, r = *args
    equilateral_algorithm.call point.x, point.y, 0, r
    
  when Signature[:to_f, :to_f, :to_f]
    x, y, r = *args
    equilateral_algorithm.call x, y, 0, r

  # isosceles triangle
  when Signature[[:x,:y,:z], :to_f, :to_f]
    point, r, a = *args
    isosceles_algorithm.call point.x, point.y, point.z, r, a

  when Signature[[:x,:y], :to_f, :to_f]
    point, r, a = *args
    isosceles_algorithm.call point.x, point.y, 0, r, a

  when Signature[:to_f, :to_f, :to_f, :to_f]
    x, y, r, a = *args
    isosceles_algorithm.call x, y, 0, r, a

  # scalene triangle
  when Signature[[:x,:y,:z], [:x,:y,:z], [:x,:y,:z]]
    a, b, c = *args
    x1, y1, z1 = a.x, a.y, a.z
    x2, y2, z2 = b.x, b.y, b.z
    x3, y3, z3 = c.x, c.y, c.z

  when Signature[[:x,:y], [:x,:y], [:x,:y]]
    a, b, c = *args
    x1, y1 = a.x, a.y
    x2, y2 = b.x, b.y
    x3, y3 = c.x, c.y

  when Signature[:to_f, :to_f, :to_f, :to_f, :to_f, :to_f, :to_f, :to_f, :to_f]
    x1, y1, z1, x2, y2, z2, x3, y3, z3 = *args

  when Signature[:to_f, :to_f, :to_f, :to_f, :to_f, :to_f]
    x1, y1, x2, y2, x3, y3 = *args
  end

  Native.ofTriangle x1.to_f, y1.to_f, z1.to_f, x2.to_f, y2.to_f, z2.to_f, x3.to_f, y3.to_f, z3.to_f
end

Details Source

# lib/core/graphics.rb:817
def triangle *args
  x1 = y1 = z1 = x2 = y2 = z2 = x3 = y3 = z3 = 0

  equilateral_algorithm = proc { |x, y, z, r|
    a = Math::PI - Math::PI/3
    x1 = x + cos(Math::PI/2) * r
    y1 = y - sin(Math::PI/2) * r
    z1 = z
    x2 = x + cos(Math::PI/2 + a) * r
    y2 = y - sin(Math::PI/2 + a) * r
    z2 = z
    x3 = x + cos(Math::PI/2 + 2*a) * r
    y3 = y - sin(Math::PI/2 + 2*a) * r
    z3 = z
  }

  isosceles_algorithm = proc { |x, y, z, r, a|
    a = a.to_rad
    h = (r+r/2) / sin(a/2) # sine law, bitches
    x1 = x + cos(Math::PI/2) * r
    y1 = y - sin(Math::PI/2) * r
    z1 = z
    x2 = x1 + cos(Math::PI + (Math::PI - a/2)) * h
    y2 = y1 - sin(Math::PI + (Math::PI - a/2)) * h
    z2 = z
    x3 = x1 + cos(a/2 - Math::PI) * h
    y3 = y1 - sin(a/2 - Math::PI) * h
    z3 = z
  }

  case args
  # equilateral triangle
  when Signature[[:x,:y,:z], :to_f]
    point, r = *args
    equilateral_algorithm.call point.x, point.y, point.z, r
    
  when Signature[[:x,:y], :to_f]
    point, r = *args
    equilateral_algorithm.call point.x, point.y, 0, r
    
  when Signature[:to_f, :to_f, :to_f]
    x, y, r = *args
    equilateral_algorithm.call x, y, 0, r

  # isosceles triangle
  when Signature[[:x,:y,:z], :to_f, :to_f]
    point, r, a = *args
    isosceles_algorithm.call point.x, point.y, point.z, r, a

  when Signature[[:x,:y], :to_f, :to_f]
    point, r, a = *args
    isosceles_algorithm.call point.x, point.y, 0, r, a

  when Signature[:to_f, :to_f, :to_f, :to_f]
    x, y, r, a = *args
    isosceles_algorithm.call x, y, 0, r, a

  # scalene triangle
  when Signature[[:x,:y,:z], [:x,:y,:z], [:x,:y,:z]]
    a, b, c = *args
    x1, y1, z1 = a.x, a.y, a.z
    x2, y2, z2 = b.x, b.y, b.z
    x3, y3, z3 = c.x, c.y, c.z

  when Signature[[:x,:y], [:x,:y], [:x,:y]]
    a, b, c = *args
    x1, y1 = a.x, a.y
    x2, y2 = b.x, b.y
    x3, y3 = c.x, c.y

  when Signature[:to_f, :to_f, :to_f, :to_f, :to_f, :to_f, :to_f, :to_f, :to_f]
    x1, y1, z1, x2, y2, z2, x3, y3, z3 = *args

  when Signature[:to_f, :to_f, :to_f, :to_f, :to_f, :to_f]
    x1, y1, x2, y2, x3, y3 = *args
  end

  Native.ofTriangle x1.to_f, y1.to_f, z1.to_f, x2.to_f, y2.to_f, z2.to_f, x3.to_f, y3.to_f, z3.to_f
end
# Isosceles triangles
fill false
triangle 50, 50, 30, 170
triangle 50, 50, 30, 150
triangle 50, 50, 30, 130
triangle 50, 50, 30, 110
triangle 50, 50, 30, 90

Details Source

# lib/core/graphics.rb:817
def triangle *args
  x1 = y1 = z1 = x2 = y2 = z2 = x3 = y3 = z3 = 0

  equilateral_algorithm = proc { |x, y, z, r|
    a = Math::PI - Math::PI/3
    x1 = x + cos(Math::PI/2) * r
    y1 = y - sin(Math::PI/2) * r
    z1 = z
    x2 = x + cos(Math::PI/2 + a) * r
    y2 = y - sin(Math::PI/2 + a) * r
    z2 = z
    x3 = x + cos(Math::PI/2 + 2*a) * r
    y3 = y - sin(Math::PI/2 + 2*a) * r
    z3 = z
  }

  isosceles_algorithm = proc { |x, y, z, r, a|
    a = a.to_rad
    h = (r+r/2) / sin(a/2) # sine law, bitches
    x1 = x + cos(Math::PI/2) * r
    y1 = y - sin(Math::PI/2) * r
    z1 = z
    x2 = x1 + cos(Math::PI + (Math::PI - a/2)) * h
    y2 = y1 - sin(Math::PI + (Math::PI - a/2)) * h
    z2 = z
    x3 = x1 + cos(a/2 - Math::PI) * h
    y3 = y1 - sin(a/2 - Math::PI) * h
    z3 = z
  }

  case args
  # equilateral triangle
  when Signature[[:x,:y,:z], :to_f]
    point, r = *args
    equilateral_algorithm.call point.x, point.y, point.z, r
    
  when Signature[[:x,:y], :to_f]
    point, r = *args
    equilateral_algorithm.call point.x, point.y, 0, r
    
  when Signature[:to_f, :to_f, :to_f]
    x, y, r = *args
    equilateral_algorithm.call x, y, 0, r

  # isosceles triangle
  when Signature[[:x,:y,:z], :to_f, :to_f]
    point, r, a = *args
    isosceles_algorithm.call point.x, point.y, point.z, r, a

  when Signature[[:x,:y], :to_f, :to_f]
    point, r, a = *args
    isosceles_algorithm.call point.x, point.y, 0, r, a

  when Signature[:to_f, :to_f, :to_f, :to_f]
    x, y, r, a = *args
    isosceles_algorithm.call x, y, 0, r, a

  # scalene triangle
  when Signature[[:x,:y,:z], [:x,:y,:z], [:x,:y,:z]]
    a, b, c = *args
    x1, y1, z1 = a.x, a.y, a.z
    x2, y2, z2 = b.x, b.y, b.z
    x3, y3, z3 = c.x, c.y, c.z

  when Signature[[:x,:y], [:x,:y], [:x,:y]]
    a, b, c = *args
    x1, y1 = a.x, a.y
    x2, y2 = b.x, b.y
    x3, y3 = c.x, c.y

  when Signature[:to_f, :to_f, :to_f, :to_f, :to_f, :to_f, :to_f, :to_f, :to_f]
    x1, y1, z1, x2, y2, z2, x3, y3, z3 = *args

  when Signature[:to_f, :to_f, :to_f, :to_f, :to_f, :to_f]
    x1, y1, x2, y2, x3, y3 = *args
  end

  Native.ofTriangle x1.to_f, y1.to_f, z1.to_f, x2.to_f, y2.to_f, z2.to_f, x3.to_f, y3.to_f, z3.to_f
end

Details Source

# lib/core/graphics.rb:817
def triangle *args
  x1 = y1 = z1 = x2 = y2 = z2 = x3 = y3 = z3 = 0

  equilateral_algorithm = proc { |x, y, z, r|
    a = Math::PI - Math::PI/3
    x1 = x + cos(Math::PI/2) * r
    y1 = y - sin(Math::PI/2) * r
    z1 = z
    x2 = x + cos(Math::PI/2 + a) * r
    y2 = y - sin(Math::PI/2 + a) * r
    z2 = z
    x3 = x + cos(Math::PI/2 + 2*a) * r
    y3 = y - sin(Math::PI/2 + 2*a) * r
    z3 = z
  }

  isosceles_algorithm = proc { |x, y, z, r, a|
    a = a.to_rad
    h = (r+r/2) / sin(a/2) # sine law, bitches
    x1 = x + cos(Math::PI/2) * r
    y1 = y - sin(Math::PI/2) * r
    z1 = z
    x2 = x1 + cos(Math::PI + (Math::PI - a/2)) * h
    y2 = y1 - sin(Math::PI + (Math::PI - a/2)) * h
    z2 = z
    x3 = x1 + cos(a/2 - Math::PI) * h
    y3 = y1 - sin(a/2 - Math::PI) * h
    z3 = z
  }

  case args
  # equilateral triangle
  when Signature[[:x,:y,:z], :to_f]
    point, r = *args
    equilateral_algorithm.call point.x, point.y, point.z, r
    
  when Signature[[:x,:y], :to_f]
    point, r = *args
    equilateral_algorithm.call point.x, point.y, 0, r
    
  when Signature[:to_f, :to_f, :to_f]
    x, y, r = *args
    equilateral_algorithm.call x, y, 0, r

  # isosceles triangle
  when Signature[[:x,:y,:z], :to_f, :to_f]
    point, r, a = *args
    isosceles_algorithm.call point.x, point.y, point.z, r, a

  when Signature[[:x,:y], :to_f, :to_f]
    point, r, a = *args
    isosceles_algorithm.call point.x, point.y, 0, r, a

  when Signature[:to_f, :to_f, :to_f, :to_f]
    x, y, r, a = *args
    isosceles_algorithm.call x, y, 0, r, a

  # scalene triangle
  when Signature[[:x,:y,:z], [:x,:y,:z], [:x,:y,:z]]
    a, b, c = *args
    x1, y1, z1 = a.x, a.y, a.z
    x2, y2, z2 = b.x, b.y, b.z
    x3, y3, z3 = c.x, c.y, c.z

  when Signature[[:x,:y], [:x,:y], [:x,:y]]
    a, b, c = *args
    x1, y1 = a.x, a.y
    x2, y2 = b.x, b.y
    x3, y3 = c.x, c.y

  when Signature[:to_f, :to_f, :to_f, :to_f, :to_f, :to_f, :to_f, :to_f, :to_f]
    x1, y1, z1, x2, y2, z2, x3, y3, z3 = *args

  when Signature[:to_f, :to_f, :to_f, :to_f, :to_f, :to_f]
    x1, y1, x2, y2, x3, y3 = *args
  end

  Native.ofTriangle x1.to_f, y1.to_f, z1.to_f, x2.to_f, y2.to_f, z2.to_f, x3.to_f, y3.to_f, z3.to_f
end
# Scalene triangle
fill false
triangle 75, 60, 50, 50, 80, 10

Details Source

# lib/core/graphics.rb:817
def triangle *args
  x1 = y1 = z1 = x2 = y2 = z2 = x3 = y3 = z3 = 0

  equilateral_algorithm = proc { |x, y, z, r|
    a = Math::PI - Math::PI/3
    x1 = x + cos(Math::PI/2) * r
    y1 = y - sin(Math::PI/2) * r
    z1 = z
    x2 = x + cos(Math::PI/2 + a) * r
    y2 = y - sin(Math::PI/2 + a) * r
    z2 = z
    x3 = x + cos(Math::PI/2 + 2*a) * r
    y3 = y - sin(Math::PI/2 + 2*a) * r
    z3 = z
  }

  isosceles_algorithm = proc { |x, y, z, r, a|
    a = a.to_rad
    h = (r+r/2) / sin(a/2) # sine law, bitches
    x1 = x + cos(Math::PI/2) * r
    y1 = y - sin(Math::PI/2) * r
    z1 = z
    x2 = x1 + cos(Math::PI + (Math::PI - a/2)) * h
    y2 = y1 - sin(Math::PI + (Math::PI - a/2)) * h
    z2 = z
    x3 = x1 + cos(a/2 - Math::PI) * h
    y3 = y1 - sin(a/2 - Math::PI) * h
    z3 = z
  }

  case args
  # equilateral triangle
  when Signature[[:x,:y,:z], :to_f]
    point, r = *args
    equilateral_algorithm.call point.x, point.y, point.z, r
    
  when Signature[[:x,:y], :to_f]
    point, r = *args
    equilateral_algorithm.call point.x, point.y, 0, r
    
  when Signature[:to_f, :to_f, :to_f]
    x, y, r = *args
    equilateral_algorithm.call x, y, 0, r

  # isosceles triangle
  when Signature[[:x,:y,:z], :to_f, :to_f]
    point, r, a = *args
    isosceles_algorithm.call point.x, point.y, point.z, r, a

  when Signature[[:x,:y], :to_f, :to_f]
    point, r, a = *args
    isosceles_algorithm.call point.x, point.y, 0, r, a

  when Signature[:to_f, :to_f, :to_f, :to_f]
    x, y, r, a = *args
    isosceles_algorithm.call x, y, 0, r, a

  # scalene triangle
  when Signature[[:x,:y,:z], [:x,:y,:z], [:x,:y,:z]]
    a, b, c = *args
    x1, y1, z1 = a.x, a.y, a.z
    x2, y2, z2 = b.x, b.y, b.z
    x3, y3, z3 = c.x, c.y, c.z

  when Signature[[:x,:y], [:x,:y], [:x,:y]]
    a, b, c = *args
    x1, y1 = a.x, a.y
    x2, y2 = b.x, b.y
    x3, y3 = c.x, c.y

  when Signature[:to_f, :to_f, :to_f, :to_f, :to_f, :to_f, :to_f, :to_f, :to_f]
    x1, y1, z1, x2, y2, z2, x3, y3, z3 = *args

  when Signature[:to_f, :to_f, :to_f, :to_f, :to_f, :to_f]
    x1, y1, x2, y2, x3, y3 = *args
  end

  Native.ofTriangle x1.to_f, y1.to_f, z1.to_f, x2.to_f, y2.to_f, z2.to_f, x3.to_f, y3.to_f, z3.to_f
end

Details Source

# lib/core/graphics.rb:1077
def vertex *args
  x = y = z = 0

  case args
  when Signature[[:x,:y,:z]]
    x, y, z, = args.first.x, args.first.y, args.first.z

  when Signature[[:x,:y]]
    x, y = args.first.x, args.first.y

  when Signature[:to_f, :to_f]
    x, y = *args

  when Signature[:to_f, :to_f, :to_f]
    x, y, z = *args
  end

  Native.ofVertex x.to_f, y.to_f, z.to_f
end

Details Source

# lib/core/graphics.rb:1232
def view
  push_view
  yield
  pop_view
end

Details Source

# lib/core/graphics.rb:1238
def viewport x=0.0, y=0.0, w=0.0, h=0.0, invert_y=true
  Native.ofViewport x.to_f, y.to_f, w.to_f, h.to_f, invert_y.to_bool
end

Details Source

# lib/core/graphics.rb:1242
def viewport_height
  Native.ofGetViewportHeight
end

Details Source

# lib/core/graphics.rb:1246
def viewport_width
  Native.ofGetViewportWidth
end

Images

x (Numeric)
y (Numeric)
width (Numeric)
height (Numeric)

Take a screenshot of the sketch

Details Source

# lib/core/images.rb:67
def grab_screen *args
  i = Image.new
  i.grab_screen *args
  i
end
# Taking a screenshot on mouse click
draw do
  fill false
  circle 50 + sin(time) * 50, 50, 20
end

mouse_down do
  grab_screen.save "~/Desktop/moving-circle.png"
end
file (#to_s)

The image file to load

x (Numeric)

the x coordinate to draw the image at

y (Numeric)

the y coordinate to draw the image at

Draw an image.

Details Source

# lib/core/images.rb:41
def image *args
  @cached_images ||= {}

  file = args.shift
  @cached_images[file] ||= Image.new file
  @cached_images[file].draw *args
end
# Drawing one image
image "docs/actual_zajal.jpg", 10, 10
# Drawing multiple images
image "docs/actual_zajal.jpg", 0, 0
image "docs/actual_zajal.jpg", 50, 50
image "docs/actual_zajal.jpg", -50, 70
image "docs/actual_zajal.jpg", 80, -40
file (#to_s)

The image file to load

x (Numeric)

the x coordinate to draw the image at

y (Numeric)

the y coordinate to draw the image at

width (Numeric)

the width to draw the image at

height (Numeric)

the height to draw the image at

Draw an image.

Details Source

# lib/core/images.rb:41
def image *args
  @cached_images ||= {}

  file = args.shift
  @cached_images[file] ||= Image.new file
  @cached_images[file].draw *args
end
# Drawing one smaller image
image "docs/actual_zajal.jpg", 20, 20, 25, 25
# Drawing one larger image
image "docs/actual_zajal.jpg", -150, -40, 250, 250
# Drawing many smaller images
4.times do |i|
  image "docs/actual_zajal.jpg", i * 25, 0, 25, 25 * (i+1)
end

Details Source

# lib/core/images.rb:205
def crop x, y, w, h
  Native.ofimage_crop @pointer, x.to_i, y.to_i, w.to_i, h.to_i
end
x (Numeric)

distance from the left to start drawing

y (Numeric)

distance from the top to start drawing

Draw the image.

Details Source

# lib/core/images.rb:193
def draw x, y, w=nil, h=nil
  if not w.present?
    w = width
    h = height
  elsif not h.present?
    w = width * w
    h = height * w
  end

  Native.ofimage_draw @pointer, x.to_f, y.to_f, 0.0, w.to_f, h.to_f
end
x (Numeric)

distance from the left to start drawing

y (Numeric)

distance from the top to start drawing

size (Numeric)

proportional scale of the drawn image

Draw the image.

Details Source

# lib/core/images.rb:193
def draw x, y, w=nil, h=nil
  if not w.present?
    w = width
    h = height
  elsif not h.present?
    w = width * w
    h = height * w
  end

  Native.ofimage_draw @pointer, x.to_f, y.to_f, 0.0, w.to_f, h.to_f
end
x (Numeric)

distance from the left to start drawing

y (Numeric)

distance from the top to start drawing

width (Numeric)

width of the drawn image

height (Numeric)

height of the drawn image

Draw the image.

Details Source

# lib/core/images.rb:193
def draw x, y, w=nil, h=nil
  if not w.present?
    w = width
    h = height
  elsif not h.present?
    w = width * w
    h = height * w
  end

  Native.ofimage_draw @pointer, x.to_f, y.to_f, 0.0, w.to_f, h.to_f
end

Details Source

# lib/core/images.rb:128
def grab_screen *args
  x, y = 0, 0
  w, h = Sketch.current.width, Sketch.current.height

  case args
  when Signature[:to_i, :to_i] # x, y
    x, y = *args
  when Signature[:to_i, :to_i, :to_i, :to_i] # x, y, w, h
    x, y, w, h = *args
  end

  raise "Requested screenshot bigger than sketch!" if x + w > Sketch.current.width or y + h > Sketch.current.height

  Native.ofimage_grabScreen @pointer, x.to_i, y.to_i, w.to_i, h.to_i
end

Details Source

# lib/core/images.rb:128
def grab_screen *args
  x, y = 0, 0
  w, h = Sketch.current.width, Sketch.current.height

  case args
  when Signature[:to_i, :to_i] # x, y
    x, y = *args
  when Signature[:to_i, :to_i, :to_i, :to_i] # x, y, w, h
    x, y, w, h = *args
  end

  raise "Requested screenshot bigger than sketch!" if x + w > Sketch.current.width or y + h > Sketch.current.height

  Native.ofimage_grabScreen @pointer, x.to_i, y.to_i, w.to_i, h.to_i
end

Details Source

# lib/core/images.rb:128
def grab_screen *args
  x, y = 0, 0
  w, h = Sketch.current.width, Sketch.current.height

  case args
  when Signature[:to_i, :to_i] # x, y
    x, y = *args
  when Signature[:to_i, :to_i, :to_i, :to_i] # x, y, w, h
    x, y, w, h = *args
  end

  raise "Requested screenshot bigger than sketch!" if x + w > Sketch.current.width or y + h > Sketch.current.height

  Native.ofimage_grabScreen @pointer, x.to_i, y.to_i, w.to_i, h.to_i
end

Details Source

# lib/core/images.rb:173
def height
  Native.ofimage_getHeight @pointer
end

Create a new image object.

Details Source

# lib/core/images.rb:90
def initialize *args
  @pointer = Native.ofimage_new

  case args
  when Signature[]
  when Signature[:to_s]
    load args.first
  when Signature[:to_i, :to_i]
    resize *args
  else
    raise ArgumentError, args.inspect
  end
end
filename (#to_s)

name of the file to load

Create a new image object.

Details Source

# lib/core/images.rb:90
def initialize *args
  @pointer = Native.ofimage_new

  case args
  when Signature[]
  when Signature[:to_s]
    load args.first
  when Signature[:to_i, :to_i]
    resize *args
  else
    raise ArgumentError, args.inspect
  end
end
# Loading an image
img = Image.new "docs/actual_zajal.jpg"
img.draw 13, 13
width (Numeric)

width of the new image

width (Numeric)

height of the new image

Create a new image object.

Details Source

# lib/core/images.rb:90
def initialize *args
  @pointer = Native.ofimage_new

  case args
  when Signature[]
  when Signature[:to_s]
    load args.first
  when Signature[:to_i, :to_i]
    resize *args
  else
    raise ArgumentError, args.inspect
  end
end
filename (#to_s)

name of the file to load

Load an image off the disk.

Load an image off the disk

This will resize the image to filename’s size and load filename’s pixels

Details Source

# lib/core/images.rb:112
def load filename
  path = File.expand_path(filename.to_s)
  Native.ofimage_loadImage @pointer, path.to_ptr
end

Mirror the image.

Details Source

# lib/core/images.rb:221
def mirror direction=:horizontal
        d = {
horizontal: [false, true],
vertical:[true, false],
diagonal:[true, true]

Mirror the image.

Details Source

# lib/core/images.rb:221
def mirror direction=:horizontal
        d = {
horizontal: [false, true],
vertical:[true, false],
diagonal:[true, true]
size (Numeric)

new width and height of the image

Resize the image.

Details Source

# lib/core/images.rb:162
def resize w, h=nil
  h = w unless h.present?
  Native.ofimage_resize @pointer, w.to_i, h.to_i
end
# Resizing an image
img = Image.new "docs/actual_zajal.jpg"
img.resize 20
img.draw 10, 10
width (Numeric)

new width of the image

height (Numeric)

new height of the image

Resize the image.

Details Source

# lib/core/images.rb:162
def resize w, h=nil
  h = w unless h.present?
  Native.ofimage_resize @pointer, w.to_i, h.to_i
end
# Resizing an image
img = Image.new "docs/actual_zajal.jpg"
img.resize 100, 50
img.draw 0, 0

Rotate the image by 90 degrees.

Details Source

# lib/core/images.rb:213
def rotate times=1
  Native.ofimage_rotate90 @pointer, times.to_i
end

Rotate the image by 90 degrees.

Details Source

# lib/core/images.rb:213
def rotate times=1
  Native.ofimage_rotate90 @pointer, times.to_i
end
path (#to_s)

location on disk to save file

quality (Symbol)

location on disk to save file

Save the image to the disk.

Details Source

# lib/core/images.rb:121
def save path, quality=:best
  Native.ofimage_saveImage @pointer, File.expand_path(path.to_s).to_ptr, quality
end

Details Source

# lib/core/images.rb:168
def width
  Native.ofimage_getWidth @pointer
end

Typography

rootFont mostly a wrapper for ofTrueTypeFont in openFrameworks.

message (#to_s)

the text to write

Render text using Zajal’s built in font.

Draw message in top left corner. Calling this method multiple times will stack the text, making #text useful for quick debugging.

Details Source

# lib/core/typography.rb:101
def text message, x=nil, y=nil
  @basic_font ||= Font.new File.join(File.dirname(__FILE__), "bin", "gohu.ttf"), 8, antialiased:false

  if x.present?
    x, y = x.to_f, y.to_f
  else
    @stacked_text_x ||= 1.0
    @stacked_text_y ||= 10.0
    
    x, y = @stacked_text_x, @stacked_text_y

    @stacked_text_y += 10.0
  end

  @basic_font.draw message.to_s, x, y
end
text "Hello, world!"
text "Hello, world!"
text "This text..."
text "...can stack!"
message (#to_s)

the text to write

x (Numeric)

the x coordinate to start writing text at

y (Numeric)

the y coordinate to start writing text at

Render text using Zajal’s built in font.

Draw message at a specific location on screen. The first letter will be placed at x,y and the text will continue to the left.

Details Source

# lib/core/typography.rb:101
def text message, x=nil, y=nil
  @basic_font ||= Font.new File.join(File.dirname(__FILE__), "bin", "gohu.ttf"), 8, antialiased:false

  if x.present?
    x, y = x.to_f, y.to_f
  else
    @stacked_text_x ||= 1.0
    @stacked_text_y ||= 10.0
    
    x, y = @stacked_text_x, @stacked_text_y

    @stacked_text_y += 10.0
  end

  @basic_font.draw message.to_s, x, y
end
text "Hello, world!", 10, 50
text (#to_s)

the text to draw

x (Numeric)

x coordinate of to start drawing text at

y (Numeric)

y coordinate of to start drawing text at

Draw text using this font’s glyphs.

Details Source

# lib/core/typography.rb:55
def draw text, x, y
  Native.oftruetypefont_drawString @pointer, text.to_s.to_ptr, x.to_f, y.to_f
end
fnt = Font.new 'Georgia', 16
fnt.draw "Nice serifs!", 10, 60
file (#to_s)

the file to load

size (Numeric)

the size of the font

options (Hash)

additional options

Details Source

# lib/core/typography.rb:17
def initialize file, size, options={}

Details Source

# lib/core/typography.rb:67
def inspect
  "<#{@name} #{size}pt>"
end

Details Source

# lib/core/typography.rb:63
def line_height
  Native.oftruetypefont_getLineHeight @pointer
end

Details Source

# lib/core/typography.rb:59
def size
  Native.oftruetypefont_getSize @pointer
end

Mathematics

Details Source

# lib/core/mathematics.rb:14
def noise x=nil, y=0.0, z=0.0, w=0.0
  x = time unless x.present?
  Native.ofNoise x.to_f, y.to_f, z.to_f, w.to_f
end

Details Source

# lib/core/mathematics.rb:14
def noise x=nil, y=0.0, z=0.0, w=0.0
  x = time unless x.present?
  Native.ofNoise x.to_f, y.to_f, z.to_f, w.to_f
end

Details Source

# lib/core/mathematics.rb:14
def noise x=nil, y=0.0, z=0.0, w=0.0
  x = time unless x.present?
  Native.ofNoise x.to_f, y.to_f, z.to_f, w.to_f
end

Details Source

# lib/core/mathematics.rb:14
def noise x=nil, y=0.0, z=0.0, w=0.0
  x = time unless x.present?
  Native.ofNoise x.to_f, y.to_f, z.to_f, w.to_f
end

Details Source

# lib/core/mathematics.rb:14
def noise x=nil, y=0.0, z=0.0, w=0.0
  x = time unless x.present?
  Native.ofNoise x.to_f, y.to_f, z.to_f, w.to_f
end

Details Source

# lib/core/mathematics.rb:29
def signed_noise x=nil?, y=0.0, z=0.0, w=0.0
  x = time unless x.present?
  Native.ofSignedNoise x.to_f, y.to_f, z.to_f, w.to_f
end

Details Source

# lib/core/mathematics.rb:29
def signed_noise x=nil?, y=0.0, z=0.0, w=0.0
  x = time unless x.present?
  Native.ofSignedNoise x.to_f, y.to_f, z.to_f, w.to_f
end

Details Source

# lib/core/mathematics.rb:29
def signed_noise x=nil?, y=0.0, z=0.0, w=0.0
  x = time unless x.present?
  Native.ofSignedNoise x.to_f, y.to_f, z.to_f, w.to_f
end

Details Source

# lib/core/mathematics.rb:29
def signed_noise x=nil?, y=0.0, z=0.0, w=0.0
  x = time unless x.present?
  Native.ofSignedNoise x.to_f, y.to_f, z.to_f, w.to_f
end

Details Source

# lib/core/mathematics.rb:29
def signed_noise x=nil?, y=0.0, z=0.0, w=0.0
  x = time unless x.present?
  Native.ofSignedNoise x.to_f, y.to_f, z.to_f, w.to_f
end