对于任何发现我的问题的人,我解决了自己的问题。要找到曲线的总距离,请将其分成1000个小段(仍然相当准确),找到每个点之间的距离,然后将它们加在一起。(您应该使用参数方程)
现在找到沿着曲线的百分比。= 距离/曲线的总长度
使用此百分比作为您的新t值用于x和y,现在您有了新的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;