Tauter
April 11, 2020, 11:41am
1
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);
}
}
}
};
glv
April 11, 2020, 12:04pm
2
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?
Tauter
April 11, 2020, 12:27pm
4
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.
Tauter
April 11, 2020, 12:28pm
5
Yes, of course i will edit the question.
glv
April 11, 2020, 12:32pm
6
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
:)
glv
April 11, 2020, 12:57pm
7
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.
}
:)
Tauter
April 11, 2020, 1:40pm
8
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 ?
glv
April 11, 2020, 1:50pm
9
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/
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...
:)
glv
April 11, 2020, 3:17pm
11
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().
:)