# How can I make a regular triangle from a line?

**URL:** https://discourse.processing.org/t/how-can-i-make-a-regular-triangle-from-a-line/19655
**Category:** Coding Questions
**Created:** [April 11, 2020, 11:41am UTC](https://discourse.processing.org/t/how-can-i-make-a-regular-triangle-from-a-line/19655 "2020-04-11T11:41:34Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![Tauter](https://avatars.discourse-cdn.com/v4/letter/t/e274bd/32.png) [@Tauter](https://discourse.processing.org/u/Tauter)
#### Post date: [April 11, 2020, 11:41am UTC](https://discourse.processing.org/t/how-can-i-make-a-regular-triangle-from-a-line/19655/1 "2020-04-11T11:41:34Z")

</div>

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 ?

```auto

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); 
      }
    }
  }
};

```

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [April 11, 2020, 12:04pm UTC](https://discourse.processing.org/t/how-can-i-make-a-regular-triangle-from-a-line/19655/2 "2020-04-11T12:04:40Z")

</div>

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:

- Processing website has references, examples, tutorials, etc.  
[https://processing.org/](https://processing.org/)

- Those available with the Processing PDE (IDE).  
An exploration of the tabs will reveal what is available; there are libraries and examples there.

- The Processing Foundation  
[https://processingfoundation.org/](https://processingfoundation.org/)  
Check out the tabs.  
In the education tab, there is a reference to the Coding Train.

- The Coding Train  
[https://www.youtube.com/channel/UCvjgXvBlbQiydffZU7m1\_aw](https://www.youtube.com/channel/UCvjgXvBlbQiydffZU7m1_aw)  
[https://thecodingtrain.com/](https://thecodingtrain.com/)

- Happy Coding  
[Processing Tutorials - Happy Coding](https://happycoding.io/tutorials/processing/)

To get you started:  
[https://happycoding.io/tutorials/processing/creating-functions](https://happycoding.io/tutorials/processing/creating-functions)

> **[Variables / Examples](https://processing.org/examples/variables.html)**
>
> Variables are used for storing values. In this example, change the values of variables to affect the composition.

> **[line() / Reference](https://processing.org/reference/line_.html)**
>
> Draws a line (a direct path between two points) to the screen. The version of line() with four parameters draws the line in 2D. To color a line, use the stroke() function. A line cannot …

`:)`

---

<div class="post-metadata">

### Author: ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)
#### Post date: [April 11, 2020, 12:24pm UTC](https://discourse.processing.org/t/how-can-i-make-a-regular-triangle-from-a-line/19655/3 "2020-04-11T12:24:43Z")

</div>

Can you post a code for the function szr please?

---

<div class="post-metadata">

### Author: ![Tauter](https://avatars.discourse-cdn.com/v4/letter/t/e274bd/32.png) [@Tauter](https://discourse.processing.org/u/Tauter)
#### Post date: [April 11, 2020, 12:27pm UTC](https://discourse.processing.org/t/how-can-i-make-a-regular-triangle-from-a-line/19655/4 "2020-04-11T12:27:50Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Tauter](https://avatars.discourse-cdn.com/v4/letter/t/e274bd/32.png) [@Tauter](https://discourse.processing.org/u/Tauter)
#### Post date: [April 11, 2020, 12:28pm UTC](https://discourse.processing.org/t/how-can-i-make-a-regular-triangle-from-a-line/19655/5 "2020-04-11T12:28:25Z")

</div>

Yes, of course i will edit the question.

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [April 11, 2020, 12:32pm UTC](https://discourse.processing.org/t/how-can-i-make-a-regular-triangle-from-a-line/19655/6 "2020-04-11T12:32:23Z")

</div>

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](https://discourse.processing.org/faq#format-your-code)

`:)`

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [April 11, 2020, 12:57pm UTC](https://discourse.processing.org/t/how-can-i-make-a-regular-triangle-from-a-line/19655/7 "2020-04-11T12:57:41Z")

</div>

This was just an exploration into your code.

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

```auto
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.
  }

```

 ![image](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/2/208bf3f5f981e2ae6e59af4f8e0a3c792cab1aa6.png)

`:)`

---

<div class="post-metadata">

### Author: ![Tauter](https://avatars.discourse-cdn.com/v4/letter/t/e274bd/32.png) [@Tauter](https://discourse.processing.org/u/Tauter)
#### Post date: [April 11, 2020, 1:40pm UTC](https://discourse.processing.org/t/how-can-i-make-a-regular-triangle-from-a-line/19655/8 "2020-04-11T13:40:33Z")

</div>

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. 😁  
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 ?

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [April 11, 2020, 1:50pm UTC](https://discourse.processing.org/t/how-can-i-make-a-regular-triangle-from-a-line/19655/9 "2020-04-11T13:50:11Z")

</div>

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/](https://processing.org/tutorials/trig/)

> **[Equilateral triangle](https://en.wikipedia.org/wiki/Equilateral_triangle)**
>
> In geometry, an equilateral triangle is a triangle in which all three sides are equal. In the familiar Euclidean geometry, an equilateral triangle is also equiangular; that is, all three internal angles are also congruent to each other and are each 60°. It is also a regular polygon, so it is also referred to as a regular triangle.
> Denoting the common length of the sides of the equilateral triangle as 
>   
>     
>       
> a
>       
>     
> {\\displaystyle a}
>   
> , we can determine using the Pyth...

`:)`

---

<div class="post-metadata">

### Author: ![Tauter](https://avatars.discourse-cdn.com/v4/letter/t/e274bd/32.png) [@Tauter](https://discourse.processing.org/u/Tauter)
#### Post date: [April 11, 2020, 2:04pm UTC](https://discourse.processing.org/t/how-can-i-make-a-regular-triangle-from-a-line/19655/10 "2020-04-11T14:04:44Z")

</div>

I will try. Thank you.

---

<div class="post-metadata">

### Author: ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)
#### Post date: [April 11, 2020, 3:17pm UTC](https://discourse.processing.org/t/how-can-i-make-a-regular-triangle-from-a-line/19655/11 "2020-04-11T15:17:43Z")

</div>

Hello,

I took the approach of rotating a point.

[Google search for rotating a point](https://www.google.com/search?q=rotate+a+point&rlz=1C1CHBF_enCA822CA822&oq=rotate&aqs=chrome.0.69i59l2j69i57j69i65l2j69i60.1745j0j8&sourceid=chrome&ie=UTF-8)

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](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/9/9b8b8d9c2a4f2657b8b89100e5296ad2b59c53dc.png)
