Random Point on a Circle

Talk about making games.

Random Point on a Circle

Postby CrackedP0t » Wed Jan 29, 2014 12:02 am

I need a way to get the X and Y coordinates of random point on a circle with an origin of (0,0) and a radius of 420px.

This gets 1/2 of the circle.

Code: Select all
const int sr = 420; // Radius
int sx = sr - rand(sr * 2); // X-coordinate
int sy = sqrt(pow(sr, 2) + pow(sx, 2)); // Y-coordinate
/人 ◕‿‿◕ 人\
Fang Pictures, my game development company:
http://fangpictures.comuf.com
User avatar
CrackedP0t
 
Posts: 157
Joined: Mon Dec 30, 2013 5:49 pm
Location: Crested Butte, CO (The US of A)
Score: 8 Give a positive score

Re: Random Point on a Circle

Postby skydereign » Thu Jan 30, 2014 2:30 am

Take the center, a random angle, and a random radius. Then use sin/cos to find the coordinates.
Code: Select all
double ang = rand(360);
double radius = rand(420);
int sx = 0 + cos(ang)*radius;
int sy = 0 + sin(ang)*radius;
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: Random Point on a Circle

Postby CrackedP0t » Thu Jan 30, 2014 2:54 am

Thanks!
This made my game very hard... heehee.
/人 ◕‿‿◕ 人\
Fang Pictures, my game development company:
http://fangpictures.comuf.com
User avatar
CrackedP0t
 
Posts: 157
Joined: Mon Dec 30, 2013 5:49 pm
Location: Crested Butte, CO (The US of A)
Score: 8 Give a positive score


Return to Game Development

Who is online

Users browsing this forum: No registered users and 1 guest