我在网上找到了这段代码,它可以生成随机的Bezier曲线并使用随机点。我想把它变成非随机的,让它使用静态点。我成功地让它只使用了4个点,这很容易。我以前从未在Python中使用过PIL,事实上我正在慢慢学习Python。而且我只做过前端工作(HTML、JavaScript、CSS等),我想知道是否有人能帮助我。以下是我在网上找到的代码:
# Random Bezier Curve using De Casteljau's algorithm
# http://en.wikipedia.org/wiki/Bezier_curve
# http://en.wikipedia.org/wiki/De_Casteljau%27s_algorithm
# FB - 201111244
import random
from PIL import Image, ImageDraw
imgx = 500
imgy = 500
image = Image.new("RGB", (imgx, imgy))
draw = ImageDraw.Draw(image)
def B(coorArr, i, j, t):
if j == 0:
return coorArr[i]
return B(coorArr, i, j - 1, t) * (1 - t) + B(coorArr, i + 1, j - 1, t) * t
n = 4 # number of control points
coorArrX = []
coorArrY = []
for k in range(n):
x = (0, imgx - 1)
y = (0, imgy - 1)
coorArrX.append(x)
coorArrY.append(y)
# plot the curve
numSteps = 10000
for k in range(numSteps):
t = float(k) / (numSteps - 1)
x = int(B(coorArrX, 0, n - 1, t))
y = int(B(coorArrY, 0, n - 1, t))
try:
image.putpixel((x, y), (0, 255, 0))
except:
pass
# plot the control points
cr = 3 # circle radius
for k in range(n):
x = coorArrX[k]
y = coorArrY[k]
try:
draw.ellipse((x - cr, y - cr, x + cr, y + cr), (255, 0, 0))
except:
pass
# image.save("BezierCurve.png", "PNG")
image.show() I add this so I can see it right away
任何形式的帮助都是非常好的。
n = 4
。现在你想要手动指定4个点,而不是使用random
函数来获取它们?你只需要为coorArrX
给出四个 x 坐标,为coorArrY
给出四个 y 坐标,并删除当前代码中填充这些列表的虚假循环。例如,定义coorArrX = [25, 220, 430, 430]
和coorArrY = [250, 10, 450, 40]
,并删除for k in range(n): ...
循环。这解决了你的问题吗? - mmgp