Jump to content
  • Advertisement
Sign in to follow this  
wicked357

X, Y points to a circles radius

This topic is 2696 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am a little confused on this programming problem I have been asked to help with, so I thought I could gain some insight and clarification on this problem just for my sake alone. Lets say there is a circle of I guess any radius as the specific doesn't matter for the algorithm to finding the points. Lets say 2 for arguments sake. The goal is to output the x,y coordinates at a given angle, it doesn't state in the problem, but lets assume 0 is the center of the circle. It doesn't sound too hard, but I am having a hard time with how to calculate such determinations. Any help on this would be great I don't need code, but example would be great. Once I get a handle on the idea of doing this, I will post some code if I have some issues or to check if I got it right to my understanding. Thank you in advance.

Share this post


Link to post
Share on other sites
Advertisement
x = center_x + radius*cos(angle);
y = center_y + radius*sin(angle);


Is that what you are asking?

Share this post


Link to post
Share on other sites
That looks to be exactly what I was looking for thank you so much. I will put together some code to make sure it is working right and post it to be certain.

Share this post


Link to post
Share on other sites
Something to consider is when the circle is very small or very large. If it is very small and only takes up something like 10 pixels wide on the screen, you will have solved for many angles that have the same screen point. If the circle is very large on the screen, like 1000 pixels wide, you can end up with gaps where you would need to solve for non-integer angles in order to get a complete circle. This is assuming that you want to draw the circle on your screen.

Here is some old C# code that I had used to draw circles on the screen. It scans both x and y axis to alleviate the gaps that can result when solving for only 1 axis. I think the code looks terrible, its been a while since I did this and I just threw it together in order to make it work. I thought it would be good to share. It uses a point on the circle to determine radius (because thats what my solution at the time called for), but it could easily be changed to a radius parameter.



private void DrawCircle(Vect2D center, Vect2D pointOn, Color drawColor)
{
Bitmap viewBitmap = new Bitmap(pictureBox_View.Image);

int x, y;
Vect2D v = new Vect2D();
v = pointOn - center;
double r = v.Magnitude();
x = center.x - (int)r;

// draw along x
while (x < center.x + (int)r)
{
y = center.y + (int)Math.Sqrt((r * r) - (x - center.x) * (x - center.x));
if (x>=0 && x < viewBitmap.Width && y >= 0 && y < viewBitmap.Height )
viewBitmap.SetPixel(x, y, drawColor);
y = center.y - (int)Math.Sqrt((r * r) - (x - center.x) * (x - center.x));
if (x >= 0 && x < viewBitmap.Width && y >= 0 && y < viewBitmap.Height)
viewBitmap.SetPixel(x, y, drawColor);
x++;
}

// draw along y
y = center.y - (int)r;
while (y < center.y + (int)r )
{
x = center.x + (int)Math.Sqrt((r * r) - (y - center.y) * (y - center.y));
if (x >= 0 && x < viewBitmap.Width && y >= 0 && y < viewBitmap.Height)
viewBitmap.SetPixel(x, y, drawColor);
x = center.x - (int)Math.Sqrt((r * r) - (y - center.y) * (y - center.y));
if (x >= 0 && x < viewBitmap.Width && y >= 0 && y < viewBitmap.Height)
viewBitmap.SetPixel(x, y, drawColor);
y++;
}

pictureBox_View.Image = viewBitmap;
}

Share this post


Link to post
Share on other sites
This is what I have so far, but I noticed some issues, I cannot seem to calculate the milliseconds correctly and the timer has a little offset so sometimes it doubles up on the x,y coordinate output before time outputs. Every 1.2 seconds x,y outputs and every 1.4 time outputs via the format Hour/Minute/Seconds/Milliseconds, suggestions are welcomed.


#include <iostream>
#include <windows.h>
#include <math.h>
#include <time.h>

//A simple counter class using QueryPerformance
class Counter
{
public:
Counter()
{
counter = SystemTime();
}
~Counter()
{
}
//Return the time elapsed
double ElapsedTime()
{
return SystemTime() - counter;
}
//Reset the timer to equal current system time
void ResetTimer()
{
counter = SystemTime();
}
private:
double counter;
//Return the systemtime
double SystemTime()
{
LARGE_INTEGER frequency;
BOOL useHighPerformanceTimer = QueryPerformanceFrequency(&frequency);

LARGE_INTEGER currentTime;
QueryPerformanceCounter(&currentTime);

return static_cast<double>(currentTime.QuadPart) / frequency.QuadPart;
}
};

//Function to output the time in the designated format
void OutputTime(tm* value)
{
std::cout << value->tm_hour << ":" << value->tm_min << ":" << value->tm_sec << ":" << 1000 / value->tm_sec << std::endl;
}

//Function to output the coordinates based on angle input
void OutputCoordinates(const double& angle)
{
//coordinate = center(x/y) + radius * (cos/sin)(angle)
double x = 10 + 1 * cos(angle);
double y = 10 + 1 * sin(angle);
std::cerr << "X: " << x << " Y: " << y << " ";
}

int main()
{
//Timer objects
Counter timerA, timerB;
timerA.ResetTimer();
timerB.ResetTimer();

//Declare the circle
double angle = 0;

//Value to determine which increment to do
bool one = true;
bool four = false;
bool thirteen = false;
bool seven = false;
bool twenty = false;

//Local time structure
time_t raw;
struct tm* clock;
time(&raw);
clock = localtime(&raw);

//Main loop to run until "ESC" is pressed
bool isRunning = true;
while(isRunning)
{
//Update the time
time(&raw);
clock = localtime(&raw);

//Output the x,y coordinates given the angle and radius of the circle, than reset the timer
if(timerA.ElapsedTime() >= 1.2)
{
if(one)
{
angle += 1;
one = false;
four = true;
}
else if(four)
{
angle += 4;
one = false;
four = false;
thirteen = true;
}
else if(thirteen)
{
angle += 13;
thirteen = false;
seven = true;
}
else if(seven)
{
angle += 7;
seven = false;
twenty = true;
}
else if(twenty)
{
angle += 20;
twenty = false;
one = true;
angle = 1;
}
OutputCoordinates(angle);
timerA.ResetTimer();
}

//Output the time format of HH:MM:SS:MS every ~1.4 seconds, than reset the timer
if(timerB.ElapsedTime() >= 1.4)
{
OutputTime(clock);
timerB.ResetTimer();
}
}

return 0;
}

Share this post


Link to post
Share on other sites
It's also quite common to draw a circle as a series of straight lines between a given set of points around the circle. You can increase the number of points until the circle loses its angular look although it can be surprising how few points you need in order to fool the eye, depending on the size of the circle of course.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!