Click to See Complete Forum and Search --> : Round Rect corners intersection with a line
Maximus_X
December 5th, 2006, 05:29 AM
I'm looking to find the intersection points of a line with a round rectangle.
The line ecuation it's something like this: y = tan (-2.04 + angleStep*I) * x
I have no problems with the intersection of the lines but with the round rectangle corners I'm not sure that my ideea it's the best.
I have search on net a lot and the most important reference that I found it's http://www.functionx.com/visualc/gdi/circles.htm ( Round Rectangles and Round Squares section ).
As we can see here, we may get the round rectangle shape by merging of a normal rectangle with four similar ellipse with it center situated on a (X3,Y3) position.
Going on this ideea and taking in consider that an ellipse ecuation it's something like this: (x-x0)^2/rx^2 + (y-y0)^2/ry^2 = 1 ;
I think that I must solve a two grade equation system.
I'm asking if there is a best ideea than that. Any advice or sugestion it's appreciated.
Regards,
Silviu alias Maximus_X.
Maximus_X
December 5th, 2006, 09:39 AM
Meantime I solved that equation system in a test application and I put the code of application.
My equations are:
y = tan(alfa_angle)*x
(x-x0)^2 / rx^2 + (y-y0)^2 / ry^2 = 1
#include "stdafx.h"
#include <iostream>
#include <math.h>
#include <conio.h>
#define rl64_PI ((double) 3.1415926535897932384626433832795)
int _tmain(int argc, _TCHAR* argv[])
{
double Y1 = 0.0, X1 = 0.0, Y2 = 0.0, X2 = 0.0;
double AlfaAngleGrad = 35.0, AlfaAngleRad = 0.0f;
double X0 = 30.0, Y0 = 30.0;
double Rx = 20.0, Ry = 10.0;
double Delta = 0.0;
AlfaAngleRad = (AlfaAngleGrad * rl64_PI) / 180;
Delta = 4*(X0*Ry*Ry + Y0*Rx*Rx*tan(AlfaAngleRad) ) * ( X0*Ry*Ry + Y0*Rx*Rx*tan(AlfaAngleRad) ) -
4 * (Ry*Ry + Rx*Rx*tan(AlfaAngleRad)*tan(AlfaAngleRad)) * (Ry* Ry* X0* X0 + Rx* Rx* Y0* Y0 - Rx* Rx * Ry* Ry);
std::cout << "Find if a line intersects a ellipse" << std::endl;
if (Delta >= 0)
{
X1 = (2*X0*Ry*Ry + 2*Y0*Rx*Rx*tan(AlfaAngleRad) - sqrt(Delta)) / (2*Ry*Ry + 2*Rx*Rx*tan(AlfaAngleRad)*tan(AlfaAngleRad));
Y1 = X1 * tan(AlfaAngleRad);
X2 = (2*X0*Ry*Ry + 2*Y0*Rx*Rx*tan(AlfaAngleRad) + sqrt(Delta)) / (2*Ry*Ry + 2*Rx*Rx*tan(AlfaAngleRad)*tan(AlfaAngleRad));
Y2 = X2 * tan(AlfaAngleRad);
std::cout << "Results";
std::cout << " X1: "<< X1 << " Y1: "<< Y1<< std::endl;
std::cout << " X2: "<< X2 << " Y2: "<< Y2<< std::endl;
}
else
{
std::cout << "Ireal solution! Complex number solution. DELTA = " << Delta << std::endl;
}
_getch();
return 0;
}
If you have other solution to find the intersection of a line with an ellipse I'm glad to hear you. ;)
Kind Regards!
Yves M
December 5th, 2006, 12:14 PM
Well, I would definitely check first whether the line actually intersects the "full" rectangle (i.e. with sharp corners). This is because there cannot be a case where it would intersect the rounded rect and not the full rect. And then only in that case, check for the real intersection. The main aim is to speed it up of course, because intersection with a rectangle is much easier to calculate than with the rounded corners.
Then check where the intersection occurs and whether it's on one of the rectangle's straight sides (i.e. to the left of the starting point of the ellipse or not). If so, there is an intersection and you're done.
If not, and only in this case you have to check for the intersection with the ellipse.
Here it is in pictures:
The rounded rectangle with the two "transition points"
http://www.codeguru.com/forum/attachment.php%3Fattachmentid=18025
A line intersection with the rectangle part
http://www.codeguru.com/forum/attachment.php%3Fattachmentid=18026
A line outside the rectangle part
http://www.codeguru.com/forum/attachment.php%3Fattachmentid=18027
A line inside the rectangle part but outside the ellipse part
Note that here, you know that you have to check the ellipse part because it's outside both the cutoff points
http://www.codeguru.com/forum/attachment.php%3Fattachmentid=18028
Inside the rectangle part and inside the ellipse part
Again, in this case it's outside both cutoff points, so you have to check the intersection with the ellipse.
http://www.codeguru.com/forum/attachment.php%3Fattachmentid=18029
Maximus_X
December 6th, 2006, 02:55 AM
Well, I would definitely check first whether the line actually intersects the "full" rectangle (i.e. with sharp corners). This is because there cannot be a case where it would intersect the rounded rect and not the full rect. And then only in that case, check for the real intersection. The main aim is to speed it up of course, because intersection with a rectangle is much easier to calculate than with the rounded corners.
Then check where the intersection occurs and whether it's on one of the rectangle's straight sides (i.e. to the left of the starting point of the ellipse or not). If so, there is an intersection and you're done.
If not, and only in this case you have to check for the intersection with the ellipse.
Hi Yves M! First of all thanks for your answer and advices. That's what I was thinking, too.
I have a classes hierarchy that handles the intersection of a line with different shapes.
The first concrete class handles the intersection of a line with a normal rectangle and now I'm working to the class the handles the line intersection with a round rectangle.
I have started from the ideea of normal rectangle intersection and on the round rectangle corners I handle the the intersection with corner's ellipse (then I need to solve upper equation system).
Mean-time I found other ideea: the building of a round rectangle by using a polygon and then you need to handle the intersection between two lines. But for this moment this it's just a second solution.
I'm opening to new ideeas.
Kind Regards,
Silviu alias Maximus_X
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.