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:

  • x1: This parameter takes the x-coordinate of the first point.
  • y1: This parameter takes the y-coordinate of the first point.
  • z1: This parameter takes the z-coordinate of the first point.
  • x2: This parameter takes the x-coordinate of the second point.
  • y2: This parameter takes the y-coordinate of the second point.
  • z2: This parameter takes the z-coordinate of the second point.
  • x3: This parameter takes the x-coordinate of the third point.
  • y3: This parameter takes the y-coordinate of the third point.
  • z3: This parameter takes the z-coordinate of the third point.
  • x4: This parameter takes the z-coordinate of the fourth point.
  • y4: This parameter takes the z-coordinate of the fourth point.
  • z4: This parameter takes the z-coordinate of the fourth point.

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

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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); 
}

chevron_right


Output:

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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); 
}

chevron_right


Output:

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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.