How can I make a regular triangle from a line?

I have to create a regular triangle from a line.
i calculated the middle of the line but after this I can’t figure it out.

(szR is a line drawing algorithm)
What should be the line end ?


int x1,x2,y1,y2,xl,yl;
void setup(){
  size(640,480);
}

void draw(){
  background(240);
  x1 = 200; y1=150;
  x2 = 450; y2 = 300;
  szR(x1,y1,x2,y2);
  xl = x2-x1; yl = y2-y1;
  szR(x1+(xl/2), y1 + (yl/2), ?, ?);
}

void szR(int xa, int ya, int xb,int yb){
  float x1 = (float) xa; float y1 = (float) ya;
  float x2 = (float) xb; float y2 = (float) yb;
  float x = x1; float y = y1;
  float m;
  m=(y1-y2)/(x1-x2);
  if(abs(m)<=1)
  {
    if(x1<x2)
    {
      for(x=x1;x<=x2;x++)
      {
        point(x,y);
        y=y+m;
      }
    }
    if(x2<x1)
    {
      for(x=x1;x>=x2;x--)
      {
        point(x,y);
        y=y-m;
      }
    }
  }
  if(abs(m)>1)
  {
    if(y1<y2)
    {
      for(y=y1;y<=y2;y++)
      {
        point(x,y);
        x=x+(1/m); 
      }
    }
     if(y2<y1)
    {
      for(y=y1;y>=y2;y--)
      {
        point(x,y);
        x=x-(1/m); 
      }
    }
  }
};

Hello,

Your posted code (and edits) suggests you are very new to Processing and programming.

For starters, you did not declare any variables and do not have a function for szR().

I encourage you to review the resources available here:

To get you started:
https://happycoding.io/tutorials/processing/creating-functions

:)

Can you post a code for the function szr please?

I’m sorry, I do declared the variables, and i have the szR function.
I thought it does not help, as the szR method is working.

Yes, of course i will edit the question.

Please do not keep editing the original post; other than properly formatting it.

Followup to questions with an updated post.

Please format your code:
https://discourse.processing.org/faq#format-your-code

:)

This was just an exploration into your code.

It appears your function just draws a line() with point().

void draw()
  {
  background(240);
  x1 = 200; y1=150;
  x2 = 450; y2 = 300;
  
  push();
  stroke(255, 0, 0);
  translate(0, -10);
  line (x1, y1, x2, y2);
  pop();
  //same as:
  szR(x1,y1,x2,y2);
  
  xl = x2-x1; yl = y2-y1;
  
  szR(x1, y1, x1, y2);
  //szR(x2, y2, ?, ?); // You can complete this.
  }

:)

Thank you!
I know the line() is way better. But my instructor does not allow it.
Thanks to you one of my task is done. :grin:
The other parts are already done.
But there is a problem,I know i asked much already, but how can I make a equilateral triangle out of the same line ?

You know where your x and y points are; just think about where to place them and how you would do that. It can be as simple as multiplying, adding, subtracting, rotating, translating, etc.

This is achievable so I will leave this with you to do and you can enjoy the feeling you get when you succeed.

https://processing.org/tutorials/trig/

:)

I will try. Thank you.

Hello,

I took the approach of rotating a point.

Google search for rotating a point

Given any line (x1, y1, x2, y2) you can rotate it to create other lines and then translate (add to x and y) to make an equilateral triangle.

I did the math and did not use the Processing rotate() or translate().

:)

image