简体   繁体   English

Bezier曲线使用静态点

[英]Bezier Curve using static points

So I found this code on line and it does a random Bezier Curve which uses random points. 所以我发现这个代码在线,它使用随机Bezier曲线,它使用随机点。 I was trying to make it non random so that it would use static points I got it to use only 4 points which was easy. 我试图让它非随机,以便它使用静态点我得到它只使用4点这很容易。 I have never used PIL before in python and in fact I am slowly learning python. 我之前从未在python中使用过PIL,事实上我正在慢慢学习python。 And I have only really done front end work (html, javascript, css, etc) and I just wanted to know if some one can help me. 我只做了前端工作(html,javascript,css等),我只是想知道是否有人可以帮助我。 Here is the code I found on line: 这是我在线找到的代码:

# 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

Any help if at all would be great. 任何帮助,如果有的话会很棒。

Ok The long detailed BS that began this all is below the long line. 好的,开始这一切的长期详细的BS都在长线以下。 The resulting answer is here. 得到的答案就在这里。

Your static points are x,y coordinates with the x values and y values placed in seperate arrays (coorArrx and coorArrY respectively) make sure to never use a value = imgx or imy. 你的静态点是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()

=.........................................................................................= I am also something of a newcommer to all of this, and I REFUSE to look this up as I see it like you do...a learning experiencee. = ................................................. ........................................ =我也是所有人的新手这一点,我拒绝看到这一点,因为我看到它就像你做...一个学习经验。

But as I look at this code I see something strange 但是当我看到这段代码时,我发现了一些奇怪的东西

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

Are you sure this part is correct? 你确定这部分是正确的吗? imgx is defined as 500 elsewhere, and n is 4. so this could read as imgx在其他地方定义为500,n为4.所以这可以解读为

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

which (since these values never change at all in this code) means: 哪个(因为这些值在此代码中根本不会改变)意味着:

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

on every pass. 在每一次通过。 So each time they get to : 所以每次他们到达:

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

They simply keep adding new copies of the same data to the array, so when it is done the array looks like this (internally) 他们只是不断向数组添加相同数据的新副本,所以当完成后,数组看起来像这样(内部)

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

What makes this more confusing, is that coorArrX and coorArrY are A) Identical, and B) identical in their basic parts(that is each element is identical). 更令人困惑的是,coorArrX和coorArrY是A)相同的,和B)基本部分相同(即每个元素是相同的)。 Therefore, when you get to this part of the code: 因此,当你到达这部分代码时:

# 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

and you substitute in the values in the arrays, you get: 并且你在数组中的值中替换,得到:

# 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

Now this is the part that controls the drawing of the curved segments for the plot, but I do not see how centering an elispe on those impossible coordinate sets can draw anything?! 现在这是控制绘图曲线段的部分,但是我没有看到elispe在那些不可能的坐标集上的中心如何可以绘制任何东西?!

Broke down and did a copy paste test run. 打破了并进行了复制粘贴测试运行。 This code is purely bogus, either placed to dupe people into wasting time, or placed where OP found it for same reason. 这段代码纯粹是虚假的,要么是为了欺骗人们浪费时间,要么是因为同样的原因而放置在OP的地方。

But it was fun trying!! 但这很有趣!

From your description, the only problem seems to be about Python basics. 从您的描述中,唯一的问题似乎是Python基础知识。 I have rearranged the code as follows, so the only things that need to be touched are at bottom. 我已按如下方式重新排列代码,因此唯一需要触及的内容是在底部。 Now, if you want to manually specify 4 control points, go ahead and do it (in the following code I have specified 4 of them myself as an example). 现在,如果你想手动指定4个控制点,请继续执行(在下面的代码中,我自己指定了4个控制点作为示例)。 You need to understand that, in the original code, coorArrX and coorArrY are just lists, which will hold 4 points each (x and y coordinates, respectively). 您需要了解,在原始代码中, coorArrXcoorArrY只是列表,每个列表将分别保持4个点(分别为x和y坐标)。 If you are manually specifying them, there is no point in using a loop to write them. 如果您手动指定它们,则使用循环来编写它们是没有意义的。 I hope this code is clear enough: 我希望这段代码足够清晰:

# 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")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM