2D Shapes

v0.1.7
circle(x, y, d);

Draw a circle on the screen.

Arguments

xThe x position of the circle
yThe y position of the circle
dThe diameter of the circle

function setup()
  createWindow(200, 200);
end

function draw()
  background('purple');
 
  circle(100, 100, 32);
end

Output

See circle in shapes.h
ellipse(x, y, w, h);

Draw an ellipse on the screen.

Arguments

xThe x position of the ellipse
yThe y position of the ellipse
wThe width diameter of the ellipse
hThe height diameter of the ellipse

function setup()
  createWindow(200, 200);
end

function draw()
  background('purple');
 
  ellipse(100, 100, 64, 32);
end

Output

See ellipse in shapes.h
rect(x, y, w, h);

Draw a rectangle on the screen.

Arguments

xThe x position of the rectangle
yThe y position of the rectangle
wThe width
hThe height

function setup()
  createWindow(200, 200);
end

function draw()
  background('purple');

 -- rectangle near the top-left corner
  rect(30, 30, 120, 80); 
end

Output

See rect in shapes.h
square(x, y, s);

Draw a square on the screen.

Arguments

xThe x position of the square
yThe y position of the square
sThe size of the square

function setup()
  createWindow(200, 200);
end

function draw()
  background('purple');

  -- square near the top-left corner
  rect(30, 20, 64);
end

Output

See square in shapes.h
line(x1, y1, x2, y2);

Draw a line on the screen.

Arguments

x1The x position of the first point
y1The y position of the first point
x2The x position of the second point
y2The y position of the second point

function setup()
  createWindow(200, 200);
end

function draw()
  background('purple');
  stroke('white');

  -- diagonal line from top-left to mouse pointer
  line(0, 0, mouseX, mouseY);
end

Output

See line in shapes.h
arc(x, y, w, h, start, stop);

Draw an arc on the screen.

Arguments

xThe x position of the arc
yThe y position of the arc
wThe width of the arc
hThe height of the arc
startThe angle to start the arc
stopThe angle to stop the arc

function setup()
    createWindow(200, 200);
end

function draw()
    background('purple');
    
    local angle = map(mouseX, 0, width, PI, TWO_PI);
    arc(100, 100, 64, 64, 0, angle);
end

Output

See arc in shapes.h
point(x, y);

Draw a point on the screen.

Arguments

xThe point
yThe point

Use stroke to set the point's color, and use strokeWeight to set the point's radius.

Setting the color with fill will not affect point

function setup()
    createWindow(200, 200);
end

function draw()
    background('purple');

    strokeWeight(4);
    stroke('white');

    point(width/2, height/2);
end

Output

See point in shapes.h
quad(x1, y1, x2, y2, x3, y3, x4, y4);

Draw a quad on the screen.

Arguments

x1The x position of the first point
y1The y position of the first point
x2The x position of the second point
y2The y position of the second point
x3The x position of the third point
y3The y position of the third point
x4The x position of the fourth point
y4The y position of the fourth point

function setup()
  createWindow(200, 200);
end

function draw()
  background('purple');

  quad(30, 60, 160, 50, 130, 165, 40, 145);
end

Output

See quad in shapes.h
triangle(x1, y1, x2, y2, x3, y3);

Draw a triangle on the screen.

Arguments

x1The x position of the first point
y1The y position of the first point
x2The x position of the second point
y2The y position of the second point
x3The x position of the third point
y3The y position of the third point

function setup()
    createWindow(200, 200);
end

function draw()
    background('purple');

    triangle(50, 150, 100, 50, 150, 150);
end

Output

See triangle in shapes.h
beginShape([mode]);

Begin adding vertices to a custom shape.

Arguments

[mode]The opengl shape mode LINES, POINTS, QUADS, TRIANGLES, TRIANGLE_FAN , default is LINES

The mode param is not completely implemented in lu5-wasm

function setup()
  createWindow(300, 300);
end

function draw()
  background('purple');

  beginShape();
    vertex(150,  50);
    vertex(170, 130);
    vertex(250, 130);
    vertex(185, 170);
    vertex(210, 250);
    vertex(150, 200);
    vertex( 90, 250);
    vertex(115, 170);
    vertex( 50, 130);
    vertex(130, 130);
  endShape();
end

Output

See beginShape in shapes.h
vertex(x, y);

Add a vertex to a custom shape.

Arguments

xThe x position
yThe y position

beginShape must be called prior.

See vertex in shapes.h
endShape();

Close the custom shape.
beginShape must be called prior.

See endShape in shapes.h