![]() |
|
#1
|
|||
|
|||
Circle meets squareI 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
|
||||
|
||||
Re: circle meets squareQuote:
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
|
||||
|
||||
Re: Circle meets squareWithout 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 GIDBlog
Accepted for Ph.D. program by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
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