Java中如何计算Path2D的长度?

发布于 2024-08-27 08:11:32 字数 228 浏览 11 评论 0原文

我有一些由 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

沧桑㈠ 2024-09-03 08:11:32

首先使用 getPathIterator() 获取路径元素。如果路径只有 SEG_MOVETOSEG_LINETO 元素,则长度应该很容易计算。只需对所有 SEG_LINETO 求和 sqrt((X1-X2)^2 + (Y1-Y2)^2),其中点 (X1, Y1) 是前一个端点,(X2, Y2) ) 是 currentSegment(double[]) 返回的当前段。

如果它还包含 SEG_QUADTOSEG_CUBICTO 元素,则需要一个更复杂的公式,我现在不想弄清楚(可能需要微积分)。

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).

梦萦几度 2024-09-03 08:11:32

根据之前关于此主题的问题,文章计算三次贝塞尔曲线的弧长可能会提供一些见解。为了方便起见,您可能需要查看 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.

巷雨优美回忆 2024-09-03 08:11:32

首先,对我的英语感到抱歉......

我知道这是一个古老的话题,但也许它对某人有用。
我做的这个不是针对 CubicCurve 而是针对 QuadCurve:

public double CurveLength (QuadCurve curve){
    double xini = curve.getStartX();
    double yini = curve.getStartY();
    double xpoint = curve.getControlX();
    double ypoint = curve.getControlY();
    double xfin = curve.getEndX();
    double yfin = curve.getEndY();

    double ax = xini-(2*xpoint)+xfin;
    double ay = yini-(2*ypoint)+yfin;
    double bx = (2*xpoint)-(2*xini);
    double by = (2*ypoint)-(2*yini);
    double A = 4*((ax*ax)+(ay*ay));
    double B = 4*((ax*bx)+(ay*by));
    double C = (bx*bx)+(by*by);

    double Sabc = 2*(Math.sqrt(A+B+C));
    double A2 = Math.sqrt(A);
    double A32 = 2*A*A2;
    double C2 = 2*(Math.sqrt(C));
    double BA = B/A2;

    double length = ((A32*Sabc) + (A2*B*(Sabc-C2)) + (((4*C*A)-(B*B))*Math.log(((2*A2)+BA+Sabc)/(BA+C2))))/(4*A32);
    return length;
}

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 double CurveLength (QuadCurve curve){
    double xini = curve.getStartX();
    double yini = curve.getStartY();
    double xpoint = curve.getControlX();
    double ypoint = curve.getControlY();
    double xfin = curve.getEndX();
    double yfin = curve.getEndY();

    double ax = xini-(2*xpoint)+xfin;
    double ay = yini-(2*ypoint)+yfin;
    double bx = (2*xpoint)-(2*xini);
    double by = (2*ypoint)-(2*yini);
    double A = 4*((ax*ax)+(ay*ay));
    double B = 4*((ax*bx)+(ay*by));
    double C = (bx*bx)+(by*by);

    double Sabc = 2*(Math.sqrt(A+B+C));
    double A2 = Math.sqrt(A);
    double A32 = 2*A*A2;
    double C2 = 2*(Math.sqrt(C));
    double BA = B/A2;

    double length = ((A32*Sabc) + (A2*B*(Sabc-C2)) + (((4*C*A)-(B*B))*Math.log(((2*A2)+BA+Sabc)/(BA+C2))))/(4*A32);
    return length;
}
智商已欠费 2024-09-03 08:11:32

您可以使用该功能
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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文