Thursday, September 4, 2025
HomeLanguagesJavascriptp5.js quad() Function

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.




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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32264 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6629 POSTS0 COMMENTS
Nicole Veronica
11799 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11858 POSTS0 COMMENTS
Shaida Kate Naidoo
6749 POSTS0 COMMENTS
Ted Musemwa
7025 POSTS0 COMMENTS
Thapelo Manthata
6698 POSTS0 COMMENTS
Umr Jansen
6716 POSTS0 COMMENTS