Loading...
Logo
Processing Forum

Draw a triangle

in Programming Questions  •  1 year ago  
I have to draw a triangle with mouse clicks (3 clicks = 1 triangle). I have this code:

Copy code
  1. void setup()
    {
      size(600, 480);
      background(255); 
     
      beginShape(TRIANGLES);
    }

    void draw()
    {
    }

    void mousePressed()
    {
        point(mouseX, mouseY);
        vertex(mouseX, mouseY);
    }
It don't draw any point and any triangle. What's wrong?

Thanks!

Replies(3)

Re: Draw a triangle

1 year ago
This changes draw the points, but no triangle's drawed when I click some times.

Copy code
  1. void setup()
    {
      size(600, 480);
      background(255); 
     
      beginShape(TRIANGLES);
    }

    void draw()
    {
      if(mousePressed)
      {
        point(mouseX,mouseY);
        vertex(mouseX,mouseY);
      }
  2. }

Re: Draw a triangle

1 year ago
Check out the reference for beginShape(). Place the vertices between the beginShape() and endShape() calls and store the points so you know when to connect three of them as a triangle. When you combine these two suggestions, you could get something like the code below. Clicking the mouse adds a point to the list. A triangles is drawn once there are three points in the list.

Code Example
Copy code
  1. ArrayList <PVector> points = new ArrayList <PVector> ();
  2.  
  3. void setup() {
  4.   size(600, 480);
  5.   background(255);
  6.   fill(0,125);
  7.   smooth();
  8. }
  9.  
  10. void draw() {
  11. }
  12.  
  13. void mousePressed() {
  14.   PVector m = new PVector(mouseX, mouseY);
  15.   points.add(m);
  16.   if (points.size() > 2) {
  17.     beginShape(TRIANGLES);
  18.     for (PVector p : points) {
  19.       vertex(p.x, p.y);
  20.     }
  21.     endShape();
  22.     points.clear();
  23.   }
  24. }
Thanks amnon.owed!

I'm starting with Processing, I like it!
Now I'm planning to create visual art with music (a mp3)