Open In App

p5.js quad() Function

The quad() function is an inbuilt function in p5.js which is used to draw a quadrilateral. A quadrilateral is a four-sided polygon. It function creates a shape similar to rectangle, but the angles between two edges are not constrained to 90 degrees.

Syntax:



quad(x1, y1, x2, y2, x3, y3, x4, y4)

or

quad(x1, y1, z1, x2, y2, z2, x3, y3, z3, x4, y4, z4)

Parameters: This function accepts twelve parameters as mentioned above and described below:



Below programs illustrates the quad() function in P5.js:

Example 1: This example uses quad() function to create a polygon without using z-coordinate.




function setup() {
      
    // Create canvas of given size
    createCanvas(400, 400);
}
   
function draw() {
      
    // Set the background color
    background(220);
      
    noStroke();
      
    // Set the fill color
    fill('green');
      
    // x1, y1 = 38, 31; x2, y2 = 300, 20;
    // x3, y3 = 100, 63; x4, y4 = 130, 250
    quad(38, 31, 300, 20, 100, 63, 130, 250); 
}

Output:

Example 2: This example uses quad() function to create polygon with z-coordinate.




function setup() {
      
    // Create canvas of given size
    createCanvas(400, 400);
}
   
function draw() {
  
    // Set the background color
    background(99);
      
    noStroke();
      
    // Set the filled color
    fill('pink');
      
    // x1, y1, z1 = 38, 131, 100;
    // x2, y2, z2 = 320, 100, 63; 
    // x3, y3, z3 = 130, 150, 134; 
    // x4, y4, z4 = 155, 66, 88;
    quad(38, 131, 100, 320, 100, 63,
        130, 150, 134, 155, 66, 88); 
}

Output:

Reference: https://p5js.org/reference/#/p5/quad


Article Tags :