简体   繁体   English

javascript - 在三次贝塞尔曲线上找到给定点的系数/函数

[英]javascript - find coefficient/function for given points on cubic bezier curve


I'm creating a simple d3 graph based on this infographic : http://bit.ly/1o4iGfK 我正在基于这个信息图创建一个简单的d3图: http//bit.ly/1o4iGfK

What I'm trying to reproduce right now is the cubic bezier lines between each of my "nodes". 我现在想要重现的是我的每个“节点”之间的立方贝塞尔线。
I've drawn a curve thanks to this website . 由于这个网站,我画了一条曲线。 So I know the x and y coordinates of each four points : 所以我知道每四个点的x和y坐标:
(x0, y0) = (32, 45); (x0,y0)=(32,45);
(x1, y1) = (32, 150); (x1,y1)=(32,150);
(x2, y2) = (190, 150); (x2,y2)=(190,150);
(x3, y3) = (190,260); (x3,y3)=(190,260);

But I don't really know how to use the common algorithms, and transpose it as a javascript function so that I could only write known (x0,y0) and (x3, y3) coordinates, and pouf! 但我真的不知道如何使用常用算法,并将其转换为javascript函数,以便我只能编写已知的(x0,y0)和(x3,y3)坐标,以及pouf! get a pretty line. 得到一个漂亮的线。
Mostly because I'm not really good with algebra. 主要是因为我对代数不是很好。 Sorry. 抱歉。

I've found this thread : here 我找到了这个帖子: 这里
It seems to be part of the answer but, well, I don't fully understand the code and the explications. 它似乎是答案的一部分,但是,我并不完全理解代码和说明。

For now I've used this function : 现在我已经使用了这个功能:

function linkArc(d) {
            var dx = d.source.x - d.target.x,
                dy = d.source.y - d.target.y,
                dr = Math.sqrt(dx * dx + dy * dy);

            return "M" + d.source.x + "," + d.source.y + "A" + dr + "," + dr + "    0    0,1 " + d.target.x + "," + d.target.y;
        }

Which is a simple arc, not really what I'm looking for. 这是一个简单的弧,而不是我正在寻找的。
(d.source.x , d.target.y) : coordinates of the first point (x0,y0) (d.source.x,d.target.y):第一个点的坐标(x0,y0)
(d.target.x , d.target.y) : coordinates of the end point (x3, y3) (d.target.x,d.target.y):终点坐标(x3,y3)

Thanks. 谢谢。

What you want is a vertical slope at the end points. 你想要的是在终点处的垂直斜率。 Since the inner control points determine the slope, you have to set them directly above and below the control points. 由于内部控制点确定斜率,因此必须将它们直接设置在控制点的上方和下方。 There are two degrees of freedom in how much above and below. 上下有多少自由度。 For instance, put them at the thirds of the height difference 例如,将它们放在高度差的三分之一处

x1 = x0;  y1 = (2*y0+y3)/3;
x2 = x3;  y2 = (2*y3+y0)/3;

The general scheme is 一般方案是

y1 = (a*y0+b*y3)/(a+b);
y2 = (a*y3+b*y0)/(a+b);

where 哪里

y1-y0 = b/(a+b)*(y3-y0)

so that b/(a+b) is the height fraction that the first control point is above the zeroth endpoint. 因此b /(a + b)是第一个控制点高于第零个端点的高度分数。


So to get a fraction of 2/3 instead of the 1/3 above, use a=1 and b=2 to get 因此,要获得2/3的一小部分而不是上面的1/3,请使用a = 1和b = 2来获得

y1=(y0+2*y3)/3;
y2=(y3+2*y0)/3;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM