|
这里吧贝兹曲线的ruby代码发下
def BZ__BezierClassic.points(pts, numpts)
curvepts = []
dt = 1.0 / numpts
# evaluate the points on the curve
for i in 0..numpts
t = i * dt
curvepts[i] = BZ__BezierClassic.evaluate(pts, t)
end
curvepts
end
def BZ__BezierClassic.evaluate(pts, t)
degree = pts.length - 1
if degree < 1
return nil
end
t1 = 1.0 - t
fact = 1.0
n_choose_i = 1
x = pts[0].x * t1
y = pts[0].y * t1
z = pts[0].z * t1
for i in 1...degree
fact = fact*t
n_choose_i = n_choose_i*(degree-i+1)/i
fn = fact * n_choose_i
x = (x + fn*pts[i].x) * t1
y = (y + fn*pts[i].y) * t1
z = (z + fn*pts[i].z) * t1
end
x = x + fact*t*pts[degree].x
y = y + fact*t*pts[degree].y
z = z + fact*t*pts[degree].z
Geom::Point3d.new(x, y, z)
这个算法还比较快~ |
评分
-
查看全部评分
|