views:

125

answers:

2

I have some paths represented by Path2D. The Path consist of multiple CubicCurve2D or Line2D segments that are connected to each other. I would like to calculate or get the length from the start to the end of a Path. How can I calculate it or get it? Is it possible? I have checked the API documentation, but couldn't find any useful methods.

+1  A: 

Start by using getPathIterator() to get the path elements. If the path only has SEG_MOVETO and SEG_LINETO elements, the length should be easy to calculate. Just sum sqrt((X1-X2)^2 + (Y1-Y2)^2) for all of the SEG_LINETO, where point (X1, Y1) was the previous endpoint, and (X2, Y2) is the current one returned by currentSegment(double[]).

If it also contains SEG_QUADTO or SEG_CUBICTO elements, that will require a more complicated formula that I don't care to figure out right now (may require calculus).

ILMTitan
+1 for checking the type to simplify calculations. I initially favored the `Polynomial` class for it's calculus methods, but the arc length integral has no closed form solution. :-(
trashgod
+2  A: 

In light of a previous question on this topic, the article Computing the Arc Length of Cubic Bezier Curves may offer some insight. For convenience, you may want to look at the JScience Polynomial class. Also, this approximation, based on the same article, may help.

trashgod