GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 12-Feb-2009, 18:02
scuzzo scuzzo is offline
Awaiting Email Confirmation
 
Join Date: Jul 2007
Posts: 19
scuzzo is an unknown quantity at this point

Circle meets square


I am trying to accomplish :
reads a circle (x-y coordinates of the center and the radius),
reads a rectangle (x-y coordinates of the bottom-left corner and the top-right corner),
determines whether the circle meets the edges of the rectangle. If yes, the program should print the x-y coordinates of all points at which the circle meets the rectangle.
I'm completely stuck, anyone have any ideas that could help me?
  #2  
Old 12-Feb-2009, 19:28
Mexican Bob's Avatar
Mexican Bob Mexican Bob is offline
Member
 
Join Date: Mar 2008
Location: Chicxulub, Yucatán
Posts: 226
Mexican Bob is a jewel in the roughMexican Bob is a jewel in the roughMexican Bob is a jewel in the rough

Re: circle meets square


Quote:
Originally Posted by scuzzo
I am trying to accomplish :
reads a circle (x-y coordinates of the center and the radius),
reads a rectangle (x-y coordinates of the bottom-left corner and the top-right corner),
determines whether the circle meets the edges of the rectangle. If yes, the program should print the x-y coordinates of all points at which the circle meets the rectangle.
I'm completely stuck, anyone have any ideas that could help me?

You must first decide on a coordinate system. If the intersection of x and y is (0,0), does the positive values for y extend in the N direction from the origin and the positive values for x extend in the E direction from the origin?

The maximum number of points that a square can intercept a circle on the same plane are 8. If you get more than that, something is wrong with your program!


MxB
  #3  
Old 13-Feb-2009, 07:31
Blake's Avatar
Blake Blake is offline
Member
 
Join Date: Nov 2005
Posts: 255
Blake is a jewel in the roughBlake is a jewel in the rough

Re: Circle meets square


Without loss of generality, you can assume that the circle is at the origin, since you can transform all the points to place the origin at the center of the circle. It will make the computations a bit cleaner if you do that.

If (xl,yb) and (xr, yt) are the bottom-left and top-corners of the rectangle, then the sides of the rectangle define four lines, namely the horizontal lines y = yb and y = yt and the vertical lines x = xl and x = xr. You should first find the interesections of the circle with these lines, and then check if the points of intersection are actually on the line segments that make up the sides of the rectangle.

If r is the radius of the circle, then the parametric equation of the circle is c(t) = (r*cos(t), r*sin(t)). To check if c(t) intersects a vertical line x = x0, first make sure that |x0| <= r. If x0 > r, there is no intersection. Otherwise, you can find the intersection by solving r*cos(t) = x0, which gives you t = acos(x0/r). Thus the point of intersection is (x0, sin(acos(x0/r)).

For a horizontal line y = y0, first make sure that |y0| <= r. If so, the equation you need to solve is r*sin(t) = y0, which gives you y0 = asin(y0/r). Thus the point of intersection is (cos(asin(y0/r), y0).

Use the above formulas to find the point(s) of intersection of the circle and the four lines defined by the sides of the rectangle. If the circle intersects one of the vertical lines, check if the y-coordinate of the point of intersection is between yb and yt. Id the circle intersects one of the horizontal lines, check if the x-coordinate of the point of intersection is between xl and xr.

You could also write the equation of the circle as x^2 + y^2 = r^2, and use that to solve for the points of intersection. That would probably be a more efficient, but you would need to be careful with the signs of square roots.
__________________
www.blake-foster.com
 
 

Recent GIDBlogAccepted for Ph.D. program by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Is ellipse only way to do circle? juned C++ Forum 3 27-Dec-2008 18:26
Need Help on Dealing with Archives. VersEtreOuNe C++ Forum 15 14-Feb-2008 06:31
Inheritance and classes mortomarty C++ Forum 2 29-Sep-2007 07:09
coordinates of a circle IRONMAN C++ Forum 2 02-Jun-2006 14:31
Convert the input into square yards pjpav Java Forum 1 08-Oct-2005 08:43

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 23:55.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.