How to create an equilateral triangle made of lines using for loops?

edited August 2015 in Questions about Code

Hello,

I'm trying to draw a triangle made of lines using a for loop, but all I can get are triangles with sides that are not equilateral. How do I do this? I've attached my code below:

void setup(){
 size(500,500);
}

void draw(){
  background(0);
    float positionHeight = 200;
  float forAdjust = 10;
  float forAdjustLeft = -forAdjust;
   for (int xPos = 409; xPos <width; xPos += 8, positionHeight += forAdjust, positionHeight -= forAdjustLeft) {
      stroke(random(0, 255), random(255), random(255));
      strokeWeight(3);
      line(xPos, 0, xPos, positionHeight );
    }

} 

Answers

  • Nice try, but its not that simple. Did you even look at what I was trying to do?

  • see if this can help you

    void setup(){
    size (400,400);
    for(int i = 0 ; i < 360; i+=120){
        float x = width/2  + cos(radians(i))*100;
        float y = height/2 + sin(radians(i))*100;
        ellipse(x,y, 10, 10);
        }
    }
    
  • I thought the example would be equil.

    it's not

    oups nvm

    that's almost:

    //
    void setup()
    {
      // init
      size(800, 600);
      noLoop();
    } // func 
    
    void draw() 
    { 
      background(255);
    
      float a1 = 26.5021; 
      triangle(30, 75, 58, a1, 86, 75);
    
    
      // points 1 and 2
      println (dist(30, 75, 58, a1));
    
      // 2 and 3
      println (dist(58, a1, 86, 75));
    
      // 1 and 3
      println (dist(30, 75, 86, 75));
    } // func 
    //
    // ==============================
    
  • _vk, this is very helpful, however I'm trying to fill the triangle full of ellipses instead of just drawing an ellipse at each corner. I believe I need a nested for loop to do this, just unsure how to execute it.

  • To clarify, I'm trying to draw a triangle made of lines with for loops. See the image below:

  • I'm trying to fill the triangle full of ellipses------

    ??

    homework?

  • I only referenced ellipses because he _vk used them in his sketch

  • Answer ✓

    look at lerp() please

    https://processing.org/reference/

  • Back in the day we used to call this 'rasterisation' or 'scanline algorithm' only the lines were horizontal. Googling brings up useful stuff.

  • Answer ✓

    A hard-coded way to go about it. As koogs mentioned, looking into rasterization is probably what you want to generalize the process:

    void setup() {
      size(500, 500);
      noFill();
      strokeWeight(2);
    }
    
    void draw() {
      background(0);
    
      // Used a calculator and the Pythagorean Theorem
      final float incHeight = 1.7320508;
    
      // Left half
      float lineHeight = 0.0;
      for (int i = width / 2 - 100; i < width / 2; i++) {
        stroke(random(255), random(255), random(255));
        line(i, height / 2, i, height / 2 + lineHeight);
        lineHeight += incHeight;
      }
    
      // Right half
      lineHeight = 0.0;
      for (int i = width / 2 + 100; i > width / 2; i--) {
        stroke(random(255), random(255), random(255));
        line(i, height / 2, i, height / 2 + lineHeight);
        lineHeight += incHeight;
      }
    } 
    
  • Answer ✓
    int i3=0;
    
    void setup() {
      size (400, 400);
      background(0);
      noStroke();
      fill(255, 0, 0);
    }
    
    void draw() {
      background(0);
    
      PVector [] a1 = new PVector [3];
      int k = 0;
      for (int i = i3; i < 360+i3; i+=120) {
        float x = width/2  + cos(radians(i))*100;
        float y = height/2 + sin(radians(i))*100;
        a1[k] = new PVector(x, y);
        k++;
      }
      i3++;
    
      for (int i2 = 0; i2 <= 10; i2++) {
        float x1 = lerp(a1[1].x, a1[0].x, i2/10.0) + 0;
        float y1 = lerp(a1[1].y, a1[0].y, i2/10.0);
    
        float x2 = lerp(a1[2].x, a1[0].x, i2/10.0) + 0;
        float y2 = lerp(a1[2].y, a1[0].y, i2/10.0);
    
        for (int i = 0; i <= 10; i++) {
          float x = lerp(x1, x2, i/10.0) + 0;
          float y = lerp(y1, y2, i/10.0);
          ellipse(x, y, 4.3, 4.99);
        }
      }
    }
    //
    
Sign In or Register to comment.