Hi,
#!/usr/bin/yabasic2008
// Windows users:
// remove the "draw" commands, remove the "fullscreen" tag from the "open window" command and change the size of the graphic window to suit the screen resolution.
gosub init
GetRandomPoint()
GetMidPointOfLine()
dot MidPoint(1), MidPoint(2)
draw
do
// GetRandomPoint()
RandomPoint(1) = MidPoint(1) // Make the start of the new line be the old mid point
RandomPoint(2) = MidPoint(2)
// pick a random vertex
Vertex = int(ran(3)) + 1
// find length of line from the point to the vertex
// Length = FindLength()
// ^Not needed^
// find middle of the line
GetMidPointOfLine()
// draw dot at the mid point
dot MidPoint(1), MidPoint(2)
draw
loop
end
label init
clear screen
print "Sierpinski's Triangle (kind of...)\n------------ --------\n\nWhen the graphic window appears click anywhere within it to mark the three vertices.\n\nPress any key to continue."
repeat
until(inkey$ <> "")
open window 100, 100, fullscreen
// vertex positions
dim Vertices(3, 2)
for n = 1 to 3
repeat
key$ = inkey$(.1)
if left$(key$, 4) = "MB1u" print key$
until(left$(key$, 4) = "MB1u")
Vertices(n, 1) = mousex(key$)
Vertices(n, 2) = mousey(key$)
beep
next n
// triangle vertices(1, 1), vertices(1, 2), vertices(2, 1), vertices(2, 2), vertices(3, 1), vertices(3, 2)
// draw
dim RandomPoint(2)
dim MidPoint(2)
return
sub GetRandomPoint()
local ran1, ran2, ran3
repeat
ran1 = ran()
ran2 = ran()
until(ran1 + ran2 < 1)
ran3 = 1 - (ran1 + ran2)
RandomPoint(1) = int((Vertices(1, 1) * ran1) + (Vertices(2, 1) * ran2) + (Vertices(3, 1) * ran3))
RandomPoint(2) = int((Vertices(1, 2) * ran1) + (Vertices(2, 2) * ran2) + (Vertices(3, 2) * ran3))
end sub
sub FindLength()
local dx, dy
// sqrt((x2-x1)^2) + ((y2-y1)^2)
dx = abs(RandomPoint(1) - Vertices(Vertex, 1)) ^ 2
dy = abs(RandomPoint(2) - Vertices(Vertex, 2)) ^ 2
return sqrt(abs(dx - dy))
end sub
sub GetMidPointOfLine()
local SmallX, SmallY, x1, x2, y1, y2
x1 = RandomPoint(1)
x2 = Vertices(Vertex, 1)
y1 = RandomPoint(2)
y2 = Vertices(Vertex, 2)
SmallX = min(x1, x2)
SmallY = min(y1, y2)
MidPoint(1) = (abs(x1 - x2) / 2) + SmallX
MidPoint(2) = (abs(y1 - y2) / 2) + SmallY
end sub
Derek.