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

Username:   Password: 
 RegisterRegister   
 Orbiting dots
Index -> Programming, Turing -> Turing Submissions
View previous topic Printable versionDownload TopicRate TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Lapsus Antepedis




PostPosted: Sun Jul 03, 2005 3:03 pm   Post subject: Orbiting dots

This is a small program that I created in about 10 minutes, It was inspired by one of the screensavers included with some common linux distros...
I am aware of it's problems with flickering, but I do not really want to rewrite the code again...

Turing:

const numofballs : int := 15

var window : int :=
    Window.Open ("graphics:500;500, nocursor, title:Orbit")

process dot (startx, starty, orbx, orby, clr, del, siz : int, power : real)
    var x, y : int
    x := startx
    y := starty
    var xspeed, yspeed : real := 0

    loop
        drawoval (maxx div 2, maxy div 2, 2, 2, 7)
        drawfilloval (x, y, siz, siz, 0)
        x += round (xspeed)
        y += round (yspeed)
        drawfilloval (x, y, siz, siz, clr)

        if x > orbx then
            xspeed -= power
        elsif x < orbx then
            xspeed += power
        end if

        if y > orby then
            yspeed -= power
        elsif y < orby then
            yspeed += power
        end if

        delay (del)
    end loop
end dot

for i : 1 .. numofballs
    fork dot (Rand.Int (0, maxx), Rand.Int (0, maxy), maxx div 2, maxy div
        2, Rand.Int (1, 255), 50, 5, 0.1)
end for


Any thoughts?
Sponsor
Sponsor
Sponsor
sponsor
Delos




PostPosted: Sun Jul 03, 2005 3:13 pm   Post subject: (No subject)

Processes! Noooooo!!!

Ok, they're not that bad, but almost. I would suggest you take all those parameters you've got, and turn them into a record-type instead. Now, you'll be able to create a pretty array of these balls, which you will be able to manipulate in a procedure, as opposed to a process.

There've been a number of similar submissions over the last while. One that (naturally) stands out was Catalyst's a while back. His was more a gravity oriented type thing anyway, but it did show the vectors graphically as well. See if you can find it, it'd could be useful.
I tried it out without the clearing of the balls, and the resulting shape oddly reminded me of certain protein structures one finds...
[Gandalf]




PostPosted: Sun Jul 03, 2005 4:51 pm   Post subject: (No subject)

Well, at least you used them for their purpose Confused...

Yeah, without the clearing it sometimes looks pretty good, but sometimes it just looks like a pile of crap - literally. I don't know, it turns out differently.

To reduce the flickering you either reduce the delay you have greatly, or you just add "offscreenonly" and "View.Update" after following Delos' advice.
Laetus Constituo




PostPosted: Tue Jul 12, 2005 12:16 pm   Post subject: NEW Orbiting Dots

Hi! It's me, Laetus, Lapsus' friend! *waves* We've been working on the dots program, and pretty well redone everything, as well as added COLISION! Now the dots can slam into each other, and grow! yay! And Delos, no processes!
Laetus Constituo




PostPosted: Tue Jul 12, 2005 12:21 pm   Post subject: (No subject)

Hey...what the....that's weird, so let's try again! Sorry bout the length, tried to upload it, but something didn't work.. Oh well
code:
code
var window : int := Window.Open ("fullscreen, graphics, title:PARTICLEs")

colourback (black)
cls
colour (0)

var words : string := "How many particles do you want? "
for i : 1 .. length (words)
    put words (i) ..
    delay (5)
end for
var ans : int := 2
get ans

cls

setscreen ("noecho, nocursor")

const dotamount : int := ans
var counter : int := 0

type particle :
    record
        x : int
        y : int
        xs : real
        ys : real
        siz : int
        force : real
        colour : int
        alive : boolean
    end record

var exita : boolean := false

var dot : array 1 .. dotamount of particle
for i : 1 .. dotamount
    dot (i).x := Rand.Int (0, maxx)
    dot (i).y := Rand.Int (0, maxy)
    dot (i).xs := 0
    dot (i).ys := 0
    dot (i).siz := 5
    dot (i).force := 0.1
    dot (i).alive := true
    dot (i).colour := Rand.Int (1, 4)
    if dot (i).colour = 1 then
        dot (i).colour := brightred
    elsif dot (i).colour = 2 then
        dot (i).colour := yellow
    elsif dot (i).colour = 3 then
        dot (i).colour := brightgreen
    elsif dot (i).colour = 4 then
        dot (i).colour := brightblue
    end if
end for

process exiter
    loop
        if hasch then
            exita := true
            exit
        end if
        if exita = true then
            exit
        end if
    end loop
end exiter


fork exiter

loop
    for i : 1 .. dotamount
        if dot (i).alive = true then
            drawfilloval (dot (i).x, dot (i).y, dot (i).siz,
                dot (i).siz,
                dot (i).colour)
        end if
    end for
    delay (10)
    for i : 1 .. dotamount
        if dot (i).alive = true then
            drawfilloval (dot (i).x, dot (i).y, dot (i).siz,
                dot (i).siz,
                black)
        end if
    end for

    for i : 1 .. dotamount
        for h : 1 .. dotamount
            if dot (i).alive = true and dot (h).alive = true then
                if dot (i).x > dot (h).x then
                    dot (i).xs -= dot (h).force
                elsif dot (i).x < dot (h).x then
                    dot (i).xs += dot (h).force
                end if

                if dot (i).y > dot (h).y then
                    dot (i).ys -= dot (h).force
                elsif dot (i).y < dot (h).y then
                    dot (i).ys += dot (h).force
                end if
            end if
        end for
    end for

    for i : 1 .. dotamount
        dot (i).x += round (dot (i).xs)
        dot (i).y += round (dot (i).ys)
    end for
    for i : 1 .. dotamount
        for h : 1 .. dotamount
            if dot (i).alive = true and dot (h).alive = true then
                if dot (i).x not= dot (h).x and dot (i).y not=
                        dot (h).y then
                    if sqrt ( (dot (h).x - dot (i).x)
                            ** 2 + (dot (h).y - dot (i).y)
                            ** 2) < dot (h).siz + ( (dot (h).xs +
                            dot (h).ys) / 2) then
                        dot (i).alive := false
                        dot (h).siz += dot (i).siz
                        dot (h).force += dot (i).force
                        dot (i).force := 0
                        dot (h).colour := Rand.Int (1, 4)
                        if dot (h).colour = 1 then
                            dot (h).colour := brightred
                        elsif dot (h).colour = 2 then
                            dot (h).colour := yellow
                        elsif dot (h).colour = 3 then
                            dot (h).colour := brightgreen
                        elsif dot (h).colour = 4 then
                            dot (h).colour := brightblue
                        end if
                    end if
                end if
            end if
        end for
    end for
    for i : 1 .. dotamount
        if dot (i).alive = false then
            counter += 1
        end if
    end for
    if counter = dotamount - 1 then
        for i : 1 .. dotamount
            dot (i).x := Rand.Int (0, maxx)
            dot (i).y := Rand.Int (0, maxy)
            dot (i).xs := 0
            dot (i).ys := 0
            dot (i).siz := 5
            dot (i).force := 0.1
            dot (i).alive := true
            dot (i).colour := Rand.Int (1, 4)
            if dot (i).colour = 1 then
                dot (i).colour := brightred
            elsif dot (i).colour = 2 then
                dot (i).colour := yellow
            elsif dot (i).colour = 3 then
                dot (i).colour := brightgreen
            elsif dot (i).colour = 4 then
                dot (i).colour := brightblue
            end if
        end for
    end if
    counter := 0

    for i : 1 .. dotamount
        if dot (i).x < 0 or dot (i).x > maxx then
            dot (i).xs *= - 1
        end if
        if dot (i).y < 0 or dot (i).y > maxy then
            dot (i).ys *= - 1
        end if
    end for

    exit when exita = true
end loop
Window.Close (window)
Delos




PostPosted: Tue Jul 12, 2005 12:43 pm   Post subject: (No subject)

Couple things:
You should use 'offscreenonly' to clear the dots instead of drawing dots over the old dots:

Turing:

    setscreen ("offscreenonly")
    %...

    for i : 1 .. dotamount
        if dot (i).alive = true then
            drawfilloval (dot (i).x, dot (i).y, dot (i).siz,
                dot (i).siz,
                dot (i).colour)
        end if
    end for
    delay (10)
    View.Update
    cls


Also, you don't need the 'exiter' process. Having an 'exit when hasch' condition at the end of your loop will do exactly the same thing.

I like it so far, now try to change it around a bit - allow for bounces between particles instead of merges. Even better yet, try turn this into a class!

+ bits
Laetus Constituo




PostPosted: Wed Jul 13, 2005 8:04 am   Post subject: (No subject)

You're right, I should've used an 'exit when' statement instead of a process. As for offscreenonly, it doesn't seem to work with the older versions of Turing, which is what i'm using, Turing 3.0.1A. But thanks for the advice!
Delos




PostPosted: Wed Jul 13, 2005 12:52 pm   Post subject: (No subject)

You're still running that! Yes, yes, please do update. If you have contact with your teacher, you should be able to ask them for the update.
Sponsor
Sponsor
Sponsor
sponsor
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  [ 8 Posts ]
Jump to:   


Style:  
Search: