所以我在网上找到了这个代码,它做了一个随机的贝塞尔曲线,使用随机点。我试图使它成为非随机的,这样它就可以使用静态点,我得到它只使用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
如果有任何帮助都很好。在
从您的描述来看,唯一的问题似乎是关于Python基础知识的。我将代码重新排列如下,因此只需要在底部进行操作。现在,如果您想手动指定4个控制点,请继续执行此操作(在下面的代码中,我自己指定了4个作为示例)。您需要理解的是,在原始代码中,}只是一个列表,每个列表包含4个点(分别是x和y坐标)。如果您是手动指定它们,那么使用循环来编写它们是没有意义的。我希望这个代码足够清楚:
coorArrX
和{好吧,开始这一切的详细的长b都在长线之下。答案就在这里。在
静态点是x,y坐标,x值和y值放在不同的数组中(分别是coorarx和coorary),确保永远不要使用值imgx或imy。在
== 我也是一个新来的人,我拒绝像你一样去看这个…一个学习的经验。在
但是当我看到这个代码时,我看到了一些奇怪的东西
^{pr2}$你确定这部分是正确的吗?imgx在其他地方定义为500,n为4。 所以这可以理解为
这(因为这些值在本规范中从未改变)意味着:
每次传球。 所以每次他们到达:
它们只是不断地向数组中添加相同数据的新副本,所以当它完成时,数组看起来是这样的(内部)
更让人困惑的是coorarx和coorary是A)相同的,和B)在它们的基本部分相同(即每个元素都是相同的)。因此,当您进入代码的这一部分时:
在数组的值中进行替换,得到:
现在这是控制绘制曲线图的部分,但是我不明白在那些不可能的坐标集中,elispe怎么能画出任何东西?!在
故障并进行了复制粘贴测试。
这段代码纯粹是假的,要么是为了欺骗人们浪费时间,要么是因为同样的原因放在OP发现它的地方。但尝试是很有趣的!!在
相关问题 更多 >
编程相关推荐