-VectorSolutions RS_Information::getIntersectionLineEllipse(RS_Line* line,
- RS_Ellipse* ellipse) {
-
- VectorSolutions ret;
-
- if (line==NULL || ellipse==NULL) {
- return ret;
- }
-
- // rotate into normal position:
- double ang = ellipse->getAngle();
-
- double rx = ellipse->getMajorRadius();
- double ry = ellipse->getMinorRadius();
- Vector center = ellipse->getCenter();
- Vector a1 = line->getStartpoint().rotate(center, -ang);
- Vector a2 = line->getEndpoint().rotate(center, -ang);
- Vector origin = a1;
- Vector dir = a2-a1;
- Vector diff = origin - center;
- Vector mDir = Vector(dir.x/(rx*rx), dir.y/(ry*ry));
- Vector mDiff = Vector(diff.x/(rx*rx), diff.y/(ry*ry));
-
- double a = Vector::dotP(dir, mDir);
- double b = Vector::dotP(dir, mDiff);
- double c = Vector::dotP(diff, mDiff) - 1.0;
- double d = b*b - a*c;
-
- if (d < 0) {
- RS_DEBUG->print("RS_Information::getIntersectionLineEllipse: outside 0");
- } else if ( d > 0 ) {
- double root = sqrt(d);
- double t_a = (-b - root) / a;
- double t_b = (-b + root) / a;
-
- /*if ( (t_a < 0 || 1 < t_a) && (t_b < 0 || 1 < t_b) ) {
- if ( (t_a < 0 && t_b < 0) || (t_a > 1 && t_b > 1) ) {
- RS_DEBUG->print("RS_Information::getIntersectionLineEllipse: outside 1");
- }
- else {
- RS_DEBUG->print("RS_Information::getIntersectionLineEllipse: inside 1");
- }
- } else {*/
- RS_DEBUG->print("RS_Information::getIntersectionLineEllipse: intersection 1");
- Vector ret1(false);
- Vector ret2(false);
- //if ( 0 <= t_a && t_a <= 1 ) {
- //RS_DEBUG->print("RS_Information::getIntersectionLineEllipse: 0<=t_a<=1");
- ret1 = a1.lerp(a2, t_a);
- RS_DEBUG->print("RS_Information::getIntersectionLineEllipse: ret1: %f/%f", ret1.x, ret1.y);
- //}
- //if ( 0 <= t_b && t_b <= 1 ) {
- //RS_DEBUG->print("RS_Information::getIntersectionLineEllipse: 0<=t_b<=1");
- ret2 = a1.lerp(a2, t_b);
- RS_DEBUG->print("RS_Information::getIntersectionLineEllipse: ret2: %f/%f", ret2.x, ret2.y);
- //}
- if (ret1.valid && ret2.valid) {
- ret = VectorSolutions(ret1, ret2);
- }
- else {
- if (ret1.valid) {
- ret = VectorSolutions(ret1);
- }
- if (ret2.valid) {
- ret = VectorSolutions(ret2);
- }
- }
- //}
- } else {
- double t = -b/a;
- if ( 0 <= t && t <= 1 ) {
- RS_DEBUG->print("RS_Information::getIntersectionLineEllipse: 0<=t<=1");
- RS_DEBUG->print("RS_Information::getIntersectionLineEllipse: intersection 2");
- ret = VectorSolutions(a1.lerp(a2, t));
- RS_DEBUG->print("RS_Information::getIntersectionLineEllipse: ret1: %f/%f", ret.get(0).x, ret.get(0).y);
- } else {
- RS_DEBUG->print("RS_Information::getIntersectionLineEllipse: outside 2");
- }
- }
-
- ret.rotate(center, ang);
- return ret;
-
-
-
- /*
- RS_Arc* arc = new RS_Arc(NULL,
- RS_ArcData(ellipse->getCenter(),
- ellipse->getMajorRadius(),
- ellipse->getAngle1(),
- ellipse->getAngle2(),
- false));
- RS_Line* other = (RS_Line*)line->clone();
- double angle = ellipse->getAngle();
- //double ratio = ellipse->getRatio();
-
- // rotate entities:
- other->rotate(ellipse->getCenter(), -angle);
- other->scale(ellipse->getCenter(), Vector(1.0, 1.0/ellipse->getRatio()));
-
- ret = getIntersectionLineArc(other, arc);
-
- ret.scale(ellipse->getCenter(), Vector(1.0, ellipse->getRatio()));
- ret.rotate(ellipse->getCenter(), angle);
-
- delete arc;
- delete other;
-
- return ret;
- */
-}