Thursday, July 4, 2024
HomeLanguagesJavascriptp5.js quadraticVertex() Function

p5.js quadraticVertex() Function

The quadraticVertex() function in p5.js is used to specify the vertex coordinates used to draw a quadratic bezier curve. Every call to this function defines the position of one control point and one anchor point of the bezier curve.

This function can only be used between beginShape() and endShape(). It must be prefaced with a call to the vertex() function to set the first anchor point when called with beginShape() for the first time.

The function expects four parameters in 2D mode and six parameters (which includes z coordinates) in 3D mode. Both the 2D and 3D modes can be used for drawing in the WebGL mode.

Syntax:

quadraticVertex( cx, cy, x3, y3 )

OR

quadraticVertex( cx, cy, cz, x3, y3, z3 )

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

  • cx: It is a number that specifies the x-coordinate of the control point.
  • cy: It is a number that specifies the y-coordinate of the control point.
  • x3: It is a number that specifies the x-coordinate of the anchor point.
  • y3: It is a number that specifies the y-coordinate of the anchor point.
  • cz: It is a number that specifies the z-coordinate of the control point. It is used in the WebGL mode.
  • z3: It is a number that specifies the z-coordinate of the anchor point. It is used in the WebGL mode.

The examples below illustrates the quadraticVertex() function in p5.js:

Example 1:




function setup() {
  createCanvas(500, 300);
  textSize(16);
}
  
function draw() {
  background("green");
  fill("black");
  text(
    "The bezier below is made using bezierVertex()"+
    " function in Canvas",
    10,
    20
  );
  text("Red fill color is used to denote anchor"+
       " points", 10, 40);
  text("Blue fill color is used to denote control"+
       " points", 10, 60);
  
  // Define the points
  // First Anchor Point
  let p1 = { x: 80, y: 240 };
  
  // First Control Point
  let p2 = { x: 200, y: 100 };
  
  // Second Anchor Point
  let p3 = { x: 300, y: 200 };
  
  // Second Control Point
  let p4 = { x: 400, y: 200 };
  
  // Third Anchor Point
  let p5 = { x: 400, y: 100 };
  
  noFill();
  
  // Start the quadratic bezier
  beginShape();
  
  // Specify the first anchor point
  // using vertex()
  vertex(p1.x, p1.y);
  
  // Use the quadraticVertex() function
  // to define the rest of the points
  quadraticVertex(p2.x, p2.y, p3.x, p3.y);
  quadraticVertex(p4.x, p4.y, p5.x, p5.y);
  endShape();
  
  // Draw circles for demonstration
  fill("red");
  circle(p1.x, p1.y, 10);
  
  fill("blue");
  circle(p2.x, p2.y, 10);
  
  fill("red");
  circle(p3.x, p3.y, 10);
  
  fill("blue");
  circle(p4.x, p4.y, 10);
  
  fill("red");
  circle(p5.x, p5.y, 10);
}


Output:

quadraticVertex_2d

Example 2:




let newFont;
  
function preload() {
  newFont = loadFont("fonts/Montserrat.otf");
}
  
function setup() {
  createCanvas(500, 300, WEBGL);
  textFont(newFont, 14);
}
  
function draw() {
  translate(-width / 2, -height / 2);
  
  background("green");
  fill("black");
  text(
    "The bezier below is made using bezierVertex()"+
    " function in Canvas",
    10,
    20
  );
  
  // Define the points
  // First Anchor Point
  let p1 = { x: 80, y: 240, z: 10 };
  
  // First Control Point
  let p2 = { x: 250, y: 100, z: 100 };
  
  // Second Anchor Point
  let p3 = { x: 300, y: 200, z: 10 };
  
  // Second Control Point
  let p4 = { x: 300, y: 250, z: 200 };
  
  // Third Anchor Point
  let p5 = { x: 400, y: 100, z: 10 };
  
  // Start the quadratic bezier
  beginShape();
  
  // Specify the first anchor point
  // using vertex()
  vertex(p1.x, p1.y, p1.z);
  
  // Use the quadraticVertex() function
  // to define the rest of the points
  quadraticVertex(p2.x, p2.y, p2.z, p3.x, p3.y, p3.z);
  quadraticVertex(p4.x, p4.y, p4.z, p5.x, p5.y, p5.z);
  endShape();
}


Output:

quadraticVertex_3d

Online editor: https://editor.p5js.org/

Environment Setup: https://www.neveropen.co.za/p5-js-soundfile-object-installation-and-methods/

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

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!

Shaida Kate Naidoo
am passionate about learning the latest technologies available to developers in either a Front End or Back End capacity. I enjoy creating applications that are well designed and responsive, in addition to being user friendly. I thrive in fast paced environments. With a diverse educational and work experience background, I excel at collaborating with teams both local and international. A versatile developer with interests in Software Development and Software Engineering. I consider myself to be adaptable and a self motivated learner. I am interested in new programming technologies, and continuous self improvement.
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments