简体   繁体   中英

Trouble drawing Bezier Curve

Im trying to graph a cubic bezier curve however, I am having difficulty with the last part of the program. I cant seem to get tkinter to actually draw the curve. It will currently just draw a small line in the top left of the tkinter window and im not sure if im doing it the wrong way or not.

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()

Yes; a small error in the loop for drawing the lines:

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

You have assigned t as the loop variable when it should be i .

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

@figbeam answer is correct, and fixes your problem.
I found your input mechanism tedious, so I changed it to allow clicks on the canvas to capture the control points of your bezier curve.

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()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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