Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Bezier Curve
Index -> Programming, Turing -> Turing Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
The_Bean




PostPosted: Mon Apr 05, 2010 10:29 pm   Post subject: Bezier Curve

Despite being in University and knowing Python, Miranda, and C, I still find a use for Turing.
It's great for quickly playing around with math and complex functions.

Bezier Curve
program can handle 1..12 nodes, change the variable called degree
Interactive concept from Catalyst - http://compsci.ca/v3/viewtopic.php?t=3932
Formula from Wikipedia http://en.wikipedia.org/wiki/B%C3%A9zier_curve
Drag the nodes around with the mouse
commonly found in MS Paint to make curved lines

Turing:

View.Set ("graphics:800,800;nobuttonbar;offscreenonly")
type point :
    record
        x, y : real
    end record

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
var degree : int := 6 %%%%%%%%%%%  Modify to change number of nodes (complexity)
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%  2=quadratic 3=cubic 4=quartic (max of 12)

var mx, my, mb : int
var poin : array 0 .. degree of point
var comb : array 0 .. degree of nat
Text.ColourBack (7)

fcn fact (n : nat) : nat
    var res : nat := 1
    for i : 2 .. n
        res *= i
    end for
    result res
end fact

proc Init ()
    for i : 0 .. degree
        poin (i).x := Rand.Int (10, maxx - 10)
        poin (i).y := Rand.Int (10, maxy - 10)
        comb (i) := fact (degree) div (fact (i) * fact (degree - i))
    end for
end Init

proc DrawBez ()
    var t : real
    var draw : point
    var prev : point
    for i : 0 .. degree - 1
        Draw.Line (round (poin (i).x), round (poin (i).y), round (poin (i + 1).x), round (poin (i + 1).y), 55)
    end for
    prev.x := poin (0).x
    prev.y := poin (0).y
    for n : 1 .. 99
        t := n / 100
        draw.x := 0
        draw.y := 0
        for i : 0 .. degree
            draw.x += comb (i) * (1 - t) ** (degree - i) * t ** i * poin (i).x
            draw.y += comb (i) * (1 - t) ** (degree - i) * t ** i * poin (i).y
        end for
        Draw.Line (round (draw.x), round (draw.y), round (prev.x), round (prev.y), 0)
        prev.x := draw.x
        prev.y := draw.y
    end for
    Draw.Line (round (poin (degree).x), round (poin (degree).y), round (prev.x), round (prev.y), 0)
    for i : 0 .. degree
        Draw.Oval (round (poin (i).x), round (poin (i).y), 5, 5, 42)
    end for
end DrawBez

cls
Init ()
DrawBez ()
View.Update ()
loop
    Mouse.Where (mx, my, mb)
    for i : 0 .. degree
        if Math.Distance (mx, my, poin (i).x, poin (i).y) <= 5 and mb = 1 then
            loop
                Mouse.Where (mx, my, mb)
                cls
                poin (i).x := mx
                poin (i).y := my
                DrawBez ()
                View.Update ()
                exit when mb ~= 1
            end loop
        end if
    end for
    exit when hasch
end loop

Sponsor
Sponsor
Sponsor
sponsor
A.J




PostPosted: Mon Apr 05, 2010 10:45 pm   Post subject: RE:Bezier Curve

Pretty good. Try making it a bit more user friendly, by allowing the user to create/delete curves at will anywhere on the screen.

Good Job.
USEC_OFFICER




PostPosted: Tue Apr 06, 2010 11:55 am   Post subject: RE:Bezier Curve

+ karma. It is very, very good. I'm glad to see that you still have a use for Turing.
SNIPERDUDE




PostPosted: Tue Apr 06, 2010 5:54 pm   Post subject: RE:Bezier Curve

+bits
Awesome work. I love the simple clean interface too.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Submissions
View previous topic Tell A FriendPrintable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 4 Posts ]
Jump to:   


Style:  
Search: