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 › math help: draw an equilateral triangle
Page Index Toggle Pages: 1
math help: draw an equilateral triangle (Read 1310 times)
math help: draw an equilateral triangle
Sep 7th, 2009, 6:24pm
 
I'm trying to write a script that draws an equilateral triangle, dynamically. COuld someone help me calculate a third vertex if two are given?

One vertex is anchored down, say at the point (80, 100). The third is the mouseDragged() location (which changes). How do I calculate the third?
Re: math help: draw an equilateral triangle
Reply #1 - Sep 7th, 2009, 7:19pm
 
This is what I have so far, but it's not drawing an equilateral:
Code:
int valueX = 700;
int valueY = 300;
int originX = 400;
int originY = 300;



void setup(){
 size(1080,720);
}

void draw(){
 frameRate(60);
 background(220, 100, 100);
 line(originX, originY, valueX, valueY);
 stroke(80, 100, 60);
 strokeWeight(5);  
   if (mousePressed == true){
 hitch();
 valueX= mouseX;
valueY = mouseY;

}
}

void mouseDragged()
{

}

void mouseReleased() {
 int tempX = mouseX;
 int tempY = mouseY;
 line(tempX, tempY, 50,50);
}

void hitch(){

 int a1 = mouseX;
 int a2 = mouseY;
//  int h1 = 50;
//  int h2 = 50;
 float triangleSide = dist(a1, a2, originX, originY);
 float triAngleFromHorizon = 60-degrees(atan((originX-a1)/(originY-a2)));
 float thirdX = triangleSide*cos(triAngleFromHorizon) + a1;
 float thirdY = triangleSide*sin(triAngleFromHorizon) + a2;
// line(a1, a2, h1, h2);
triangle(a1, a2, originX, originY, thirdX, thirdY);
}

Re: math help: draw an equilateral triangle
Reply #2 - Sep 7th, 2009, 8:13pm
 
atan2() is your friend...

Quote:
float x1, y1, x2, y2, x3, y3;

void setup(){
  size(400,400);
  fill(200);
  x1 = width/2;
  y1 = height/2;
}

void draw(){
  background(100);
  x2 = mouseX;
  y2 = mouseY;
  x3 = x1 + (cos(atan2(y2-y1,x2-x1)-PI/3) * dist(x1,y1,x2,y2));
  y3 = y1 + (sin(atan2(y2-y1,x2-x1)-PI/3) * dist(x1,y1,x2,y2));
  triangle(x1,y1,x2,y2,x3,y3);
}


--Ben
Re: math help: draw an equilateral triangle
Reply #3 - Sep 8th, 2009, 2:14am
 
aren't there two possibly equilateral triangles given only two initial points? mirror images of each other...
Re: math help: draw an equilateral triangle
Reply #4 - Sep 8th, 2009, 6:26pm
 
sure...but I think he just wants "a" triangle, not "the" one true triforce :P

+ or - PI/3 in prev post to get the other one.
Page Index Toggle Pages: 1