'...SOW _Langtons Ant ver 1.0
'...using BrowserBasic ver 0.5
'...Created on 20130714


'...SOW Define Global Variables
var Ant_Pos_x as number
var Ant_Pos_y as number
var Ant_Hdg_x as number
var Ant_Hdg_y as number
var iii as number
var stop_running as number

'-------------------------------------------------------------------------------
Function OnLoad()

	setBackgroundColor(255,128,128)
	Ant_Pos_x = 100
	Ant_Pos_y = 100
	Ant_Hdg_x = 0
	Ant_Hdg_y = 1

EndFunction '... OnLoad()

'-------------------------------------------------------------------------------

Function OnDraw()

	setBackgroundColor(255,255,255)'... white
	setcolor(0,0,0)'... black

	for iii = 1 to 12000

		Ant_pixel_color = getpixel(Ant_Pos_x, Ant_Pos_y)
		
		if (Ant_pixel_color == 0) then '...pixel is black
			turn90degreesright
			MoveForward
			setcolor(0,0,0)'...setcolour("black")
			setpixel(Ant_Pos_x, Ant_Pos_y)
		else '...pixel is white
			turn90degreesleft
			MoveForward
			setcolor(255,255,255) '...setcolour("white")
			setpixel(Ant_Pos_x, Ant_Pos_y)
		endif
		
	next iii
	
EndFunction'...OnDraw()

'-------------------------------------------------------------

Function turn90degreesright
	'...we apply the matrix
	'... ( 0, -1)
	'... ( 1,  0)
	var New_Ant_Hdg_x  as number
	var New_Ant_Hdg_y  as number

	New_Ant_Hdg_x = ( 0 * Ant_Hdg_x) + (-1 * Ant_Hdg_y)
	New_Ant_Hdg_y = ( 1 * Ant_Hdg_x) + ( 0 * Ant_Hdg_y)
	Ant_Hdg_x=New_Ant_Hdg_x
	Ant_Hdg_y=New_Ant_Hdg_y
endFunction '... turn90degreesright

'-------------------------------------------------------------
Function turn90degreesleft
	'...we apply the matrix
	'... ( 0, 1)
	'... (-1, 0)
	var New_Ant_Hdg_x  as number
	var New_Ant_Hdg_y  as number

	New_Ant_Hdg_x = ( 0 * Ant_Hdg_x) + (1 * Ant_Hdg_y)
	New_Ant_Hdg_y = (-1 * Ant_Hdg_x) + (0 * Ant_Hdg_y)
	
	Ant_Hdg_x=New_Ant_Hdg_x
	Ant_Hdg_y=New_Ant_Hdg_y
endFunction '... turn90degreesleft

'-------------------------------------------------------------
Function MoveForward
	Ant_Pos_x = Ant_Pos_x  + (1 * Ant_Hdg_x)
	Ant_Pos_y = Ant_Pos_y  + (1 * Ant_Hdg_y)
	
	var Ant_off_Grid as number
	Ant_off_Grid = false
	if Ant_Pos_x < Ant_Pos_Xmin or Ant_Pos_x> Ant_Pos_Xmax then
		Ant_off_Grid = true
	elseif Ant_Pos_y < Ant_Pos_Ymin or Ant_Pos_y> Ant_Pos_Ymax  then
		Ant_off_Grid = true
	endif
	if Ant_off_Grid = true then
		stop_running = true
	endif
	
endFunction '...MoveForward
