[SOLVED] intersection point between a QGraphicsEllipceItem and a QLineF in a GraphicsScene
-
-
You also need to be aware that there will be one, two, or no intersections of a line (infinite) and ellipse... As a general rule having only one intersection will be rare because the line has to be perfectly tangential to the ellipse. Once you find the intersections of the infinite line then you can check if either falls on the segment of interest i.e. the part corresponding to your line item.
-
Well this my code to determine the intersection point :
@
QPointF Line::intersectionPoint1(QGraphicsItem * circle, QLineF * line)
{float R= 20; float x0 = (circle->mapToScene(circle->shape().controlPointRect().center())).x(); float y0 = (circle->mapToScene(circle->shape().controlPointRect().center())).y();
if(line->x2() != line->x1())
{
QPointF yaxis_intersection;
line->intersect( QLineF(QPointF(0, 10000), QPointF(0, -10000)), &yaxis_intersection);float a = (line->y2() - line->y1())/(line->x2() - line->x1()); float b = yaxis_intersection.y(); float A = 1 + a*a; float B = 2*(a*b - a*y0 - x0); float C = x0 * x0 + y0*y0 + b*b - 2*b*y0 - R*R; float Q = B*B - 4*A*C; if(Q > 0) { float s1 = (-1)*(B + sqrt(Q))/(2*A); float s2 = (sqrt(Q) - B)/(2*A); QPointF ps1(s1, a*s1 + b); QPointF ps2(s2, a*s2 + b);
if(contains(ps1))
return ps1;
else
return ps2;
}
else
{
float s0 = (-1)B/(2A);
return QPointF(s0, as0 + b);
}
}
else
{
float x = line->x1();
// yy - 2y0y + (x - x0)(x - x0) + y0y0 - R*R = 0float C = (x - x0)*(x - x0) + y0*y0 - R*R; float Q = 4*y0*y0 - 4*C; if(Q > 0) { float s1 = y0 - sqrt(Q)/2; float s2 = y0 + sqrt(Q)/2 ; QPointF ps1(x, s1); QPointF ps2(x, s2);
if(contains(ps1))
return ps1;
else
return ps2;
}
else
{
return QPointF(x, y0);
}
}}
@