Polygon formula [for graphics]

Thread Solved

Join Date: Jun 2008
Posts: 97
Reputation: Alex_ is an unknown quantity at this point 
Solved Threads: 2
Alex_'s Avatar
Alex_ Alex_ is offline Offline
Junior Poster in Training

Polygon formula [for graphics]

 
0
  #1
Feb 17th, 2009
Hello everybody, i need a polygon formula to draw one. I want to make a function that will draw a polygon , depending on how many vertex's it has. I'm not good at math so i can't figure it out how to do it. Though, i do understand that it should look something like: pass=360/vertex , then calculate some end points of lines that are 'pass' degrees from each other; but i don't know how to do that.
Can someone help?Pls!
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 83
Reputation: nmaillet is an unknown quantity at this point 
Solved Threads: 18
nmaillet nmaillet is offline Offline
Junior Poster in Training

Re: Polygon formula [for graphics]

 
1
  #2
Feb 17th, 2009
If my math is correct,
  1. x(n) = x + r*cos(a + n*pass)
  2. y(n) = y + r*sin(a + n*pass)
where 0 <= n < vertex , x and y is the center point, a is the angle of the first point to the right of the center point from the horizontal(counter-clockwise) and r is the distance from the center point to each of the vertices(the "radius"). Let me know if this is what you were looking for.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 476
Reputation: nucleon has a spectacular aura about nucleon has a spectacular aura about 
Solved Threads: 91
nucleon's Avatar
nucleon nucleon is offline Offline
Posting Pro in Training

Re: Polygon formula [for graphics]

 
1
  #3
Feb 17th, 2009
Also note that you have to use radians (not degrees) for your angles, so pass = 2 * M_PI / vertex (M_PI is in math.h) .
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 97
Reputation: Alex_ is an unknown quantity at this point 
Solved Threads: 2
Alex_'s Avatar
Alex_ Alex_ is offline Offline
Junior Poster in Training

Re: Polygon formula [for graphics]

 
0
  #4
Feb 17th, 2009
Sort of ! But not really..
This is what i wrote
  1. void polygon(int x, int y, int n, int r){
  2. int k=0, *poly;
  3. double pass= 360/n, _n=0,pass2=n*pass;
  4. poly=new int[n];
  5. while(k<n*2){
  6. poly[k++]=x +r * cos(pass2);
  7. poly[k++]=y +r * sin(pass2);
  8. pass2+=pass;
  9. }
  10. drawpoly(n,poly);
  11. }
as i understood , a is a=a+a; at each iteration? If it is so, it is not really what i expected, though it's better than nothing. Thx!
This is the output,at:
Last edited by Alex_; Feb 17th, 2009 at 4:02 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 97
Reputation: Alex_ is an unknown quantity at this point 
Solved Threads: 2
Alex_'s Avatar
Alex_ Alex_ is offline Offline
Junior Poster in Training

Re: Polygon formula [for graphics]

 
0
  #5
Feb 17th, 2009
tried some modifications
  1. void polygon(int x, int y, int n, int r){
  2. int k=0, *poly;
  3. double pass= 2* M_PI/n,pass2=n*pass, a=360/n;
  4. poly=new int[n];
  5. while(k<n*2){
  6. pass2+=a;
  7. poly[k++]=x +r * cos(pass2);
  8. poly[k++]=y +r * sin(pass2);
  9. }
  10. drawpoly(n,poly);
  11. }
check:
Last edited by Alex_; Feb 17th, 2009 at 4:01 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 71
Reputation: Alibeg is an unknown quantity at this point 
Solved Threads: 10
Alibeg Alibeg is offline Offline
Junior Poster in Training

Re: Polygon formula [for graphics]

 
0
  #6
Feb 17th, 2009
a is constant, but n*pass changes because n changes
i dont know why do you use double pass = 360 / n
and after that pass2 = n * pass, do you mean pass2 = 360?
and why do you use _n?
dont forget about radians, because, i think, sin() and cos() use radians as input parameters
this is not about your previous post but one before it
Last edited by Alibeg; Feb 17th, 2009 at 4:06 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 71
Reputation: Alibeg is an unknown quantity at this point 
Solved Threads: 10
Alibeg Alibeg is offline Offline
Junior Poster in Training

Re: Polygon formula [for graphics]

 
1
  #7
Feb 17th, 2009
double pass2 = a; //where a is angle between line (first point of polygon joined with centre of polygon) and x axis.....you might like to set pass2 to 0 in the beginning
while(k<n*2)
{
poly[k++]=x +r * cos(pass2);
poly[k++]=y +r * sin(pass2);
pass2 += pass;
}
this is how it goes i think
Last edited by Alibeg; Feb 17th, 2009 at 4:13 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 97
Reputation: Alex_ is an unknown quantity at this point 
Solved Threads: 2
Alex_'s Avatar
Alex_ Alex_ is offline Offline
Junior Poster in Training

Re: Polygon formula [for graphics]

 
0
  #8
Feb 17th, 2009
Woaaah! That did it! I am very thankfull!! Super, just what i need!
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 71
Reputation: Alibeg is an unknown quantity at this point 
Solved Threads: 10
Alibeg Alibeg is offline Offline
Junior Poster in Training

Re: Polygon formula [for graphics]

 
0
  #9
Feb 17th, 2009
no prob, just dont forget to give the credits to a man who gave you the formula in the first place (nmaillet)
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC