簡體   English   中英

Bezier曲線使用靜態點

[英]Bezier Curve using static points

所以我發現這個代碼在線,它使用隨機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

任何幫助,如果有的話會很棒。

好的,開始這一切的長期詳細的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

現在這是控制繪圖曲線段的部分,但是我沒有看到elispe在那些不可能的坐標集上的中心如何可以繪制任何東西?!

打破了並進行了復制粘貼測試運行。 這段代碼純粹是虛假的,要么是為了欺騙人們浪費時間,要么是因為同樣的原因而放置在OP的地方。

但這很有趣!

從您的描述中,唯一的問題似乎是Python基礎知識。 我已按如下方式重新排列代碼,因此唯一需要觸及的內容是在底部。 現在,如果你想手動指定4個控制點,請繼續執行(在下面的代碼中,我自己指定了4個控制點作為示例)。 您需要了解,在原始代碼中, coorArrXcoorArrY只是列表,每個列表將分別保持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")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM