Java – detect self crossing in closed Bezier curves
I created a "blob" shape by patching the cubic Bezier curve together (screenshot below) I want to be able to detect that the curve has crossed itself or another curve, and want to know if there is a recommended method or known algorithm to perform this operation?
One idea is to use the flatteningpathiterator to decompose the shape into linear segments and then detect whether a given segment intersects another segment, but I am interested in whether there is a better method (because it will have quadratic performance) If I pursue this method, is the library function in Java to detect whether the two line segments overlap?
thank you.
No cross
No Crossover http://www.freeimagehosting.net/uploads/7ad585414d.png
overlapping
Crossover http://www.freeimagehosting.net/uploads/823748f8bb.png
Solution
I actually found a job using the built-in Java2D function @ R_ 419_ 2422 @ case, and very fast
Just create a path2d in the curve, then create an area in path2d, and call area Issingular() method;
It works... See this little example
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Area;
import java.awt.geom.CubicCurve2D;
import java.awt.geom.Path2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Test {
@SuppressWarnings("serial")
public static void main(String[] args) {
JFrame f = new JFrame("Test");
JPanel c = new JPanel() {
Area a;
Path2D p;
{
p = new Path2D.Double();
p.append(new CubicCurve2D.Double(0,100,150,50,200,100),true);
p.append(new CubicCurve2D.Double(200,true);
p.append(new CubicCurve2D.Double(100,0),true);
a = new Area(p);
setPreferredSize(new Dimension(300,300));
}
@Override
protected void paintComponent(Graphics g) {
g.setColor(Color.black);
((Graphics2D)g).fill(p);
System.out.println(a.isSingular());
}
};
f.setContentPane(c);
f.setDefaultCloSEOperation(JFrame.EXIT_ON_CLOSE);
f.pack();
f.setVisible(true);
}
}
