How to get the points of a bezier curve/parabola using 3 unique points on a graph

Viewed 972

I'm trying to make a parabolic/bezier curve between two main points (using a third point as the control point) and cannot work out how to do it.

from turtle import *

pointA = (0.00,50.00)
pointB = (0.00,350.00)
pointC = (-300.00,50.00)

pu()
goto(pointB)
pd()
dot()
goto(pointC)
dot()

ht()

this creates a line between the 2 points that are my main points, i also want to make a curve using pointA so i can have multiple lines, i have already ruled out the possibility of parabolic curves because that equation doesn't fit on a parabola unless i rotate the plane but thats a whole other kettle of fish i'm not ready for.

i would love some help as i am decently stuck, thank you

Edit: i have tried a fair few things none of them got close, i ended up resorting to just running with a midpoint translated a few pixels. E.G.

for j in range(3):
        pu()
        goto(pointB)
        pd()
        dot()
        midpoint = ((pointB[0]+pointC[0])/2, (pointB[1]+pointC[1])/2)
        goto(midpoint[0]+(20*j), midpoint[1])
        goto(pointC)
        dot()

this is a more realistic use of what im using it for, except i want to change that solid line into a variable line as dependednt on the position of the two points it will be on the same line, thus making it look like 1 singular line.

1 Answers

Based on Wikipedia's explanation of quadratic Bézier curves we should be able to simply do:

from turtle import Screen, Turtle, Vec2D

p0 = Vec2D(0, 50)
p1 = Vec2D(-300, 50)
p2 = Vec2D(0, 350)

b = lambda t: p1 + (1 - t)**2 * (p0 - p1) + t**2 * (p2 - p1)

turtle = Turtle()
turtle.penup()

for position in [p2, p1, p0]:
    turtle.goto(position)
    turtle.dot()

turtle.pendown()

t = 0

while t <= 1:
    position = b(t)

    turtle.setheading(turtle.towards(position))
    turtle.goto(position)

    t += 0.1

screen = Screen()
screen.exitonclick()

enter image description here

Related