#Region Project Attributes
#MainFormWidth: 1000
#MainFormHeight: 650
#End Region
#Region Form Preperation
'Template version 1.00
Sub Process_Globals
Private fx As JFX
Private MainForm As Form
Private xui As XUI 'ignore
Private Turtle As B4XTurtle
Dim zerox, zeroy, offset As Float
Private Spinner1 As Spinner
End Sub
Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.RootPane.LoadLayout("Main")
MainForm.Show
Spinner1.SetNumericItems(1, 150, 1, 1)
End Sub
#End Region
'B4XTurtle examples: https://www.b4x.com/android/forum/threads/examples-for-teachers-and-parents.116979
Sub Turtle_Start
turtle_prep
End Sub
Private Sub btnDraw_Click
Dim val As Int = Spinner1.Value
Dim x As Float = 0
Dim y As Float = 0
Turtle.SetPenColor(xui.Color_ARGB(255, Rnd(0, 255), Rnd(0, 255), Rnd(0, 255)))
For i = -720 To 720
x = offset + (2 * zerox / (2 * cPI)) * ((i+720)/1440) * 2 * cPI 'add the offset to the calculated x position
y=0
For n = 1 To val 'Fourier series for a square wave
y = y + (Sin((2*n-1) * i * 2 * cPI/360)/(2*n-1))
Next
y = offset + (y * -1 * zeroy) + zeroy
If i = -720 Then
Turtle.SetX(x)
Turtle.SetY(y)
End If
Turtle.MoveTo(x,y)
Next
End Sub
Private Sub btnClear_Click
Turtle.SetPenColor(xui.Color_Black)
Turtle.ClearScreen
turtle_prep
End Sub
Sub turtle_prep
offset = 25 'leave a gap of 25 at the top and bottom as well as on the left and right of the X and Y axis
zerox = (Turtle.Width - 2 * offset ) / 2
zeroy = (Turtle.Height - 2 * offset) / 2
Turtle.SetSpeedFactor(10)
Turtle.mBase.Color = xui.Color_LightGray 'set the Turtle background color
Turtle.PenUp
Turtle.SetX(zerox + offset)
Turtle.SetY(0 + offset)
Turtle.PenDown
Turtle.MoveTo(zerox + offset, 2 * zeroy + offset) 'Draw the y-axis
Turtle.SetX(0 + offset)
Turtle.SetY(zeroy + offset)
Turtle.MoveTo(2 * zerox + offset , zeroy + offset) 'Draw the x-axis
End Sub