python - 使用静态点的贝塞尔曲线

标签 python python-imaging-library bezier curve

所以我在网上找到了这段代码,它使用随机点绘制随机贝塞尔曲线。我试图让它成为非随机的,这样它就可以使用静态点,我让它只使用 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

如果有任何帮助,那就太好了。

最佳答案

好的,开始这一切的冗长详细的 BS 在长线下方。结果答案在这里。

您的静态点是 x、y 坐标,x 值和 y 值分别放置在单独的数组中(分别为 coorArrx 和 coorArrY),确保永远不要使用 value = imgx 或 imy。

# 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 = random.randint(3, 6) # number of control points
n=4
#coorArrX = []
#coorArrY = []
#for k in range(n):
#    x = random.randint(0, imgx - 1)
#    y = random.randint(0, imgy - 1)
#    coorArrX.append(x)
#    coorArrY.append(y)
coorArrX=[3,129,12,77]
coorArrY=[128,52,12,491]

# 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.show()

=................................................ .....................................= 我也是所有这一切的新手,我拒绝像您一样查看它……学习经验。

但是当我看这段代码时,我发现了一些奇怪的东西

for k in range(n):
    x = (0, imgx - 1)
    y = (0, imgy - 1)
    coorArrX.append(x)
    coorArrY.append(y)

你确定这部分是正确的吗? imgx在别处定义为500,n为4。 所以这可以读作

for k in range(4):
    x = (0, 500 - 1)
    y = (0, 500 - 1)

这(因为这些值在此代码中根本不会改变)意味着:

x = (0, 499)
y = (0, 499)

每次传球。 所以每次他们到达:

coorArrX.append(x)
coorArrY.append(y)

他们只是简单地将相同数据的新副本添加到数组中,所以完成后数组看起来像这样(内部)

[(0, 499), (0, 499), (0, 499), (0,499)]

更令人困惑的是,coorArrX 和 coorArrY A) 相同,B) 基本部分相同(即每个元素相同)。因此,当您到达这部分代码时:

# 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

然后你代入数组中的值,你会得到:

# plot the control points
cr = 3 # circle radius
for k in range(n):
    x = coorArrX[k]
    y = coorArrY[k]
    try:
        draw.ellipse(((0, 499) - 3, (0, 499) - 3, (0, 499) + 3, (0, 499) + 3), (255, 0, 0))
    except:
        pass

现在这是控制绘图曲线段绘制的部分,但我看不出如何将省略号置于那些不可能的坐标集上以绘制任何东西?!

故障并进行了复制粘贴测试运行。 这段代码纯粹是伪造的,要么是用来欺骗人们浪费时间,要么是出于同样的原因放在 OP 发现它的地方。

但是尝试很有趣!!

关于python - 使用静态点的贝塞尔曲线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13713831/

相关文章:

python - 在python中解释来自照片的exif数据的GPS信息

ios - 如何在 UIKit 中实现 Photoshop 曲线编辑器

math - 获取贝塞尔曲线的边界

python - 如何分析 python 进程的内存使用情况?

python - 如何向我的 td 添加增量 ID?

python - 在Python 2.7中将.po文件转换为json

python - PIL 模块错误

python - 如何从 index.html 中的模板返回 appengine 数据存储对象的键?

python - 如何检查不同的文件格式扩展名并接受有效的扩展名?

javascript - HTML5 Canvas | .moveTo() 和 .fillI() 的细微差别