Book
Submodule 16.2: Basics of p5.js
Submodule 16.2: Basics of p5.js
Completion requirements
View
- setup() and draw() functions
- createCanvas
- Primitives Shapes
Primitives Shapes - triangle()
triangle ()
The triangle() takes 6 arguments.
These are: x1,y1,x2,y2,x3,y3. Thus we have triangle(x1,y1,x2,y2,x3,y3).
The x1,x2 and x3 are respectively the x coordinates for the first, second and third point.
The y1,y2 and y3 are respectively the y coordinates for the first, second and third point.
The triangle is created by connecting these points.
Let's see an example of a code that uses the triangle() function:
function setup() {
// create canvas
createCanvas(800,500);
}
function draw () {
// set background color
background('orange');
triangle(width/2,height/2,500,400,300,400);
triangle(width/2,height/2,500,100,300,100);
}
Exercise
- Open your Visual Studio editor and the
p5yourName
folder. - Open the file
ex812.js
in your editor and save it asex827.js
- Open the file
ex812.html
in your editor and save it asex827.html
- In the
ex827.html
file, update the link toex827.js
from ex812.js - Go to the
index.html
file and create, underModule 8
, alink
to theex827.html
file with the title "Primitives Shapes - triangle".
Modify the ex827.js
file in order to create the abstract shape using triangles. You can see here an example.
function setup() {
// create canvas
createCanvas(800,500);
}
function draw () {
// set background color
background('orange');
// create the shape using triangles
triangle(400,250,500,400,300,400);
triangle(300,100,400,250,200,250);
triangle(500,100,600,250,400,250);
triangle(400,250,200,250,300,400);
}
Do a Git commit with the message "Primitives Shapes - triangle".
- See more about the triangle() function