Computer Science Canada [source] Bezier Curve

Author:  Catalyst [ Tue Mar 02, 2004 9:17 pm ]
Post subject:  [source] Bezier Curve

Implementation of a 4-point Bezier Curve
Translated (and simplified) from my c++ implementation

 code: View.Set ("offscreenonly,nobuttonbar,graphics:512;512") type Point2D :     record         x, y : real     end record var controlPoints : array 1 .. 4 of Point2D var hold1, hold2, hold3 : real controlPoints (1).x := 100 controlPoints (1).y := maxy div 2 controlPoints (2).x := 200 controlPoints (2).y := maxy div 2 + 150 controlPoints (3).x := 300 controlPoints (3).y := maxy div 2 - 150 controlPoints (4).x := 400 controlPoints (4).y := maxy div 2 var holdPoint : Point2D fcn PointOnCurve (t : real) : Point2D%% Percentage along Curve : 0 to 1     hold1 := 1 - t     hold2 := hold1 * hold1 * hold1     hold3 := t * t * t     holdPoint.x := controlPoints (1).x * hold2     holdPoint.y := controlPoints (1).y * hold2     holdPoint.x += controlPoints (2).x * (3 * t * hold1 * hold1)     holdPoint.y += controlPoints (2).y * (3 * t * hold1 * hold1)     holdPoint.x += controlPoints (3).x * (3 * t * t * hold1)     holdPoint.y += controlPoints (3).y * (3 * t * t * hold1)     holdPoint.x += controlPoints (4).x * hold3     holdPoint.y += controlPoints (4).y * hold3     result holdPoint end PointOnCurve var hold, holdNext : Point2D := PointOnCurve (0) proc DrawCurve     holdNext := PointOnCurve (0)     for i : 1 .. 3         drawline (round (controlPoints (i).x), round (controlPoints (i).y), round (controlPoints (i + 1).x), round (controlPoints (i + 1).y), 22)     end for     for i : 1 .. 100         hold := holdNext         holdNext := PointOnCurve ((i + 1) / 100)         drawline (round (hold.x), round (hold.y), round (holdNext.x), round (holdNext.y), 42)     end for     for i : 1 .. 4         drawoval (round (controlPoints (i).x), round (controlPoints (i).y), 5, 5, 103)     end for end DrawCurve var x, y, z : int loop     mousewhere (x, y, z)     DrawCurve     for i : 1 .. 4         if sqrt ((controlPoints (i).x - x) * (controlPoints (i).x - x) + (controlPoints (i).y - y) * (controlPoints (i).y - y)) <= 5 and z = 1 then             loop                 mousewhere (x, y, z)                 DrawCurve                 controlPoints (i).x := x                 controlPoints (i).y := y                 exit when z = 0                 View.Update                 drawfillbox (0, 0, maxx, maxy, 7)             end loop         end if     end for     View.Update     drawfillbox (0, 0, maxx, maxy, 7) end loop

 Author: zylum [ Tue Mar 02, 2004 9:23 pm ] Post subject: wow that is really nice... im going to try and recreate that without looking at the code +20 bits -zylum

 Author: Tony [ Tue Mar 02, 2004 10:38 pm ] Post subject: sweet anyone wants to write up a vector design app (adobe illustrator) in turing? Author: AsianSensation [ Tue Mar 02, 2004 11:12 pm ] Post subject: that is really really nice

 Author: jonos [ Wed Mar 03, 2004 8:15 am ] Post subject: yeah, awesome.

 Author: recneps [ Wed Mar 03, 2004 3:53 pm ] Post subject: Nice indeed. That has many many possibilities. Author: shorthair [ Wed Mar 03, 2004 5:09 pm ] Post subject: Once again , 2 thumbs up , just a quality app catalyst , i really like it , your code is just crazy you have areally really unique style

 Author: the_short1 [ Fri Mar 12, 2004 2:16 pm ] Post subject: crzy,,,, taht lookz realy nice... and black backround.. YAY someone makes it look good... does anyone know the proper slope to get a circle... like for drawing a line at a central point and rotating it do a nice circle.. whenever i try i only get it to / and \ not round x += ? y +=? y := ?x + ? blah blah blah... i should know this cuz we did sometihgn similiar to it in math.. but i forget the slope now but i dont got my bookz with me... thx..

 Author: jonos [ Fri Mar 12, 2004 3:22 pm ] Post subject: you have to square the y or x coordinates to get a parabola (i think, we haven't done that in math yet).

 Author: the_short1 [ Fri Mar 12, 2004 3:31 pm ] Post subject: i have the exact formula but its at school . . im on march break as as yesterday...      Author: Tony [ Fri Mar 12, 2004 4:03 pm ] Post subject: isn't it something like x*x + y*y = r*r ?

 Author: Delos [ Fri Mar 12, 2004 6:21 pm ] Post subject: That only applies to a circle from the origin. I believe the general eqn for a cirle on a Cartesian place is: (y-p)^2 + (x-q)^2 = r^2 Or something to that effect...urgh..Gr11 maths haunting again.

 Author: the_short1 [ Fri Mar 12, 2004 10:36 pm ] Post subject: thanxTony... thats it ..or at least thats was the formula i remember....

 Author: SuperGenius [ Fri Mar 19, 2004 2:38 pm ] Post subject: the formula for the radius of a circle that my tracher told us is: r=sqrt ((x2-x1)** + (y2-y1)**)

 Author: jonos [ Fri Mar 19, 2004 3:07 pm ] Post subject: i think they are the same, because one of your x,y is the center of the circle and the other is a point on the circle, so the radius is the the length of that (i think)

 Author: the_short1 [ Fri Mar 19, 2004 5:21 pm ] Post subject: homer made me the program i wanted ... it uses cos and sin.... i dont fully understand how to integrate math into compsci... thats was gr.11 for... either way... its all good

 Author: SuperGenius [ Fri Mar 19, 2004 8:15 pm ] Post subject: jonos wrote:i think they are the same, because one of your x,y is the center of the circle and the other is a point on the circle, so the radius is the the length of that (i think) You are correct.

 :