' 3D Text, Noega33, Jan 6, 2012
'Inspired from MM2forever Code published on Basic4Android Blog
'Draws text in a 3D rotating cube, text is entered line 84. Beware complete alphabet not yet available
' alternatively it can plot a curve using the "courbe" sub: comment lines 119-130 & 138, uncomment 131
'
Sub Process_Globals
Dim time As Timer
End Sub
Sub Globals
Rot_X=0
Rot_Y=0
Rot_Z=0
zoom = 8000
depth = 40
Dim xarr(16) As Int
Dim yarr(16) As Int
Dim zarr(16) As Int
Dim xAxe(6) As Int
Dim yAxe(6) As Int
Dim zAxe(6) As Int
Dim xpoints(400) As Float
Dim ypoints(400) As Float
Dim x(400) As Float
Dim xcourbe(400) As Float
Dim ycourbe(400) As Float
Dim zcourbe(400) As Float
Dim EchelX As Float
Dim k As Int
Dim i As Int
Dim can As Canvas
Dim courbe_Max As Int
Dim EditText1 As EditText
Dim EditText2 As EditText
Dim EditText3 As EditText
Dim X_Angle As SeekBar
Dim Y_Angle As SeekBar
Dim Z_Angle As SeekBar
Dim Label1 As Label
Dim Label2 As Label
Dim Label3 As Label
Dim AFont As String
Dim DecodeStr(30) As String
Dim Automatic As CheckBox
Dim Autom As Boolean
Dim Text As String
Dim Car As String
Dim NbLtr As Int
Dim Color_line As Int
Dim Taille As Int
End Sub
Sub init
' Cube Coordinates
' Note: All coordinates are normalized from -1 to +1
' Some segments will be drawn twice
xarr(0)=1: yarr(0)=1: zarr(0)=1
xarr(1)=1: yarr(1)=1: zarr(1)=-1
xarr(2)=1: yarr(2)=-1: zarr(2)=-1
xarr(3)=1: yarr(3)=-1: zarr(3)=1
xarr(4)=1: yarr(4)=1: zarr(4)=1
xarr(5)=-1: yarr(5)=1: zarr(5)=1
xarr(6)=-1: yarr(6)=1: zarr(6)=-1
xarr(7)=-1: yarr(7)=-1: zarr(7)=-1
xarr(8)=-1: yarr(8)=-1: zarr(8)=1
xarr(9)=-1: yarr(9)=1: zarr(9)=1
xarr(10)=-1: yarr(10)=1: zarr(10)=-1
xarr(11)=1: yarr(11)=1: zarr(11)=-1
xarr(12)=1: yarr(12)=-1: zarr(12)=-1
xarr(13)=-1: yarr(13)=-1: zarr(13)=-1
xarr(14)=-1: yarr(14)=-1: zarr(14)=1
xarr(15)=1: yarr(15)=-1: zarr(15)=1
'Axes Coordinates
xAxe(0)=-1: yAxe(0)=0: zAxe(0)=0
xAxe(1)=1: yAxe(1)=0: zAxe(1)=0
xAxe(2)=0: yAxe(2)=-1: zAxe(2)=0
xAxe(3)=0: yAxe(3)=1: zAxe(3)=0
xAxe(4)=0: yAxe(4)=0: zAxe(4)=-1
xAxe(5)=0: yAxe(5)=0: zAxe(5)=1
Automatic_CheckedChange(True)
Text="NOEGA33"
End Sub
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("3D_Golf.bal")
can.Initialize(Activity)
init
time.Initialize("Time",10)
time.Enabled=True
End Sub
Sub Activity_Resume
time.Enabled=True
End Sub
Sub Activity_Pause (UserClosed As Boolean)
time.Enabled=False
End Sub
Sub plot_object(xrot,yrot,zrot)
' Plot Cube
For i=0 To 15
threedpoints(xarr(i),yarr(i),zarr(i),xrot,yrot,zrot)
Next
courbe_Max=16
Color_line=Colors.White
Taille=1
linecon3
'Plot Axis
For i=0 To 5
threedpoints(xAxe(i),yAxe(i),zAxe(i),xrot,yrot,zrot)
Next
linecon2
'Plot Text
NbLtr=Text.Length
EchelX=2/(NbLtr)
For k=0 To NbLtr-1
Car= Text.CharAt(k)
vector(Car)
courbe_Max=(AFont.Length-1)/4
DecodeStr=Regex.Split(",",AFont)
For p=0 To courbe_Max-1
xcourbe(p)=k*EchelX-1+EchelX*0.1*DecodeStr(1+p*2)
ycourbe(p)=0
zcourbe(p)=EchelX*0.1*DecodeStr(2+p*2)
Next
'courbe
For i =0 To courbe_Max-1
threedpoints(xcourbe(i),ycourbe(i),zcourbe(i),xrot,yrot,zrot)
Next
Color_line=Colors.Green
Taille=4
linecon3
Next
End Sub
Sub threedpoints(xthreed,ythreed,zthreed,rotx,roty,rotz)
'3D Engine...
x2 = (xthreed * CosD(rotz)) - (ythreed * SinD(rotz))
y2 = (xthreed * SinD(rotz)) + (ythreed * CosD(rotz))
z2 = (x2 * SinD(roty)) + (zthreed * CosD(roty))
x3 = (x2 * CosD(roty)) - (zthreed * SinD(roty))
y3 = (y2 * CosD(rotx)) - (z2 * SinD(rotx))
z3 = (y2 * SinD(rotx)) + (z2 * CosD(rotx))
xpoints(i) = zoom * (x3 / (z3 + depth)) + 300
ypoints(i) = zoom * (y3 / (z3 + depth)) + 300
End Sub
Sub linecon2
' Draws single segments
For i=0 To 2
can.DrawLine(xpoints(i*2),ypoints(i*2),xpoints(i*2+1),ypoints(i*2+1),Colors.Red,1)
Next
End Sub
Sub linecon3
' Draws multiple segments
For n=0 To courbe_Max-2
can.DrawLine(xpoints(n),ypoints(n),xpoints(n+1),ypoints(n+1),Color_line,Taille)
Next
End Sub
Sub time_Tick
If Autom=True Then
can.DrawColor(Colors.Black)
Rot_X=Rot_X+1
Rot_Y=Rot_Y+1
Rot_Z=Rot_Z+1
plot_object(Rot_X,Rot_Y,Rot_Z)
EditText1.Text=X_Angle.Value
EditText2.Text=Y_Angle.Value
EditText3.Text=Z_Angle.Value
Activity.Invalidate
End If
End Sub
Sub courbe
courbe_Max=200
For k=0 To courbe_Max-1
x(k)=k-Floor(courbe_Max/2)
xcourbe(k)=SinD(5*x(k))
ycourbe(k)=CosD(5*x(k))
zcourbe(k)=x(k)/Floor(courbe_Max/2)
Next
End Sub
Sub vector(Carac As String)
' Font Vectors are generated on a 8X10 grid
' for example for letter "A"
' 1,1 is first point (x=1, y=1)
' 1,8 is second point (X=1, Y=8)
' 4,9 is thrid point (X=4, Y=9)
' and so on...
' Code is done to draw without lifting the pen...
Select Carac
Case "A"
AFont="A,1,1,1,8,4,9,7,8,7,1,7,5,1,5"
Case "B"
AFont="B,1,1,1,9,5,9,7,7,5,5,1,5,5,5,7,3,5,1,1,1"
Case "C"
AFont="C,7,1,1,1,1,9,7,9"
Case "D"
AFont="D,1,1,1,9,5,9,7,7,7,3,5,1,1,1"
Case "E"
AFont="E,7,1,1,1,1,5,5,5,1,5,1,9,7,9"
Case "F"
AFont="F,1,1,1,5,6,5,1,5,1,9,7,9"
Case "G"
AFont="G,4,5,7,5,7,1,7,1,1,1,1,9,7,9"
Case "U"
AFont="U,7,9,7,1,1,1,1,9"
Case "S"
AFont="S,7,9,1,9,1,5,7,5,7,1,1,1"
Case "L"
AFont="L,6,1,1,1,1,9"
Case "O"
AFont="O,7,1,1,1,1,9,7,9,7,1"
Case "I"
AFont="I,4,1,4,9"
Case "N"
AFont="N,1,1,1,9,7,1,7,9"
Case "3"
AFont="3,1,1,7,1,7,5,2,5,7,5,7,9,1,9"
Case " "
AFont=" ,1,1"
End Select
End Sub
Sub Z_Angle_ValueChanged (Value As Int, UserChanged As Boolean)
Update
End Sub
Sub Y_Angle_ValueChanged (Value As Int, UserChanged As Boolean)
Update
End Sub
Sub X_Angle_ValueChanged (Value As Int, UserChanged As Boolean)
Update
End Sub
Sub Update
can.DrawColor(Colors.Black)
Rot_X=X_Angle.Value
Rot_Y=Y_Angle.Value
Rot_Z=Z_Angle.Value
plot_object(Rot_X,Rot_Y,Rot_Z)
EditText1.Text=X_Angle.Value
EditText2.Text=Y_Angle.Value
EditText3.Text=Z_Angle.Value
Activity.Invalidate
End Sub
Sub Automatic_CheckedChange(Checked As Boolean)
If Checked=True Then
Autom=True
Else
Autom=False
End If
End Sub