1-4 More Shapes

p5.js Online Help Reference

We’ve seen a lot of different drawing functions so far and it can be hard to remember them all and how they work.  Fortunately, help is close at hand!  The p5.js online editor has an excellent reference built-in that covers all p5.js functions.  You can access it through the Help menu, and choose Reference.  Check it out!  So far, all of the functions we’ve covered are there, including bits of sample code.

Let’s expand our toolbox of drawing functions by looking at a few more useful ones.

More Polygons

You’ll recall from math class that a polygon is a multi-sided shape.  So far we have used the rect() function to draw a 4-sided polygon.  But there are many other polygons we might want to draw; for example, triangles, parallelograms, and octagons.

Here is a summary of some useful polygon drawing functions, click on each function name below to view the online reference material for each.

Function Description
triangle(x1, y1, x2, y2, x3, y3) Draws a triangle by connecting three points.
quad(x1, y1, x2, y2, x3, y3, x4, y4) Draws a quadrilateral (polygon) by connecting four points (in order).
 

beginShape()

vertex(x,y)

endShape([mode])

 

These functions work together to create a shape with any number of sides.  The beginShape() function tells p5.js that you are about to specify a series of (x,y) points (called vertices) to be joined.  The vertex() function is used to specify each point for your shape, p5.js will use these points to “connect the dots” for your polygon. The endShape() function tells p5.js that you are finished specifying vertices.  If you want to join the first and last vertex points (to close the shape) you can include the optional CLOSE argument to this function.

Here’s an example of these functions in action:

Arcs and Curves

p5.js also includes some very powerful functions for drawing arcs (think Pac-Man) and different types of curves.

To draw an arc, you need to specify the start and end angles.  You are probably used to thinking in degrees for angle measurements.  p5.js uses a different system called radians (based on fractions of π).  Fortunately, there is a function called radians() that will convert degree measurements into radians for us so we don’t need to get into the details of radians — I’ll leave that to your future math teacher.

Description
arc(x, y, w, h, startAngle, stopAngle, [drawMode])

 

radians(degreeValue)

 

Draws a section of an ellipse.  The first four arguments are the same as an ellipse, the next two specify the angles which the arc should start and finish (in radians).  There is an optional 7th drawMode argument that can be OPEN, CHORD, or PIE depending on the style of arc you’d like.

You are probably used to using degrees to represent angles, but p5.js uses radians. Fortunately, there is a function called radians() that will convert from degrees into radians for you.  I’ve used this in the example code below.

curve(x1Anchor, y1Anchor, x2, y2, x3, y3, x4Anchor, y4Anchor)

 

Draws a curve called a Catmull-Rom Spline.  The first and last pair of coordinates define the start and end anchor points for the curve, and the middle two pairs of coordinates (x2, y2) and (x3, y3) define the part of the curve to draw on the canvas.
bezier(x1Anchor, y1Anchor, x2, y2, x3, y3, x4Anchor, y4Anchor)

 

Draws another type of curve called a Bezier curve.  The first and last pair of coordinates define the start and end of the curve, but the middle two pairs of coordinates (x2, y2) and (x3, y3) define the shape of the curve by “pulling” the curve toward them.

Here’s some example code to demonstrate these drawing functions:

You Try!

  1. Use the arc() function to draw four yellow PacMan figures.  Have each facing in a different direction (left, down, right, and up).  Don’t forget to draw an eye for each figure!
  2. Use the arc() function to create a pie chart that is divided into 4 different coloured slices: 40%, 30%, 20%, and 10% of the pie.
  3. Here’s another brain teaser.  Draw the following figure using only 4 calls to the triangle() function.
  4. Wassily Kandinsky was a famous Russian painter and is considered by many to be a pioneer of abstract art.  In the 1920’s he was very interested in creating abstract art using simple geometric shapes — he would have loved p5.js!  Below is one of his priceless paintings (“Composition II”) that I’ve ripped off the Internet (for free!).
    Try to reproduce his painting using as many of the p5.js functions you have learned so far!  If you look carefully at his work you will see the subtle use of transparency.  Paint colours and RGB colours mix differently so your transparency colours may look different (that’s ok!).