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

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




PostPosted: Thu Dec 04, 2008 8:56 pm   Post subject: Sierpinski Triangle

Me and a friend were working on this in class today while practicing recursion, and I put the finishing touches on it.
It's not perfect at higher stages, but it does the job fairly well. Enjoy Smile

code:
proc Draw_Triangle (X1, Y1, X2, Y2, X3, Y3, Colour : int)
    var XArray, YArray : array 1 .. 3 of int
    XArray (1) := X1
    YArray (1) := Y1
    XArray (2) := X2
    YArray (2) := Y2
    XArray (3) := X3
    YArray (3) := Y3
    drawfillpolygon (XArray, YArray, 3, Colour)
end Draw_Triangle

proc Sierpinski_Triangle (X1, Y1, X2, Y2, X3, Y3, Colour, Iterations : int)
    if Iterations > 0 then
        Draw_Triangle((X1+X2)div 2, (Y1+Y2)div 2, (X2+X3)div 2, (Y2+Y3)div 2, (X1+X3)div 2, (Y1+Y3)div 2, Colour)
        Sierpinski_Triangle (X1, Y1, (X1+X2)div 2, (Y1+Y2)div 2, (X1+X3)div 2, Y3, Colour, Iterations-1)
        Sierpinski_Triangle ((X1+X3)div 2, Y1, (X2+X3)div 2, (Y2+Y3)div 2, X3, Y3, Colour, Iterations-1)
        Sierpinski_Triangle ((X1+X2)div 2, (Y1+Y2)div 2, X2, Y2, (X2+X3)div 2, (Y2+Y3)div 2, Colour, Iterations-1)
    end if
end Sierpinski_Triangle

var Arrow : string(1)
var Iter, Max : int := 0

put "Seirpinski's Triangle\n"
put "Controls:"
put "Up arrow - one more deep"
put "Down arrow - one less deep"
put "Escape - quit\n"
put "Enter the triangle base length (0>L>1500): "..
get Max

View.Set("graphics:" + intstr(Max) + ";" + intstr(round(Max*sind(60))+1) + ",offscreenonly,nocursor")
loop
    cls
    Draw_Triangle      (0,0,Max div 2,round(Max*sind(60)),Max,0,black)
    Sierpinski_Triangle(0,0,Max div 2,round(Max*sind(60)),Max,0,red,Iter)
    View.Update
    getch(Arrow)
    if ord(Arrow) = 200 then
        Iter += 1
    elsif ord(Arrow) = 208 and Iter > 0 then
        Iter -= 1
    end if
    exit when ord(Arrow) = 27       
end loop
Sponsor
Sponsor
Sponsor
sponsor
ecookman




PostPosted: Thu Dec 04, 2008 9:27 pm   Post subject: RE:Sierpinski Triangle

i do not understand the "one more deep" function


mind explaining?

(looks as if it just draws red triangles :confused: Confused )
HellblazerX




PostPosted: Thu Dec 04, 2008 9:45 pm   Post subject: Re: Sierpinski Triangle

By "one more deep", he means adding one more iteration to the triangle (though it is a funny, and grammatically incorrect, way of wording it). And in case you don't know what a Sierpinski triangle is:
http://en.wikipedia.org/wiki/Sierpinski_Triangle

Also:
code:
(0>L>1500)

I don't believe that's possible, and you should add some idiot-proofing for Max. Good job though.
ecookman




PostPosted: Thu Dec 04, 2008 9:47 pm   Post subject: RE:Sierpinski Triangle

ooh I feel like an idiot right now.... i thought he made up the triangle


Embarassed


thanks HellblazerX
Tyr_God_Of_War




PostPosted: Thu Dec 04, 2008 11:23 pm   Post subject: RE:Sierpinski Triangle

Very awesome. the (0>L>1500) is a bit misleading as small numbers don't work well. anything less then 10 doesn't really show up
gitoxa




PostPosted: Fri Dec 05, 2008 11:29 am   Post subject: Re: Sierpinski Triangle

HellblazerX @ Thu Dec 04, 2008 9:45 pm wrote:
By "one more deep", he means adding one more iteration to the triangle (though it is a funny, and grammatically incorrect, way of wording it). And in case you don't know what a Sierpinski triangle is:
http://en.wikipedia.org/wiki/Sierpinski_Triangle

Also:
code:
(0>L>1500)

I don't believe that's possible, and you should add some idiot-proofing for Max. Good job though.


Up - Up one stage
Down - Down one stage
Although quite frankly I really don't care, you get the idea if you have any idea what a Sierpinski Triangle is. Razz

And why should I add input error checking for a little program like this? How about if you're an idiot you're not allowed to use it Smile Besides, I'm pretty sick and tired of input error checking, i just finished writing 12 modules for input error checking for a database. lol
Nick




PostPosted: Fri Dec 05, 2008 1:12 pm   Post subject: RE:Sierpinski Triangle

a neat little input error checking line I like is:
code:
max(minNumber, min (maxNumber, input))
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  [ 7 Posts ]
Jump to:   


Style:  
Search: