Draw a triangle with pushMatrix and popMatrix

Hello everyone,

I am trying to draw a triangle using pushMatrix(), popMatrix(), and rotate(). But it doesn't seem that the rotation works. I tried the method using line(x1, y1, x2, y2) command and the rotation works fine. I wonder what I am doing wrong with the beginShape() command.

Thank you for your help.

-unparadise

Below is my code.

color baseShade = color(108, 245, 72);
float len1 = random(40, 80);
float len2 = random(50, 60);

void setup() {
  size(400, 400);
  background(40);
  smooth();
  stroke(baseShade);
  fill(baseShade);  
}

void draw() {
  background(40);
  drawTriangle(120, len1, len2);
}

void drawTriangle(float angle, float len1, float len2) {
  pushMatrix();
  translate(width/2, height/2);
  rotate(radians(-90));
  //line(0, 0, len1, 0);
  //rotate(radians(angle));
  //line(0, 0, len2, 0);
  beginShape(TRIANGLES);
  vertex(0, 0);
  vertex(len1, 0);

  rotate(radians(angle));

  vertex(len2, 0);
  endShape(CLOSE);
  popMatrix();
}
Tagged:

Answers

  • Found the answer myself from the beginShape() reference page.

    "Transformations such as translate(), rotate(), and scale() do not work within beginShape(). It is also not possible to use other shapes, such as ellipse() or rect() within beginShape(). "

  • It is also not possible to use other shapes, such as ellipse() or rect() within beginShape()

    Well, you can, but not to construct the shape that you're using the vertices (or curveVertices) for:

    beginShape();
    for(int i = 0; i < 6; i++)
    {
      vertex(random(width), random(height));
    
      ellipse(random(width), random(height), 3, 3);
    }
    endShape(CLOSE);
    
  • Sorry for the late of my reply. Thank you for your help, allonestring.

Sign In or Register to comment.