Java中如何计算Path2D的长度?
我有一些由 Path2D 表示的路径。路径由多个相互连接的 CubicCurve2D 或 Line2D 线段组成。我想计算或获取路径从起点到终点的长度。我该如何计算或得到它?是否可以?我已经检查了API文档 ,但找不到任何有用的方法。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
首先使用
getPathIterator()
获取路径元素。如果路径只有SEG_MOVETO
和SEG_LINETO
元素,则长度应该很容易计算。只需对所有SEG_LINETO
求和 sqrt((X1-X2)^2 + (Y1-Y2)^2),其中点 (X1, Y1) 是前一个端点,(X2, Y2) ) 是 currentSegment(double[]) 返回的当前段。如果它还包含
SEG_QUADTO
或SEG_CUBICTO
元素,则需要一个更复杂的公式,我现在不想弄清楚(可能需要微积分)。Start by using
getPathIterator()
to get the path elements. If the path only hasSEG_MOVETO
andSEG_LINETO
elements, the length should be easy to calculate. Just sum sqrt((X1-X2)^2 + (Y1-Y2)^2) for all of theSEG_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
orSEG_CUBICTO
elements, that will require a more complicated formula that I don't care to figure out right now (may require calculus).根据之前关于此主题的问题,文章计算三次贝塞尔曲线的弧长可能会提供一些见解。为了方便起见,您可能需要查看 JScience
Polynomial< /code>
类。此外,这个近似值基于相同的文章,可能会有所帮助。
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.首先,对我的英语感到抱歉......
我知道这是一个古老的话题,但也许它对某人有用。
我做的这个不是针对 CubicCurve 而是针对 QuadCurve:
First of all sorry for my english...
I know this is an old topic but perhaps it's useful for someone.
I made this, is not for a CubicCurve but for a QuadCurve:
您可以使用该功能
public PathIterator getPathIterator(AffineTransform at, 双平坦度);
这将给出一个仅包含 SEG_MOVETO、SEG_LINETO 和 SEG_CLOSE 段的“flattenend”迭代器。
You can use the function
public PathIterator getPathIterator(AffineTransform at, double flatness);
This will give a 'flattenend' iterator containing only SEG_MOVETO, SEG_LINETO, and SEG_CLOSE segments.