给定距离起点的距离时,在Bézier曲线上找到一个点? [英] Finding a point on a Bézier curve when given the distance from the start point?

查看:208
本文介绍了给定距离起点的距离时,在Bézier曲线上找到一个点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个4点Bézier曲线和一个距离。从起点开始,如何找到与起点相距一定距离的点的x,y坐标?

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?

我看过其他的例子,从我所知道的,他们通过将曲线分成几千个点来近似值,然后找到最近的点。这对我不起作用。对于我正在做的事情,我想准确到只有两位小数。下面是我创建Bézier曲线的简单形式。 (y值是任意的,x值总是相隔352个像素)。如果重要,我正在使用Java。

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

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

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)

推荐答案

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

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)

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

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

使用此百分比作为x和y的新t值,现在您拥有新的x和y位置。

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

重要提示:这是一个奇怪的情况,但如果你的t值将大于1,则使用绝对值。当你将它立方体化时,该值将为负... =坏事情发生了。

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.

将曲线分成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;

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

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

计算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;

这篇关于给定距离起点的距离时,在Bézier曲线上找到一个点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆