We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Centered, equilateral triangle - math help
Page Index Toggle Pages: 1
Centered, equilateral triangle - math help (Read 946 times)
Centered, equilateral triangle - math help
Jan 6th, 2010, 11:16am
 
Hi there,

I saw this thread:
http://processing.org/discourse/yabb2/num_1252373090.html
and it is very useful, except I would like to know how to do 3 things:
1. draw the triangle so that your cursor is in the middle of it and it moves
2. depict orientation of the triangle (left/right/up/down) somehow
3. specify the distance from the CURSOR to each of the points (or even the length of the sides)
Re: Centered, equilateral triangle - math help
Reply #1 - Jan 6th, 2010, 12:41pm
 
Here is some math:
if the triangle has the bottom line in direction of the x-achsis, an the lenght of a side is c, you add a vector to the left bottom point
with x-component c/2 and an y-component -c/sqrt(3). Thats the middle.
From here you get the distance to the three points with the dist()-function.
Re: Centered, equilateral triangle - math help
Reply #2 - Jan 6th, 2010, 1:31pm
 
Hm; OK.  For a second let's just forget about the mouse.
How about a way to just draw the triangle?  I don't want to use all of the built-in functions; I'm really looking to learn more about the math involved.

I'm not looking for someone to do this for me, but I really thought it would be something closer to:

Code:
float prevx, prevy, nextx, nexty, radius, angle;
int deg, midx, midy;

void setup()
{
 size(400,400);
 fill(200);
 midx = width/2;
 midy = height/2;
 radius = 80;

}

void draw()
{
 background(100);
 translate(midx, midy);
 
 beginShape();
 for(deg = 0; deg < 360; deg += 120)
 {
   angle = deg * PI / 180;
   nextx = cos(angle) * radius;
   nexty = sin(angle) * radius;
   vertex(nextx, nexty);
 }
 endShape(CLOSE);
}


... but I want the triangle to sit pointing directly upwards (north).
For ex., if I add the code...

Code:
  rotate(radians(30)); 



...after the shape has been ended, it seems to be sorta straight; but I know that there is more to this; I would like to know what I need to do to have the for loop draw the correct vertices...  

Thanks
Re: Centered, equilateral triangle - math help
Reply #3 - Jan 7th, 2010, 2:12pm
 
I had a very similar idea after my first reply. I did the rotation by adding a multiple of 90°-angles to the 120-degrees.
This is my  code:
Code:
void setup() {
size(600,400);
smooth();
}
float angle=2*PI/3; //120 Degrees
float angle_rect=PI/2;
float d=50;
float centerx=200;
float centery=200;

void draw() {

stroke(0);

}

void draw_shape(float x, float y, int b){

beginShape();
for(int i=0;i<3;i++){
vertex(x+d*sin(angle*i+b*angle_rect),y+d*cos(angle*i+b*angle_rect));
}
endShape();

}


void keyPressed() {
background(200);
switch(key){
case '0':
draw_shape(centerx,centery,0);
break;
case '1':
draw_shape(centerx,centery,1);
break;
case '2':
draw_shape(centerx,centery,2);
break;

case '3':
draw_shape(centerx,centery,3);
break;
}

}
Page Index Toggle Pages: 1