使用静态点的Bezier曲线

2024-10-01 09:17:32 发布

您现在位置:Python中文网/ 问答频道 /正文

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

如果有任何帮助都很好。在


Tags: 代码inimageforpilrangedecr
2条回答

从您的描述来看,唯一的问题似乎是关于Python基础知识的。我将代码重新排列如下,因此只需要在底部进行操作。现在,如果您想手动指定4个控制点,请继续执行此操作(在下面的代码中,我自己指定了4个作为示例)。您需要理解的是,在原始代码中,coorArrX和{}只是一个列表,每个列表包含4个点(分别是x和y坐标)。如果您是手动指定它们,那么使用循环来编写它们是没有意义的。我希望这个代码足够清楚:

# 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
from PIL import Image, ImageDraw

def plot_curve(image, px, py, steps=1000, color=(0, 255, 0)):
    def B(coord, i, j, t):
        if j == 0:
            return coord[i]
        return (B(coord, i, j - 1, t) * (1 - t) +
                B(coord, i + 1, j - 1, t) * t)

    img = image.load()
    for k in range(steps):
        t = float(k) / (steps - 1)
        x = int(B(px, 0, n - 1, t))
        y = int(B(py, 0, n - 1, t))
        try:
            img[x, y] = color
        except IndexError:
            pass

def plot_control_points(image, px, py, radi=3, color=(255, 0, 0)):
    draw = ImageDraw.Draw(image)
    for x, y in zip(px, py):
        draw.ellipse((x - radi, y - radi, x + radi, y + radi), color)


# Your fixed, manually specified, points.
n = 4
coord_x = [25, 220, 430, 410]
coord_y = [250, 10, 450, 40]

image = Image.new("RGB", (500, 500))

plot_curve(image, coord_x, coord_y)
plot_control_points(image, coord_x, coord_y)

image.save("BezierCurve.png")

好吧,开始这一切的详细的长b都在长线之下。答案就在这里。在

静态点是x,y坐标,x值和y值放在不同的数组中(分别是coorarx和coorary),确保永远不要使用值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()

== 我也是一个新来的人,我拒绝像你一样去看这个…一个学习的经验。在

但是当我看到这个代码时,我看到了一些奇怪的东西

^{pr2}$

你确定这部分是正确的吗?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)]

更让人困惑的是coorarx和coorary是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

现在这是控制绘制曲线图的部分,但是我不明白在那些不可能的坐标集中,elispe怎么能画出任何东西?!在

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

但尝试是很有趣的!!在

相关问题 更多 >