gpt4 book ai didi

javascript - 给定 x 立方贝塞尔曲线的 y 坐标

转载 作者:数据小太阳 更新时间:2023-10-29 03:48:53 25 4
gpt4 key购买 nike

这个问题非常类似于:Quadratic bezier curve: Y coordinate for a given X? .但是这个是立方体的...

我正在使用 getBezier函数计算贝塞尔曲线的 Y 坐标。贝塞尔曲线总是从 (0,0) 开始,总是在 (1,1) 结束。

我知道 X 值,所以我尝试将其作为百分比插入(我是个白痴)。但这显然行不通。你能提供一个解决方案吗?这是必要的,它是一个白痴证明功能。喜欢:

function yFromX (c2x,c2y,c3x,c3y) { //c1 = (0,0) and c4 = (1,1), domainc2 and domainc3 = [0,1]
//your magic
return y;
}

最佳答案

由于问题非常有限(函数 x(t) 是单调的),我们可能可以使用一种非常便宜的解决方法——二分查找来解决问题。

var bezier = function(x0, y0, x1, y1, x2, y2, x3, y3, t) {
/* whatever you're using to calculate points on the curve */
return undefined; //I'll assume this returns array [x, y].
};

//we actually need a target x value to go with the middle control
//points, don't we? ;)
var yFromX = function(xTarget, x1, y1, x2, y2) {
var xTolerance = 0.0001; //adjust as you please
var myBezier = function(t) {
return bezier(0, 0, x1, y1, x2, y2, 1, 1, t);
};

//we could do something less stupid, but since the x is monotonic
//increasing given the problem constraints, we'll do a binary search.

//establish bounds
var lower = 0;
var upper = 1;
var percent = (upper + lower) / 2;

//get initial x
var x = myBezier(percent)[0];

//loop until completion
while(Math.abs(xTarget - x) > xTolerance) {
if(xTarget > x)
lower = percent;
else
upper = percent;

percent = (upper + lower) / 2;
x = myBezier(percent)[0];
}
//we're within tolerance of the desired x value.
//return the y value.
return myBezier(percent)[1];
};

这肯定会破坏您的约束之外的某些输入。

关于javascript - 给定 x 立方贝塞尔曲线的 y 坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7348009/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com