| | |
Polygon formula [for graphics]
Thread Solved |
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!
Can someone help?Pls!
•
•
Join Date: Aug 2008
Posts: 83
Reputation:
Solved Threads: 18
If my math is correct, where
C Syntax (Toggle Plain Text)
x(n) = x + r*cos(a + n*pass) y(n) = y + r*sin(a + n*pass)
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. Sort of ! But not really..
This is what i wrote
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:
This is what i wrote
C Syntax (Toggle Plain Text)
void polygon(int x, int y, int n, int r){ int k=0, *poly; double pass= 360/n, _n=0,pass2=n*pass; poly=new int[n]; while(k<n*2){ poly[k++]=x +r * cos(pass2); poly[k++]=y +r * sin(pass2); pass2+=pass; } drawpoly(n,poly); }
This is the output,at:
Last edited by Alex_; Feb 17th, 2009 at 4:02 pm.
tried some modifications
check:
c++ Syntax (Toggle Plain Text)
void polygon(int x, int y, int n, int r){ int k=0, *poly; double pass= 2* M_PI/n,pass2=n*pass, a=360/n; poly=new int[n]; while(k<n*2){ pass2+=a; poly[k++]=x +r * cos(pass2); poly[k++]=y +r * sin(pass2); } drawpoly(n,poly); }
Last edited by Alex_; Feb 17th, 2009 at 4:01 pm.
•
•
Join Date: Aug 2008
Posts: 71
Reputation:
Solved Threads: 10
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
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.
•
•
Join Date: Aug 2008
Posts: 71
Reputation:
Solved Threads: 10
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
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.
![]() |
Other Threads in the C Forum
- Previous Thread: Win32 text looks awful
- Next Thread: Code::blocks compilation error
| Thread Tools | Search this Thread |
#include * ansi array arrays asterisks bash binarysearch calculate centimeter changingto char character convert copyanyfile copyimagefile creafecopyofanytypeoffileinc createprocess() database dynamic execv fgets file floatingpointvalidation fork framework function getlogicaldrivestrin givemetehcodez grade gtkwinlinux histogram ide inches include infiniteloop initialization input interest intmain() iso keyboard km license linked linkedlist linux list looping lowest matrix meter microsoft number oddnumber open opendocumentformat openwebfoundation owf pdf pointer pointers posix power probleminc process program programming pyramidusingturboccodes radix read recursion recv recvblocked research reversing scheduling segmentationfault send sequential single socket socketprogramming standard strchr string suggestions systemcall test testautomation testing threads turboc unix urboc user variable whythiscodecausesegmentationfault win32api windowsapi





(nmaillet) 