Stupid V3 geometry question
Displaying 1-7 of 7 total.
1
Please enter a numerical value for the importance of this sticky.
Enter 0 to unsticky.
Gayo

This is kind of embarrassing, but although I *should* be able to figure out how to do this on my own, it's stymied me long enough to ask. I'm probably not going to be able to describe it too well, so bear with me.

I have a circle. Given the coordinates of the circle, I want to be able to make a square that bounds the circle -- that is to say, each side of the square would touch the circle at a point, and that the slope of that side would be the tangent of the circle at that point. Of course you can just work this out by hand if you need it for a specific angle of square, but I want to be able to draw the square at any angle, which means I need a specific formula to determine the four lines of the square from the lines of the circle and an angle measurement.

I've poked around with this for a couple hours, but I won't bore with the minutiae of what I've done so far since it's probably fruitless. Any ideas?

Posted on 2004-04-06 00:30:41

mcgrue

Well, do you have the radius of the circle? And do you have the centerpoint of the circle?

It should be a trivial task if you do. otherwise it becomes more intresting.

Posted on 2004-04-06 00:43:28

Gayo

Yes.

Posted on 2004-04-06 01:03:12

mcgrue

okay, let's make this simple

assuming you have:

int center_x, int center_y, radius_length;

you want to find:
int topleft_x, topleft_y, botright_x, botright_y;

topleft_x = center_x - radius_length;
topleft_y = center_y - radius_length;

botright_x = topleft_x + (radius_length*2);
botright_y = topleft_y + (radius_length*2);

and then to draw the rectangle:

Rect(topleft_x, topleft_y, botright_x, botright_y, myColor, screen);

That should be right, but I'm not testing it or anything.

Posted on 2004-04-06 01:33:11

Gayo

That only gives me the square in that one position, though. If I want, say, a square where each point is rotated 17 degrees from that position, it's trickier.

Edit: Well, I found a messy way to do it, but it only works with squares and isn't generally applicable to drawing the tangent line intersecting a circle at a given point.

Posted on 2004-04-06 01:35:45 (last edited on 2004-04-06 01:52:39)

vecna

How about:

Draw a circle. Find the endpoints of the 'easy' rectangle.

Take your angle and rotate each of those four points by that angle. Simple sin/cos.

Use linedraws to draw the rectangle. If you want it filled, you can use Triangle().

Posted on 2004-04-06 02:46:10

Gayo

That would work! What I ended up doing was finding the square root of 2, and multiplying that by the radius of the circle. That then gave me the length of the lines from the centre of the circle to the corners of the square, and I could find those with sin/cos and then join them.

Posted on 2004-04-07 02:43:48


Displaying 1-7 of 7 total.
1
 
Newest messages

Ben McGraw's lovingly crafted this website from scratch for years.
It's a lot prettier this go around because of Jon Wofford.
Verge-rpg.com is a member of the lunarnet irc network, and would like to take this opportunity to remind you that regardless how babies taste, it is wrong to eat them.