Showing posts with label turtle. Show all posts
Showing posts with label turtle. Show all posts

Tuesday, April 10, 2012

SmallBasic Turtle Logo Source Code 3

This is my polished version of Logo Turtle example code implementation in Small Basic. I hope you can easily understand the code. The code is optimized for simplicity, not execution speed. I will be bringing you more examples as time goes by. I don't really teach, but hopefully, by showing you the source codes, this can be a self-guided tutorial for you.

'Turtle Logo in Small Basic - NMC672
'by Harry Hardjono
'Turtle angle speed x1 y1 x0 y0
'Turtle move moveto penup pendown
'Turtle turn turnleft turnright
'Turtle show hide
'
' I hope this program is a good tutorial on how you can do your own programs!

GraphicsWindow.Show()
GraphicsWindow.Clear()

'----------------------------------------------
' Example Turtle Operation Code
'----------------------------------------------

cmd1="init"
Toitle()
cmd1="size"
cmd2=10
Toitle()
Program.Delay(1000)
cmd1="color"
cmd2="green"
Toitle()
Program.Delay(1000)
cmd1="color"
cmd2="red"
Toitle()
Program.Delay(1000)
cmd1="size"
cmd2=4
Toitle()
Program.Delay(1000)
cmd1="size"
cmd2=8
Toitle()



For loop=0 to 360 Step 20
cmd1="moveto"
cmd2=sx/2
cmd3=sy/2
Toitle()
cmd1="turn"
cmd2=loop
Toitle()
cmd1="move"
cmd2=200
Toitle()
Program.Delay(1000)
endfor



beginloop:
GraphicsWindow.Clear()
cmd1="init"
Toitle()
Program.Delay(2000)
cmd1="show"
Toitle()

For loop=1 to 50
cmd1="penup"
Toitle()
cmd1="moveto"
cmd2=Math.GetRandomNumber(sx)
cmd3=Math.GetRandomNumber(sy)
Toitle()
cmd1="pendown"
Toitle()
cmd1="color"
cmd2=GraphicsWindow.GetRandomColor()
Toitle()
cmd1="size"
cmd2=Math.GetRandomNumber(10)
Toitle()
For loop2=1 to Math.GetRandomNumber(6)
cmd1="turn"
cmd2=Math.GetRandomNumber(360)
Toitle()
cmd1="move"
cmd2=Math.GetRandomNumber(1000)
Toitle()
endfor
endfor
Program.Delay(15000)
Goto beginloop

'----------------------------------------------
' Main Turtle function.
'----------------------------------------------


Sub Toitle
'cmd1=command (input)
'cmd2=param1 (input)
'cmd3=param2 (input)
If (cmd1="init") then
sy=GraphicsWindow.Height
sx=GraphicsWindow.Width
x0=0
y0=0
x1=sx/2
y1=sy/2
px=x1
py=y1
p=1
angle=180
col="blue"
speed=100
siz=1
ts=1
DrawTurtle()
elseif (cmd1="penup") then
p=0
elseif (cmd1="pendown") then
p=1
elseif (cmd1="move") then
'forward
x0=x1
y0=y1
x1=x0+cmd2*Math.Sin(Math.GetRadians(angle))
y1=y0+cmd2*Math.Cos(Math.GetRadians(angle))
draw()
elseif (cmd1="moveto") then
x0=x1
y0=y1
x1=cmd2
y1=cmd3
draw()
elseif (cmd1="turn") then
angle=cmd2
DrawTurtle()
elseif (cmd1="turnleft") then
angle=angle+cmd2
DrawTurtle()
elseif (cmd1="turnright") then
angle=angle-cmd2
DrawTurtle()
elseif (cmd1="color") then
col=cmd2
DrawTurtle()
elseif (cmd1="size") then
siz=math.Max(1,cmd2)
siz=math.Min(10,siz)
DrawTurtle()
elseif (cmd1="speed") then
speed=cmd2
speed=Math.Max(0,speed)
speed=Math.Min(100,speed)
elseif (cmd1="show") then
ts=1
elseif (cmd1="hide") then
ts=0
endif

If (ts=1) then
MoveTurtle()
ShowTurtle()
Else
HideTurtle()
endif
endsub

'Draw
Sub Draw
If (p>0) then
If (x0<>x1 or y0<>y1) then
m=math.Max(math.Abs(x1-x0),math.Abs(y1-y0))
map_var["mx1"]=0
map_var["mx3"]=m
For i=0 To m
map_var["my1"]=x0
map_var["my3"]=x1
map_var["mx2"]=i
map()
px=map_var["my2"]
map_var["my1"]=y0
map_var["my3"]=y1
map_var["mx2"]=i
map()
py=map_var["my2"]
RemBugFix()
px=Math.Remainder(px,sx)
py=Math.Remainder(py,sy)
GraphicsWindow.BrushColor=col
GraphicsWindow.FillEllipse(px,py,siz,siz)
MoveTurtle()
If (speed<100) then
Program.Delay(100-speed)
endif
EndFor
endif
endif

'These really need to be Mod
'Seems buggy - doesn't work with negative number?
RemBugFix2()
x1=Math.Remainder(x1,sx)
y1=Math.Remainder(y1,sy)
angle=Math.Remainder(angle,360)

endsub

'----------------------------------------------
'map function
'----------------------------------------------
Sub map
'x1-x2-x3 y1-y2-y3
'(x2-x1)/(x3-x1)=(y2-y1)/(y3-y1)
'y1+(y3-y1)*(x2-x1)/(x3-x1)=y2
map_var["my2"]=((map_var["my3"]-map_var["my1"])*(map_var["mx2"]-map_var["mx1"])/(map_var["mx3"]-map_var["mx1"]))+map_var["my1"]
EndSub


'----------------------------------------------
'Turtle Operations - Could've been Shape Operation had Rotate Triangle not buggy
'----------------------------------------------
Sub DrawTurtle
'Sets size, color, and rotation of Turtle
Shapes.Remove(TT) 'Nothing happens if you remove empty shape. Good!
DrawTurtle_local["tx1"]=(10+siz)*Math.Sin(Math.GetRadians(angle))
DrawTurtle_local["ty1"]=(10+siz)*Math.Cos(Math.GetRadians(angle))
DrawTurtle_local["tx2"]=(10+siz)*Math.Sin(Math.GetRadians(angle+140))
DrawTurtle_local["ty2"]=(10+siz)*Math.Cos(Math.GetRadians(angle+140))
DrawTurtle_local["tx3"]=(10+siz)*Math.Sin(Math.GetRadians(angle+220))
DrawTurtle_local["ty3"]=(10+siz)*Math.Cos(Math.GetRadians(angle+220))
GraphicsWindow.BrushColor=col
TT=Shapes.AddTriangle(DrawTurtle_local["tx1"],DrawTurtle_local["ty1"],DrawTurtle_local["tx2"],DrawTurtle_local["ty2"],DrawTurtle_local["tx3"],DrawTurtle_local["ty3"])
endsub

Sub MoveTurtle
'Move the turtle
Shapes.Move(TT,px+(siz/2),py+(siz/2))
endsub

Sub ShowTurtle
Shapes.ShowShape(TT)
endsub

Sub HideTurtle
Shapes.HideShape(TT)
endsub
'----------------------------------------------
' Various bug fix
'----------------------------------------------

Sub RemBugFix
While (px<0)
px=px+sx
EndWhile
While (py<0)
py=py+sy
EndWhile
endsub
Sub RemBugFix2
While (x1<0)
x1=x1+sx
EndWhile
While (y1<0)
y1=y1+sy
EndWhile
While (angle<0)
angle=angle+360
EndWhile
endsub

Tuesday, April 3, 2012

SmallBasic Turtle Logo Source Code 2

'Turtle Logo in Small Basic
'by Harry Hardjono
'Turtle angle speed x1 y1 x0 y0
'Turtle move moveto penup pendown
'Turtle turn turnleft turnright
'Turtle show hide
'
' I did this in one afternoon. Could've been faster if I ignore Show/Hide Turtle
' Updated with Shapes. Sure is a lot faster had I known this earlier!

GraphicsWindow.Show()
GraphicsWindow.Clear()

cmd1="init"
Toitle()
cmd1="size"
cmd2=8
Toitle()



For loop=1 to 360 Step 20
cmd1="moveto"
cmd2=sx/2
cmd3=sy/2
Toitle()
cmd1="turn"
cmd2=loop
Toitle()
cmd1="move"
cmd2=200
Toitle()
Program.Delay(1000)
endfor



beginloop:
GraphicsWindow.Clear()
cmd1="init"
Toitle()
Program.Delay(2000)
cmd1="show"
Toitle()

For loop=1 to 50
cmd1="penup"
Toitle()
cmd1="moveto"
cmd2=Math.GetRandomNumber(sx)
cmd3=Math.GetRandomNumber(sy)
Toitle()
cmd1="pendown"
Toitle()
cmd1="color"
cmd2=GraphicsWindow.GetRandomColor()
Toitle()
cmd1="size"
cmd2=Math.GetRandomNumber(10)
Toitle()
' Show Turtle is very slow!
' If (loop=10) Then
' cmd1="show"
' Toitle()
' ElseIf (loop=30) then
' cmd1="hide"
' Toitle()
' EndIf
For loop2=1 to Math.GetRandomNumber(6)
cmd1="turn"
cmd2=Math.GetRandomNumber(360)
Toitle()
cmd1="move"
cmd2=Math.GetRandomNumber(1000)
Toitle()
endfor
endfor
Program.Delay(15000)
Goto beginloop



Sub Toitle
'cmd1=command (input)
'cmd2=param1 (input)
'cmd3=param2 (input)
If (cmd1="init") then
sy=GraphicsWindow.Height
sx=GraphicsWindow.Width
'Bug: GetPixel does not take Background color
GraphicsWindow.BrushColor="white"
GraphicsWindow.FillRectangle(0,0,sx,sy)
x1=sx/2
y1=sy/2
x0=x1
y0=y1
p=1
angle=180
col="blue"
GraphicsWindow.BrushColor=col
siz=1
ts=1
SaveBG()
elseif (cmd1="penup") then
p=0
elseif (cmd1="pendown") then
p=1
elseif (cmd1="move") then
'forward
' LoadBG()
x0=x1
y0=y1
x1=x0+cmd2*Math.Sin(Math.GetRadians(angle))
y1=y0+cmd2*Math.Cos(Math.GetRadians(angle))
draw()
' SaveBG()
elseif (cmd1="moveto") then
' LoadBG()
x0=x1
y0=y1
x1=cmd2
y1=cmd3
draw()
' SaveBG()
elseif (cmd1="turn") then
LoadBG()
angle=cmd2
SaveBG()
elseif (cmd1="turnleft") then
LoadBG()
angle=angle+cmd2
SaveBG()
elseif (cmd1="turnright") then
LoadBG()
angle=angle-cmd2
SaveBG()
elseif (cmd1="color") then
col=cmd2
elseif (cmd1="size") then
siz=math.Max(1,cmd2)
siz=math.Min(10,siz)
elseif (cmd1="show") then
if (ts<>1) then
ts=1
SaveBG()
endif
elseif (cmd1="hide") then
LoadBG()
ts=0
endif

'These really need to be Mod
'Seems buggy - doesn't work with negative number?
' RemBugFix2()
'x1=Math.Remainder(x1,sx)
'y1=Math.Remainder(y1,sy)
'angle=Math.Remainder(angle,360)

If (ts=1) then
' DrawTurtle()
' Shapes.Move(TT,x1,y1)
Shapes.Move(TT,px+(siz/2),py+(siz/2))
endif

endsub

Sub DrawTurtle
tx1=10*Math.Sin(Math.GetRadians(angle))
ty1=10*Math.Cos(Math.GetRadians(angle))
tx2=10*Math.Sin(Math.GetRadians(angle+140))
ty2=10*Math.Cos(Math.GetRadians(angle+140))
tx3=10*Math.Sin(Math.GetRadians(angle+220))
ty3=10*Math.Cos(Math.GetRadians(angle+220))
TT=Shapes.AddTriangle(tx1,ty1,tx2,ty2,tx3,ty3)
endsub


Sub RemBugFix
While (px<0) px=px+sx EndWhile While (py<0) py=py+sy EndWhile endsub Sub RemBugFix2 While (x1<0) x1=x1+sx EndWhile While (y1<0) y1=y1+sy EndWhile While (angle<0) angle=angle+360 EndWhile endsub 'Draw Sub Draw If (p>0) then
If (x0<>x1 or y0<>y1) then
m=math.Max(math.Abs(x1-x0),math.Abs(y1-y0))
mx1=0
mx3=m
For i=0 To m
my1=x0
my3=x1
mx2=i
map()
px=my2
my1=y0
my3=y1
mx2=i
map()
py=my2
RemBugFix()
px=Math.Remainder(px,sx)
py=Math.Remainder(py,sy)
If (siz>1) then
GraphicsWindow.BrushColor=col
GraphicsWindow.FillEllipse(px,py,siz,siz)
else
GraphicsWindow.SetPixel(px,py,col)
endif
Shapes.Move(TT,px+(siz/2),py+(siz/2))
EndFor
endif
endif

'These really need to be Mod
'Seems buggy - doesn't work with negative number?
RemBugFix2()
x1=Math.Remainder(x1,sx)
y1=Math.Remainder(y1,sy)
angle=Math.Remainder(angle,360)

endsub

'map function
Sub map
'x1-x2-x3 y1-y2-y3
'(x2-x1)/(x3-x1)=(y2-y1)/(y3-y1)
'(y3-y1)*(x2-x1)/(x3-x1)=(y2-y1)
'y1+(y3-y1)*(x2-x1)/(x3-x1)=y2
my2=((my3-my1)*(mx2-mx1)/(mx3-mx1))+my1
EndSub


sub SaveBG
If (ts=1) then
DrawTurtle()



' BGc=0
' BGx1=Math.Floor(Math.Max(0,x1-10))
' BGx1=Math.Floor(Math.Min(sx,BGx1))
' BGy1=Math.Floor(Math.Max(0,y1-10))
' BGy1=Math.Floor(Math.Min(sy,BGy1))
' BGx2=Math.Floor(Math.Max(0,x1+10))
' BGx2=Math.Floor(Math.Min(sx,BGx2))
' BGy2=Math.Floor(Math.Max(0,y1+10))
' BGy2=Math.Floor(Math.Min(sy,BGy2))
' For BGy=BGy1 To BGy2
' For BGx=BGx1 To BGx2
' BGc=BGc+1
' BG_Array[BGc]=GraphicsWindow.GetPixel(BGx,BGy)
' EndFor
' endfor
' TextWindow.Write("SaveBG ")
' TextWindow.Write(BGx1)
' TextWindow.Write(" ")
' TextWindow.Write(BGy1)
' TextWindow.Write(" ")
' TextWindow.Write(BGx2)
' TextWindow.Write(" ")
' TextWindow.Write(BGy2)
' TextWindow.WriteLine(" ")
endif
endsub

Sub LoadBG
If (ts=1) then
Shapes.Remove(TT)

' BGc=0
' For BGy=BGy1 To BGy2
' For BGx=BGx1 To BGx2
' BGc=BGc+1
' GraphicsWindow.SetPixel(BGx,BGy,BG_Array[BGc])
' EndFor
' endfor
' TextWindow.Write("LoadBG")
' TextWindow.Write(BGx1)
' TextWindow.Write(" ")
' TextWindow.Write(BGy1)
' TextWindow.Write(" ")
' TextWindow.Write(BGx2)
' TextWindow.Write(" ")
' TextWindow.Write(BGy2)
' TextWindow.WriteLine(" ")
endif
EndSub

SmallBasic Turtle Logo Source Code 2

The bit about Show/Hide Turtle was very time consuming. I do wish Small Basic has better Screen Buffer capabilities.










Turtle Logo in Small Basic
'by Harry Hardjono
'Turtle angle speed x1 y1 x0 y0
'Turtle move moveto penup pendown
'Turtle turn turnleft turnright
'Turtle show hide
'
' I did this in one afternoon. Could've been faster if I ignore Show/Hide Turtle

GraphicsWindow.Show()
GraphicsWindow.Clear()


beginloop:
GraphicsWindow.Clear()
cmd1="init"
Toitle()
Program.Delay(2000)
cmd1="hide"
Toitle()

For loop=1 to 50
cmd1="penup"
Toitle()
cmd1="moveto"
cmd2=Math.GetRandomNumber(sx)
cmd3=Math.GetRandomNumber(sy)
Toitle()
cmd1="pendown"
Toitle()
cmd1="color"
cmd2=GraphicsWindow.GetRandomColor()
Toitle()
cmd1="size"
cmd2=Math.GetRandomNumber(10)
Toitle()
' Show Turtle is very slow!
' If (loop=10) Then
' cmd1="show"
' Toitle()
' ElseIf (loop=30) then
' cmd1="hide"
' Toitle()
' EndIf
For loop2=1 to Math.GetRandomNumber(6)
cmd1="turn"
cmd2=Math.GetRandomNumber(360)
Toitle()
cmd1="move"
cmd2=Math.GetRandomNumber(1000)
Toitle()
endfor
endfor
Program.Delay(5000)
Goto beginloop



Sub Toitle
'cmd1=command (input)
'cmd2=param1 (input)
'cmd3=param2 (input)
If (cmd1="init") then
sy=GraphicsWindow.Height
sx=GraphicsWindow.Width
'Bug: GetPixel does not take Background color
GraphicsWindow.BrushColor="white"
GraphicsWindow.FillRectangle(0,0,sx,sy)
x1=sx/2
y1=sy/2
x0=x1
y0=y1
p=1
angle=180
col="blue"
GraphicsWindow.BrushColor=col
siz=1
ts=1
SaveBG()
elseif (cmd1="penup") then
p=0
elseif (cmd1="pendown") then
p=1
elseif (cmd1="move") then
'forward
LoadBG()
x0=x1
y0=y1
x1=x0+cmd2*Math.Sin(Math.GetRadians(angle))
y1=y0+cmd2*Math.Cos(Math.GetRadians(angle))
draw()
SaveBG()
elseif (cmd1="moveto") then
LoadBG()
x0=x1
y0=y1
x1=cmd2
y1=cmd3
draw()
SaveBG()
elseif (cmd1="turn") then
LoadBG()
angle=cmd2
SaveBG()
elseif (cmd1="turnleft") then
LoadBG()
angle=angle+cmd2
SaveBG()
elseif (cmd1="turnright") then
LoadBG()
angle=angle-cmd2
SaveBG()
elseif (cmd1="color") then
col=cmd2
elseif (cmd1="size") then
siz=math.Max(1,cmd2)
siz=math.Min(10,siz)
elseif (cmd1="show") then
if (ts<>1) then
ts=1
SaveBG()
endif
elseif (cmd1="hide") then
LoadBG()
ts=0
endif

'These really need to be Mod
'Seems buggy - doesn't work with negative number?
' RemBugFix2()
'x1=Math.Remainder(x1,sx)
'y1=Math.Remainder(y1,sy)
'angle=Math.Remainder(angle,360)

If (ts=1) then
DrawTurtle()
endif

endsub

Sub DrawTurtle
tx1=x1+10*Math.Sin(Math.GetRadians(angle))
ty1=y1+10*Math.Cos(Math.GetRadians(angle))
tx2=x1+10*Math.Sin(Math.GetRadians(angle+140))
ty2=y1+10*Math.Cos(Math.GetRadians(angle+140))
tx3=x1+10*Math.Sin(Math.GetRadians(angle+220))
ty3=y1+10*Math.Cos(Math.GetRadians(angle+220))
GraphicsWindow.FillTriangle(tx1,ty1,tx2,ty2,tx3,ty3)
endsub


Sub RemBugFix
While (px<0) px=px+sx EndWhile While (py<0) py=py+sy EndWhile endsub Sub RemBugFix2 While (x1<0) x1=x1+sx EndWhile While (y1<0) y1=y1+sy EndWhile While (angle<0) angle=angle+360 EndWhile endsub 'Draw Sub Draw If (p>0) then
If (x0<>x1 or y0<>y1) then
m=math.Max(math.Abs(x1-x0),math.Abs(y1-y0))
mx1=0
mx3=m
For i=0 To m
my1=x0
my3=x1
mx2=i
map()
px=my2
my1=y0
my3=y1
mx2=i
map()
py=my2
RemBugFix()
px=Math.Remainder(px,sx)
py=Math.Remainder(py,sy)
If (siz>1) then
GraphicsWindow.BrushColor=col
GraphicsWindow.FillEllipse(px,py,siz,siz)
else
GraphicsWindow.SetPixel(px,py,col)
endif
EndFor
endif
endif

'These really need to be Mod
'Seems buggy - doesn't work with negative number?
RemBugFix2()
x1=Math.Remainder(x1,sx)
y1=Math.Remainder(y1,sy)
angle=Math.Remainder(angle,360)

endsub

'map function
Sub map
'x1-x2-x3 y1-y2-y3
'(x2-x1)/(x3-x1)=(y2-y1)/(y3-y1)
'(y3-y1)*(x2-x1)/(x3-x1)=(y2-y1)
'y1+(y3-y1)*(x2-x1)/(x3-x1)=y2
my2=((my3-my1)*(mx2-mx1)/(mx3-mx1))+my1
EndSub


sub SaveBG
If (ts=1) then
BGc=0
BGx1=Math.Floor(Math.Max(0,x1-10))
BGx1=Math.Floor(Math.Min(sx,BGx1))
BGy1=Math.Floor(Math.Max(0,y1-10))
BGy1=Math.Floor(Math.Min(sy,BGy1))
BGx2=Math.Floor(Math.Max(0,x1+10))
BGx2=Math.Floor(Math.Min(sx,BGx2))
BGy2=Math.Floor(Math.Max(0,y1+10))
BGy2=Math.Floor(Math.Min(sy,BGy2))
For BGy=BGy1 To BGy2
For BGx=BGx1 To BGx2
BGc=BGc+1
BG_Array[BGc]=GraphicsWindow.GetPixel(BGx,BGy)
EndFor
endfor
' TextWindow.Write("SaveBG ")
' TextWindow.Write(BGx1)
' TextWindow.Write(" ")
' TextWindow.Write(BGy1)
' TextWindow.Write(" ")
' TextWindow.Write(BGx2)
' TextWindow.Write(" ")
' TextWindow.Write(BGy2)
' TextWindow.WriteLine(" ")
endif
endsub

Sub LoadBG
If (ts=1) then
BGc=0
For BGy=BGy1 To BGy2
For BGx=BGx1 To BGx2
BGc=BGc+1
GraphicsWindow.SetPixel(BGx,BGy,BG_Array[BGc])
EndFor
endfor
' TextWindow.Write("LoadBG")
' TextWindow.Write(BGx1)
' TextWindow.Write(" ")
' TextWindow.Write(BGy1)
' TextWindow.Write(" ")
' TextWindow.Write(BGx2)
' TextWindow.Write(" ")
' TextWindow.Write(BGy2)
' TextWindow.WriteLine(" ")
endif
EndSub

Monday, April 2, 2012

SmallBasic Turtle Logo Source Code

Just how hard is it to code a turtle? Not hard at all, if one hour is all it takes to code this. I'm still missing Show and Hide Turtle, but hey, it's only one hour!















'Turtle Logo in Small Basic
'by Harry Hardjono
'Turtle angle speed x1 y1 x0 y0
'Turtle move moveto penup pendown
'Turtle turn turnleft turnright
' Only took me 1 hour to do this. :)
'
GraphicsWindow.Show()
GraphicsWindow.Clear()

cmd1="init"
Toitle()
cmd1="move"
cmd2="50"
Toitle()
cmd1="turnright"
cmd2="90"
Toitle()
cmd1="move"
cmd2="50"
Toitle()
cmd1="turnright"
cmd2="90"
Toitle()
cmd1="move"
cmd2="50"
Toitle()
cmd1="turnright"
cmd2="90"
Toitle()
cmd1="move"
cmd2="150"
Toitle()
cmd1="turnleft"
cmd2="90"
Toitle()
cmd1="move"
cmd2="50"
Toitle()
cmd1="turnright"
cmd2="90"
Toitle()
cmd1="moveto"
cmd2="20"
cmd3="50"
Toitle()
cmd1="turn"
cmd2="20"
Toitle()
cmd1="move"
cmd2="1500"
Toitle()
cmd1="turnright"
cmd2="100"
Toitle()
cmd1="move"
cmd2="1500"
Toitle()
cmd1="turnright"
cmd2="100"
Toitle()
cmd1="move"
cmd2="1500"
Toitle()
cmd1="turnright"
cmd2="100"
Toitle()
cmd1="move"
cmd2="1500"
Toitle()



Sub Toitle
'cmd1=command (input)
'cmd2=param1 (input)
'cmd3=param2 (input)
If (cmd1="init") then
sy=GraphicsWindow.Height
sx=GraphicsWindow.Width
x1=sx/2
y1=sy/2
x0=x1
y0=y1
p=1
angle=90
elseif (cmd1="penup") then
p=0
elseif (cmd1="pendown") then
p=1
elseif (cmd1="move") then
'forward
x0=x1
y0=y1
x1=x0+cmd2*Math.Sin(Math.GetRadians(angle))
y1=y0+cmd2*Math.Cos(Math.GetRadians(angle))
draw()
elseif (cmd1="moveto") then
x0=x1
y0=y1
x1=cmd2
y1=cmd3
draw()
elseif (cmd1="turn") then
angle=cmd2
elseif (cmd1="turnleft") then
angle=angle+cmd2
elseif (cmd1="turnright") then
angle=angle-cmd2
endif


'These really need to be Mod
'Seems buggy - doesn't work with negative number?
RemBugFix2()
x1=Math.Remainder(x1,sx)
y1=Math.Remainder(y1,sy)
angle=Math.Remainder(angle,360)
endsub

Sub RemBugFix
While (px<0)
px=px+sx
EndWhile
While (py<0)
py=py+sy
EndWhile
endsub
Sub RemBugFix2
While (x1<0)
x1=x1+sx
EndWhile
While (y1<0)
y1=y1+sy
EndWhile
While (angle<0)
angle=angle+360
EndWhile
endsub
'Draw
Sub Draw
If (p>0) then
If (x0<>x1 or y0<>y1) then
m=math.Max(math.Abs(x1-x0),math.Abs(y1-y0))
mx1=0
mx3=m
For i=0 To m
my1=x0
my3=x1
mx2=i
map()
px=my2
my1=y0
my3=y1
mx2=i
map()
py=my2
RemBugFix()
px=Math.Remainder(px,sx)
py=Math.Remainder(py,sy)
GraphicsWindow.SetPixel(px,py,"blue")
EndFor
endif
endif
endsub


'map function
Sub map
'x1-x2-x3 y1-y2-y3
'(x2-x1)/(x3-x1)=(y2-y1)/(y3-y1)
'(y3-y1)*(x2-x1)/(x3-x1)=(y2-y1)
'y1+(y3-y1)*(x2-x1)/(x3-x1)=y2
my2=((my3-my1)*(mx2-mx1)/(mx3-mx1))+my1
EndSub