Here is a simple Theodorus spiral.

Code: Select all

```
' vectors.bi
type TVector
x as double
y as double
end type
declare function Rotate(byval AVector as TVector, byval ATheta as double) as TVector
declare function Perpendicular(byval AVector as TVector, byval AClockWise as boolean = FALSE) as TVector
declare function Normalize(byval AVector as TVector) as TVector
declare function Add(byval AVector1 as TVector, byval AVector2 as TVector) as TVector
declare function Scale(byval AVector as TVector, byval AFactor as double) as TVector
declare function Mirror(byval AVector as TVector) as TVector
function Rotate(byval AVector as TVector, byval ATheta as double) as TVector
/'
x' = x cos θ − y sin θ
y' = x sin θ + y cos θ
'/
dim result as TVector
with AVector
result.x = .x * Cos(ATheta) - .y * Sin(ATheta)
result.y = .x * Sin(ATheta) + .y * Cos(ATheta)
end with
return result
end function
function Perpendicular(byval AVector as TVector, byval AClockWise as boolean) as TVector
/'
x' = x cos θ − y sin θ
y' = x sin θ + y cos θ
'/
dim result as TVector
dim as integer LSign = IIf(AClockWise, -1, 1)
with AVector
result.x = -.y * LSign
result.y = .x * LSign
end with
return result
end function
function Normalize(byval AVector as TVector) as TVector
dim result as TVector
dim LLength as double
with AVector
LLength = Sqr(.x * .x + .y * .y)
result.x = .x / LLength
result.y = .y / LLength
end with
return result
end function
function Add(byval AVector1 as TVector, byval AVector2 as TVector) as TVector
dim result as TVector
result.x = AVector1.x + AVector2.x
result.y = AVector1.y + AVector2.y
return result
end function
function Scale(byval AVector as TVector, byval AFactor as double) as TVector
dim result as TVector
with AVector
result.x = .x * AFactor
result.y = .y * AFactor
end with
return result
end function
function Mirror(byval AVector as TVector) as TVector
dim result as TVector
with AVector
result.x = .x
result.y = -.y
end with
return result
end function
```

Code: Select all

```
' theodorus.bas
#include "vectors.bi"
const SW = 640
const SH = SW
const CNumTriangles = 16
const CScale = 72
dim shared LCenter as TVector = (SW / 2, SH / 2)
dim shared LColor as uinteger = rgb(0, 255, 0)
sub DrawLine(byval APoint1 as TVector, byval APoint2 as TVector)
APoint1 = Add(LCenter, Mirror(Scale(APoint1, CScale)))
APoint2 = Add(LCenter, Mirror(Scale(APoint2, CScale)))
line(APoint1.x, APoint1.y)-(APoint2.x, APoint2.y), LColor
end sub
screenres SW, SH, 32
windowtitle "Theodorus Spiral"
dim A as TVector = (0.0, 0.0)
dim B as TVector = (1.0, 0.0)
dim C as TVector
for i as integer = 1 to CNumTriangles
C = Add(B, Normalize(Perpendicular(B)))
DrawLine(A, B)
DrawLine(B, C)
B = C
next
' Close the last triangle
DrawLine(C, A)
sleep
```