当给定距起点的距离时,找到 Bézier 曲线上的点?

发布于 2024-12-10 08:21:39 字数 388 浏览 0 评论 0原文

我创建了一条 4 点贝塞尔曲线和一个距离。从起点开始,如何找到距起点一定距离的点的 x,y 坐标?

我查看了其他示例,据我所知,他们通过将曲线划分为几千个点,然后找到最近的点来近似值。这对我不起作用。对于我正在做的事情,我希望精确到小数点后两位。下面是我创建贝塞尔曲线的简单形式。 (y 值是任意的,x 值始终相距 352 像素)。如果重要的话,我正在用 Java 工作。

path.moveTo(0, 400);
path.curveTo(352, 480, 704, 590, 1056, 550);

因此,假设我的起点是 0,400,我如何找到距起点 35 距离(沿曲线)的点的坐标? (理想情况下不是处理器密集型的。这可能最终必须每秒运行 200 次)

I created a 4 point Bézier curve, and a distance. Starting at the start point, how do I find the x,y coordinates of a point which is that distance away from the start point?

I've looked at the other examples, and from what I can tell, they approximate the values by dividing the curve into several thousand points, then finding the nearest point. This will not work for me. For what I'm doing, I'd like to be accurate to only two decimal places. Below is a simple form of what I have to create my Bézier curve. (The y values are arbitrary, the x values are always 352 pixels apart). If it matters, I'm working in Java.

path.moveTo(0, 400);
path.curveTo(352, 480, 704, 590, 1056, 550);

So assuming my start point is 0,400, how do I find the coordinates of a point that is 35 distance form that start point(along the curve)? (Ideally something not processor intensive. This may end up having to run 200 times per second)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

月依秋水 2024-12-17 08:21:39

对于任何碰巧发现我的问题的人来说,我解决了我自己的问题。要找到曲线的总距离,请将其分成 1000 个左右的部分(仍然相当准确),找到每个点之间的距离,然后将它们全部加在一起。 (您应该使用参数公式)

现在找到沿曲线的百分比。 = distance/totalLengthOfCurve

使用这个百分比作为 x 和 y 的新 t 值,现在您就有了新的 x 和 y 位置。

重要提示:这是一种奇怪的情况,但如果您的 t 值大于 1,请使用绝对值。当您对其进行立方时,该值将为负数...=发生不好的事情。

丑陋但相关的代码如下所示。

将曲线分成 1000 段

    for (double t = 0.00; t < 1.001; t= t + .001) {
         double xValue = Math.pow((1-t), 3) * point1x + 3 * Math.pow((1-t), 2) * t * point2x + 3 * (1-t) * Math.pow(t, 2) * point3x + Math.pow(t, 3) * point4x;
         double yValue = Math.pow((1-t), 3) * point1y + 3 * Math.pow((1-t), 2) * t * point2y + 3 * (1-t) * Math.pow(t, 2) * point3y + Math.pow(t, 3) * point4y;

**现在是计算每个点之间的距离的时候。我建议将上述计算出的值放入数组中并循环遍历。

计算 x 和 y 位置

    xPos = Math.abs(Math.pow((1 - percenttraveled), 3)) * point1x + 3 * Math.pow((1 - percenttraveled), 2) * percenttraveled * point2x + 3 * Math.abs((1 - percenttraveled)) * Math.pow(percenttraveled, 2) * point3x + Math.abs(Math.pow(percenttraveled, 3)) * point4x;
    yPos = Math.abs(Math.pow((1 - percenttraveled), 3)) * point1y + 3 * Math.pow((1 - percenttraveled), 2) * percenttraveled * point2y + 3 * Math.abs((1 - percenttraveled)) * Math.pow(percenttraveled, 2) * point3y + Math.abs(Math.pow(percenttraveled, 3)) * point4y;

For anyone that happens to find my question, I solved my own problem. To find the total distance of your curve, break it into 1000 or so pieces(still fairly accurate), find the distance between each point, then add them all together. (you should be using the parametric formula)

Now find the percent of the way along your curve. = distance/totalLengthOfCurve

Use this percent as your new t value for x and y, and now you have your new x and y positions.

IMPORTANT: This is an odd case, but make to to use the absolute value if your t value will ever be greater than 1. When you cube it, that value would be negative...=bad things happen.

Ugly, but relevant code shown below.

Breaking the curve into 1000 pieces

    for (double t = 0.00; t < 1.001; t= t + .001) {
         double xValue = Math.pow((1-t), 3) * point1x + 3 * Math.pow((1-t), 2) * t * point2x + 3 * (1-t) * Math.pow(t, 2) * point3x + Math.pow(t, 3) * point4x;
         double yValue = Math.pow((1-t), 3) * point1y + 3 * Math.pow((1-t), 2) * t * point2y + 3 * (1-t) * Math.pow(t, 2) * point3y + Math.pow(t, 3) * point4y;

**Now is when you calculate the distance between each point. Id suggest putting the above values calculated into an array and looping through.

Calculating the x and y positions

    xPos = Math.abs(Math.pow((1 - percenttraveled), 3)) * point1x + 3 * Math.pow((1 - percenttraveled), 2) * percenttraveled * point2x + 3 * Math.abs((1 - percenttraveled)) * Math.pow(percenttraveled, 2) * point3x + Math.abs(Math.pow(percenttraveled, 3)) * point4x;
    yPos = Math.abs(Math.pow((1 - percenttraveled), 3)) * point1y + 3 * Math.pow((1 - percenttraveled), 2) * percenttraveled * point2y + 3 * Math.abs((1 - percenttraveled)) * Math.pow(percenttraveled, 2) * point3y + Math.abs(Math.pow(percenttraveled, 3)) * point4y;
好倦 2024-12-17 08:21:39

javagraphics 库具有 MeasuredShape (https:// /javagraphics.java.net/doc/com/bric/geom/MeasuredShape.html) 类,它提供了 getPoint 方法来执行此操作。它还具有一些非常方便的方法来获取子路径和切线角度。据我所知,他们正在“正确”地实现路径逻辑,而不是诉诸于破坏路径。

我已经在一个需要这种几何计算的项目中使用了库的这一部分,并且它似乎工作得很好。

The javagraphics library has the MeasuredShape (https://javagraphics.java.net/doc/com/bric/geom/MeasuredShape.html) class which provides the getPoint method to do just this. It also has some very handy methods for getting subpaths and tangent angles. As far as I can tell, they are implementing the path logic "correctly," not resorting to breaking up the paths.

I have used this part of the library on a project that requires this sort of geometric calculation and it seems to be work perfectly.

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