3

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.

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
lach993
  • 57
  • 1
  • 6
  • 1
    which ones are your main points? Typically if you have three points, the first is a real point, the second is a control point, and the third is a real point again, so the code you're showing, which suggests you're treating points B and C as the real points, is very curious. That said, if there is no bezier function built in, you might just have to implement the actual code for tracing a bezier path. Googling for "python turtle draw bezier curve" gives me https://gist.github.com/SuperDoxin/d7bb473dcec7e1c55f48, does that about cover it? – Mike 'Pomax' Kamermans Jun 26 '19 at 21:31

1 Answers1

3

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

cdlane
  • 40,441
  • 5
  • 32
  • 81