Search code examples
mathprocessing

Parabola in Processing


I was transferring tasks from my maths textbook to Processing. One of the problems required the use of a parabola. I decided to write it as a Bézier curve, wrote the code, but something didn't work. Could anyone please explain to me what I did wrong here?

The code:

def setup():

    background(255,255,255)

    size(800,800)

    rectMode(CENTER)

    line(400,0,400,800)

    line(0,400,800,400)

    #B 0 to know the start and the end points

    a=1

    x=3

    b=0

    c=10

    translate(width/2, height/2)

    noFill()

    beginShape()

    #Parabola formula

    y=-(a*x*x+b*x+c)

    #X of the point of the symetry

    Sx=(-b)/(2*a)

    #Y of the point of the symetry

    Sy=-(a*Sx*Sx+b*Sx+c)

    #Derivative

    Ny=-(800+b)

    #Y of the starting point

    By=-(a*400*400-b*400+c)

    #Y of the end point

    Ey=-(a*400*400+b*400+c)

    #Y3 Y4

    Ty=Ny*(x+400)-By

    bezier(-400,By,Sx,Ty,400,Ey,Sx,Ty)

    endShape()

    noFill()

    beginShape()

    for i in range (-400,400):

        x=i

        y=-i

        vertex(x,y)

    endShape()

(It had to be an 800x800 coordinate plane, a parabola and a straight line.)


Solution

  • Your resulting y coordinates are way too high for the bezier curve.

    If you want to draw the curve with line segments, you need to calculate the y coordinates inside the loop:

    def setup():
        size(400, 400)
        
        background(255,255,255)
        rectMode(CENTER)
        line(width/2, 0, width/2, height)
        line(0, height/2, width, height/2)
        translate(width/2, height/2)
        
        a, b, c = 1, 10, -100
        scaleX = 0.1;
        noFill()
        strokeWeight(4)
        beginShape(LINES)
        for i in range (-400, 400):
            x = i * scaleX
            y = -(a*x*x +b*x + c)
            vertex(i, y)
        endShape()