#!/usr/local/bin/cz --
# somewhat improved turtle / coordinate graphics
# TODO merge / replace libb/turtle.b ?
use b

Main:
	space()
	tri()
	home(100, 100, 30, 1)
	tri()

tri():
	repeat(3)
		go(100,0,-60)
		Turn(120)
		turtle_zoom(1.2)

# spider graphics?
# To draw a n-gon of radius 1:
# move()
# repeat(n):
#	go(1)   # or go()
#	point()
#	go(-1)
#	turn(360/n)

struct turtle:
	num x, y
	num a
	num z
	num c, s
	bit draw
#	bit fill

local turtle ts = { 0, 0, 0, 1, 1, 0, 1 }

turtle_zoom(num zf)
	ts.z *= zf
	calc_ts_sin_cos()

def go(x) go1(x)
def go(x, y) go2(x, y)
def go(x, y, a) go3(x, y, a)

go1(num x)
	num x1 = ts.x + ts.c * x
	num y1 = ts.y + ts.s * x
	if ts.draw:
		draw(x1, y1)
	 else:
		move(x1, y1)
	ts.x = x1
	ts.y = y1

go2(num x, num y)
	num x1 = ts.x + ts.c * x - ts.s * y
	num y1 = ts.y + ts.s * x + ts.c * y
	if ts.draw:
		draw(x1, y1)
	 else:
		move(x1, y1)
	ts.x = x1
	ts.y = y1

go3(num x, num y, num a)
	num x1 = ts.x + ts.c * x - ts.s * y
	num y1 = ts.y + ts.s * x + ts.c * y
	if ts.draw:
		draw(x1, y1, a)
	 else:
		move(x1, y1)
	ts.x = x1
	ts.y = y1

def home() home4(0, 0, 0, 1)
def home(x, y) home4(x, y, 0, 1)
def home(x, y, a) home4(x, y, a, 1)
def home(x, y, a, z) home4(x, y, a, z)
home4(num x, num y, num a, num z)
	ts.x = x
	ts.y = y
	ts.a = a
	ts.z = z
	calc_ts_sin_cos()
	move(x, y)

calc_ts_sin_cos():
	Sin_Cos(ts.s, ts.c, ts.a)
	ts.s *= ts.z
	ts.c *= ts.z

Turn(num a):
	ts.a += a
	calc_ts_sin_cos()
