
-----------------------------------
gitoxa
Thu Dec 04, 2008 8:56 pm

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 :)

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

-----------------------------------
ecookman
Thu Dec 04, 2008 9:27 pm

RE:Sierpinski Triangle
-----------------------------------
i do not understand the "one more deep" function


mind explaining? 

(looks as if it just draws red triangles :confused: :? )

-----------------------------------
HellblazerX
Thu Dec 04, 2008 9:45 pm

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:
(0>L>1500)
I don't believe that's possible, and you should add some idiot-proofing for Max.  Good job though.

-----------------------------------
ecookman
Thu Dec 04, 2008 9:47 pm

RE:Sierpinski Triangle
-----------------------------------
ooh I feel like an idiot right now.... i thought he made up the triangle


:oops:


thanks HellblazerX

-----------------------------------
Tyr_God_Of_War
Thu Dec 04, 2008 11:23 pm

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
Fri Dec 05, 2008 11:29 am

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:
(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. :P

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 :) 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
Fri Dec 05, 2008 1:12 pm

RE:Sierpinski Triangle
-----------------------------------
a neat little input error checking line I like is:
max(minNumber, min (maxNumber, input))
