问题
Is there an easy way to approximate the points (if any) where two instances of QuadCurve2D
intersect?
That is, how could I compute the coordinates of the red dots in this diagram? There is no obvious method in QuadCurve2D
to do this.
(note: the points are not exact as I have tweaked them manually for the diagram. Also note the "missing" fourth point which does not lie on the curve segment even though it lies on the (infinite) parabola.)
These two curve segments were created with the following code:
QuadCurve2D curve1 = new QuadCurve2D.Double(-2.00, -2.00, +0.75, +4.75, +2.00, -0.75);
QuadCurve2D curve2 = new QuadCurve2D.Double(-2.50, -0.75, +5.50, -0.50, +0.50, +1.25);
Note 2: I would also like to be able to intersect a straight line & a quadratic curve, but I assume this can be handled by setting one of the control points to be colinear with the endpoints.
回答1:
Depends on whether you're interested in approximate or in exact solutions (up to double precision). For approximate ones, you could simply parametrize the curves as some function f(t) and then do some interval nesting to find values for t which minimize the distance between the curves.
For exact solutions, you'd have to find the four points where two conic sections intersect. There is a short paragraph about this on wikipedia. The book Perspectives on Projective Geometry has a longer section explaining the details. There certainly are implementations available for various languages; the one for Asymptote comes to my mind just now. The implementation for its general case looks quite frightening, though, so it might be they are doing something overly complicated there.
Once you have all four points of intersection, you'd still have to decide which of them are on the part of the conic section delimited by the endpoints of your QuadCurve, but that should be simple in comparison. So in total, you'd have these three steps:
- Compute matrix of conic sections section from endpoints and control points
- Intersect conic sections using degenerat elements in their pencil
- Decide whether these intersections lie between the endpoints
If you have a problem with the mathematical details of one of these steps, it might be better to ask on the mathematics stack exchange. Not only have people there more experience with solving mathematical problems, the MathJax features for typesetting math will make the answers far more readable than an answer here could hope to be.
As to your note 2 about the straight line: that is easier by far, because if you express that problem in terms of coordinates, you'd only end up with a quadratic equation, instead of an equation of degree 4 for the naive approach to the general problem, and still degree 3 if you solve it as described by the above references. One can write the general aproach in such a way that intersecting a conic with a line is a step in the solution, so having a method for this might work well enough.
回答2:
A pragmatic approach would be creating two Area
s by adding the curves and closing the results, The intersection of these Area
s should have all the original points of intersection as endpoints of some segments. So iterate over the resulting path, disregard any Bézier control points, and for the each segment end point encountered, check whether it lies on the original curves.
Or have a look at the implementation of how Area
does this, and see if you can adapt this to your needs. If your licensing allows including GPL2 code.
来源:https://stackoverflow.com/questions/11479664/find-the-intersections-of-a-pair-of-quadcurve2ds