FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Programs
(Moderators: fry, REAS)
   Drawing an equilateral triangle
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: Drawing an equilateral triangle  (Read 635 times)
jtonsfeldt


Drawing an equilateral triangle
« on: Sep 20th, 2004, 6:20pm »

Hi,
 
I'm trying to figure out the coordinates for an equilateral triangle, positioned in the center of a screen, with all corners extending 255 pixels from the center.  Let's say the screen is 600x600.  
 
I failed Geometry in high school (I know...), so I'm sure there is a simple formula I'm not aware of.  Any suggestions?
 
Thanks
 
Josh
 
Frederik

WWW Email
Re: Drawing an equilateral triangle
« Reply #1 on: Sep 20th, 2004, 8:28pm »

Ok (taking deep breath)...
 
* Instant gratification
 
 Assuming triangle is pointing up:
 
 point apex
 x=600.0/2.0
 y=600.0/2.0-255.0
 
 point left base
 x=600.0/2.0-255.0*cos(PI/6.0)
 y=600.0/2.0+255.0*sin(PI/6.0)
 
 point right base
 x=600.0/2.0+255.0*cos(PI/6.0)
 y=600.0/2.0+255.0*sin(PI/6.0)
 
* In general: equilateral triangle inscribed in circle with radius R, centered at point (Xc,Yc),
apex at angle ALPHA from vertical:
 
 point 1
 x=Xc+R*cos(ALPHA)
 y=Yc+R*sin(ALPHA)
 
 point 2
 x=Xc+R*cos(ALPHA+TWO_PI/3.0)
 y=Yc+R*sin(ALPHA+TWO_PI/3.0)
 
 point 3
 x=Xc+R*cos(ALPHA+TWO_PI/1.5)
 y=Yc+R*sin(ALPHA+TWO_PI/1.5)
 
* Even more general: N-sided regular polygon inscribed in circle with radius R, centered at point (Xc,Yc), first point at angle ALPHA from vertical:
 
 point i (from 0 to N-1):
 xi=Xc+R*cos(ALPHA+i*TWO_PI/N)
 yi=Yc+R*sin(ALPHA+i*TWO_PI/N)
 
* Deeper background, think about it:
 
 point on circle centered on (Xc,Yc) with radius R,
 at angle ALPHA from vertical
 
 x=Xc+R*cos(ALPHA)
 y=Yc+R*sin(ALPHA)
 
Hmm, triangle with corners extending 255 pixels from the center. Colour triangle?
 
Anyway, have fun. Don't forget to share your work!
 
Seeya,
 
Frederik
http://www.wmute.org
« Last Edit: Sep 20th, 2004, 8:30pm by Frederik »  
jtonsfeldt


Re: Drawing an equilateral triangle
« Reply #2 on: Sep 21st, 2004, 7:03pm »

Thanks!  
 
You are right - 'color triangles'.  
 
I implemented the first set of formulas you provided.  
 
Here is initial sketch: www.friendlycommunity.net/josh/triangles.html
 
Click on part of the image.  A triangle is drawn which plots the RGB values of the selected pixel, and draws the triangle in corresponding color.  The point apex is red, the right is green, the left is blue.  
 
Any comments appreciated.
 
   
Here's the code:
 
void setup()
{
size(700, 700);
framerate(30);
BImage c = loadImage("http://www.friendlycommunity.net/josh/bug4.jpg");
image(c, -300, 500);
BImage d = loadImage("http://www.friendlycommunity.net/josh/greenery2.jpg");
image(d, 300, 500);
}
 
color b;
 
void mousePressed()
{
b = get(mouseX, mouseY);
}
 
void mouseDragged()
{
b = get(mouseX, mouseY);
}
 
void loop()
{
 
 int x1 = width/2;
 int y1 = int(height/2-(red(b)*1.25));
 
 int x2 = int(width/2-(green(b)*1.25)*cos(PI/6));
 int y2 = int(height/2+(green(b)*1.25)*sin(PI/6));
 
 int x3 = int(width/2+(blue(b)*1.25)*cos(PI/6));
 int y3 = int(height/2+(blue(b)*1.25)*sin(PI/6));
 
 
stroke(b);
noFill();
triangle(x1,y1,x2,y2,x3,y3);
 
}
 
Pages: 1 

« Previous topic | Next topic »