Sub DrawBlock
' param bx, by, bz - base point
' param xmax, ymax, zmax - size of block array
param = ""
For _z = zmax To 0 Step -1
param["z"] = _z + bz
For _y = ymax To 0 Step -1
param["y"] = _y + by
For _x = xmax To 0 Step -1
param["x"] = _x + bx
color = Text.GetSubText(block[_z][_y], (xmax + 1) - _x, 1)
If color <> " " Then
param["color"] = palette[color]
DrawVoxel()
EndIf
EndFor
EndFor
EndFor
EndSub
Sub CalcColors
color = param["color"]
If color = "Transparent" Then
transparent = "True"
color = "Black"
Else
transparent = "False"
EndIf
Color_NameToColor()
colorTop = color
rate = 0.2
Color_Blacken()
colorLeft = color
color = colorTop
rate = 0.4
Color_Blacken()
colorRight = color
If transparent Then
colorTop = "#66" + Text.GetSubTextToEnd(colorTop, 2)
colorLeft = "#66" + Text.GetSubTextToEnd(colorLeft, 2)
colorRight = "#66" + Text.GetSubTextToEnd(colorRight, 2)
EndIf
EndSub
Sub CalcVertex
' Calcurate vertex between line p1-p2 and line p3-p4
a = p1["x"] * p2["y"] - p1["y"] * p2["x"]
b = p1["y"] - p2["y"]
c = p1["x"] - p2["x"]
d = p3["x"] * p4["y"] - p3["y"] * p4["x"]
e = p3["y"] - p4["y"]
f = p3["x"] - p4["x"]
If b = 0 Then
p["y"] = a / c
p["x"] = (f * p["y"] - d) / e
ElseIf c = 0 Then
p["x"] = -a / b
p["y"] = (e * p["x"] + d) / f
Else
p["x"] = ((a * f) - (c * d)) / ((c * e) - (b * f))
p["y"] = (a + b * p["x"]) / c
EndIF
EndSub
Sub Map2D
' param sx, sy, sz ≧ 0
' return p["x"], p["y"]
k = (rv - ru) / (rv + ru)
If sx = 0 Then
rx = 0
Else
rx = Math.Power(1 + k, Math.Log(sx) / Math.Log(2)) * ru
EndIf
If sy = 0 Then
ry = 0
Else
ry = Math.Power(1 + k, Math.Log(sy) / Math.Log(2)) * ru
EndIf
If sz = 0 Then
rz = 0
Else
rz = Math.Power(1 + k, Math.Log(sz) / Math.Log(2)) * ru
EndIf
If debug Then
TextWIndow.WriteLine("sz=" + sz)
TextWIndow.WriteLine("logsz=" + Math.Log(sz))
TextWindow.WriteLine("rx=" + rx)
TextWindow.WriteLine("ry=" + ry)
TextWindow.WriteLine("rz=" + rz)
EndIf
_px["x"] = xo + rx * Math.Sin(-a60)
_px["y"] = yo - rx * Math.Cos(-a60)
If debug Then
GraphicsWindow.PenColor = "Black"
GraphicsWindow.DrawLine(xo, yo, _px["x"], _px["y"])
EndIf
_py["x"] = xo + ry * Math.Sin(a60)
_py["y"] = yo - ry * Math.Cos(a60)
If debug Then
GraphicsWindow.DrawLine(xo, yo, _py["x"], _py["y"])
EndIf
_pz["x"] = xo
_pz["y"] = yo + rz
If debug Then
GraphicsWindow.DrawLine(xo, yo, _pz["x"], _pz["y"])
EndIf
p1 = _px
p2 = vy
p3 = _py
p4 = vx
CalcVertex()
If debug Then
GraphicsWindow.DrawLine(_px["x"], _px["y"], p["x"], p["y"])
GraphicsWindow.DrawLine(_py["x"], _py["y"], p["x"], p["y"])
EndIf
_pxy = p
p1 = _px
p2 = vz
p3 = _pz
p4 = vx
CalcVertex()
If debug Then
GraphicsWindow.DrawLine(_px["x"], _px["y"], p["x"], p["y"])
GraphicsWindow.DrawLine(_pz["x"], _pz["y"], p["x"], p["y"])
EndIf
_pxz = p
p1 = _pxz
p2 = vy
p3 = _pxy
p4 = vz
CalcVertex()
If debug Then
GraphicsWindow.DrawLine(_pxy["x"], _pxy["y"], p["x"], p["y"])
GraphicsWindow.DrawLine(_pxz["x"], _pxz["y"], p["x"], p["y"])
GraphicsWindow.DrawLine(p["x"] - 5, p["y"] - 5, p["x"] + 5, p["y"] + 5)
GraphicsWindow.DrawLine(p["x"] - 5, p["y"] + 5, p["x"] + 5, p["y"] - 5)
bc = GraphicsWindow.BrushColor
GraphicsWindow.BrushColor = "Black"
GraphicsWindow.DrawText(p["x"], p["y"], "(" + sx + "," + sy + "," + sz + ")")
GraphicsWindow.BrushColor = bc
EndIf
EndSub
Sub ShowCountDown
' param secRemain
sec = Math.Remainder(secRemain, 60)
secRemain = secRemain - sec
min = Math.Remainder(secRemain / 60, 60)
secRemain = secRemain - min * 60
hour = secRemain / 3600
If hour < 10 Then
time = Text.Append(0, hour) + ":"
Else
time = hour + ":"
EndIf
If min < 10 Then
time = time + 0 + min + ":"
Else
time = time + min + ":"
EndIf
If sec < 10 Then
time = time + 0 + sec
Else
time = time + sec
EndIf
Shapes.SetText(txt, time)
EndSub
Sub Color_HSLtoRGB
' Color | Convert HSL to RGB
' param hue - [0, 360) or UNDEFINED
' param lightness - [0, 1]
' param saturation - [0, 1]
' return r, g, b - RGB values 0..255
If lightness <= 0.5 Then
n2 = lightness * (1 + saturation)
Else
n2 = lightness + saturation - lightness * saturation
EndIf
n1 = 2 * lightness - n2
If saturation = 0 Then
r = Math.Round(lightness * 255)
g = Math.Round(lightness * 255)
b = Math.Round(lightness * 255)
Else
h = hue + 120
Color_Value()
r = value
h = hue
Color_Value()
g = value
h = hue - 120
Color_Value()
b = value
EndIf
EndSub
Sub Color_Blacken
' Color | Blacken given color
' param color - given color
' param rate - 0..1
' return color - color blackened
Color_NameToColor()
Color_ColorToRGB()
r = Math.Floor(r * (1 - rate))
g = Math.Floor(g * (1 - rate))
b = Math.Floor(b * (1 - rate))
color = GraphicsWindow.GetColorFromRGB(r, g, b)
EndSub
Sub Color_Whiten
' Color | Whiten given color
' param color - given color
' param rate - 0..1
' return color - color whitened
Color_NameToColor()
Color_ColorToRGB()
r = Math.Floor(r * (1 - rate) + 255 * rate)
g = Math.Floor(g * (1 - rate) + 255 * rate)
b = Math.Floor(b * (1 - rate) + 255 * rate)
color = GraphicsWindow.GetColorFromRGB(r, g, b)
EndSub
Sub Color_ColorToRGB
' Color | Convert color to RGB values
' param color - "#rrggbb" (hexadecimal values)
' return r, g, b - RGB values 0..255
sR = Text.GetSubText(color, 2, 2)
sG = Text.GetSubText(color, 4, 2)
sB = Text.GetSubText(color, 6, 2)
hex = sR
Math_Hex2Dec()
r = dec
hex = sG
Math_Hex2Dec()
g = dec
hex = sB
Math_Hex2Dec()
b = dec
EndSub
Sub Color_NameToColor
' Color | Convert color name to color
' param color - color name
' returns color -"#rrggbb"
If Text.StartsWith(color, "#") Then
color = Text.ConvertToUpperCase(color)
Else
color = Text.ConvertToLowerCase(color)
color = colors[color]
EndIf
EndSub
Sub Color_Value
' Color | Function value
' param n1, n2
' param h - [-120, 480)
' return value - 0..255
If h >= 360 Then
h = h - 360
EndIF
If h < 0 Then
h = h + 360
EndIF
If h < 60 Then
v = n1 + (n2 - n1) * h / 60
ElseIf h < 180 Then
v = n2
ElseIf h < 240 Then
v = n1 + (n2 - n1) * (240 - h) / 60
Else
v = n1
EndIf
value = Math.Round(v * 255)
EndSub
Sub Math_Hex2Dec
' Math | Convert hexadecimal to decimal
' param hex
' return dec
dec = 0
len = Text.GetLength(hex)
For ptr = 1 To len
dec = dec * 16 + Text.GetIndexOf("123456789ABCDEF", Text.GetSubText(hex, ptr, 1))
EndFor
EndSub