943,807 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 1438
  • C RSS
Feb 17th, 2009
0

Polygon formula [for graphics]

Expand Post »
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!
Reputation Points: 10
Solved Threads: 3
Junior Poster
Alex_ is offline Offline
175 posts
since Jun 2008
Feb 17th, 2009
1

Re: Polygon formula [for graphics]

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.
Reputation Points: 69
Solved Threads: 48
Posting Whiz in Training
nmaillet is offline Offline
203 posts
since Aug 2008
Feb 17th, 2009
1

Re: Polygon formula [for graphics]

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) .
Reputation Points: 163
Solved Threads: 91
Posting Pro in Training
nucleon is offline Offline
476 posts
since Oct 2008
Feb 17th, 2009
0

Re: Polygon formula [for graphics]

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.
Reputation Points: 10
Solved Threads: 3
Junior Poster
Alex_ is offline Offline
175 posts
since Jun 2008
Feb 17th, 2009
0

Re: Polygon formula [for graphics]

tried some modifications
c++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 3
Junior Poster
Alex_ is offline Offline
175 posts
since Jun 2008
Feb 17th, 2009
0

Re: Polygon formula [for graphics]

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.
Reputation Points: 11
Solved Threads: 11
Junior Poster in Training
Alibeg is offline Offline
81 posts
since Aug 2008
Feb 17th, 2009
1

Re: Polygon formula [for graphics]

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.
Reputation Points: 11
Solved Threads: 11
Junior Poster in Training
Alibeg is offline Offline
81 posts
since Aug 2008
Feb 17th, 2009
0

Re: Polygon formula [for graphics]

Woaaah! That did it! I am very thankfull!! Super, just what i need!
Reputation Points: 10
Solved Threads: 3
Junior Poster
Alex_ is offline Offline
175 posts
since Jun 2008
Feb 17th, 2009
0

Re: Polygon formula [for graphics]

no prob, just dont forget to give the credits to a man who gave you the formula in the first place (nmaillet)
Reputation Points: 11
Solved Threads: 11
Junior Poster in Training
Alibeg is offline Offline
81 posts
since Aug 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Win32 text looks awful
Next Thread in C Forum Timeline: heap sort





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC