簡體   English   中英

問題畫貝塞爾曲線

[英]Trouble drawing Bezier Curve

我試圖繪制三次貝塞爾曲線,但是我在程序的最后部分遇到了困難。 我似乎無法讓tkinter真正繪制曲線。 目前,它只會在tkinter窗口的左上方繪制一條小線,我不確定自己是否以錯誤的方式進行操作。

from tkinter import *

root = Tk()

window = Canvas(root, width=800, height=800)
window.pack()

def bezier_curve():
    #create empty list for points
    p = []

    #loops through 4 times to get 4 control points
    for i in range(4):
        while True:
            #user input
            p_input = input("Enter X,Y Coordinates for p" + str(i) + ":")
            #splits the string into x and y coordinates
            p_components = p_input.split(',')
            #checks to see if user hasnt entered two coordinates
            if len(p_components) != 2:
                print("Missing coordinate please try again.")
                p_input = input("Enter starting point X,Y Coordinates:")
            #checks to see if the values can not be converted into floats.
            try:
                x = float(p_components[0])
                y = float(p_components[1])
            except ValueError:
                print("Invalid coordinates", p_components, "please try again.")
            #appends the x and y coordinates as a 2 dimensional array.
            else:
                p.append([float(p_components[0]), float(p_components[1])])
                break
    print(p[0][0])

    #Start x and y coordinates, when t = 0
    x_start = p[0][0]
    y_start = p[0][1]

    #loops through in intervals of 0.1 
    for t in range(0, 11, 1):
        t = i/10
        x=(p[0][0]*(1-t)**3+p[1][0]*3*t*(1-t)**2+p[2][0]*3*t**2*(1-t)+p[3][0]*t**3)
        y=(p[0][1]*(1-t)**3+p[1][1]*3*t*(1-t)**2+p[2][1]*3*t**2*(1-t)+p[3][1]*t**3)

        draw_line = window.create_line(x,y,x_start,y_start)
        #updates initial values
        x_start = x
        y_start = y


bezier_curve()
root.mainloop()

是; 畫線循環中的一個小錯誤:

#loops through in intervals of 0.1 
for t in range(0, 11, 1):
    t = i/10

您已將t分配為i時的循環變量。

for t in range(0, 11, 1):
    ^
    This shoud be i 

@figbeam答案正確,並可以解決您的問題。
我發現您的輸入機制很乏味,因此我對其進行了更改,以允許在畫布上單擊以捕獲貝塞爾曲線的控制點。

import tkinter as tk


def draw_bezier():
    # Start x and y coordinates, when t = 0
    x_start = control_points[0][0]
    y_start = control_points[0][1]

    p = control_points

    # loops through
    n = 50
    for i in range(50):
        t = i / n
        x = (p[0][0] * (1-t)**3 + p[1][0] * 3 * t * (1-t)**2 + p[2][0] * 3 * t**2 * (1-t) + p[3][0] * t**3)
        y = (p[0][1] * (1-t)**3 + p[1][1] * 3 * t * (1-t)**2 + p[2][1] * 3 * t**2 * (1-t) + p[3][1] * t**3)

        canvas.create_line(x, y, x_start, y_start)
        # updates initial values
        x_start = x
        y_start = y


def get_point(event):
    global control_points
    point = x, y = (event.x, event.y)
    control_points.append(point)
    canvas.create_oval(x, y, x+3, y+3)
    if len(control_points) == 4:
        draw_bezier()
        control_points = []


if __name__ == '__main__':

    control_points = []

    root = tk.Tk()

    canvas = tk.Canvas(root, width=800, height=800)
    canvas.pack()

    canvas.bind('<Button-1>', get_point)

    root.mainloop()

暫無
暫無

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

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