B4A Library [B4X] ? B4XTurtle - Library for teachers and parents

Library is available here:

1587567435075-png.92383
 
Last edited:

amidgeha

Active Member
Licensed User
Longtime User
Draw a star on each of the five vertices of a star
Recursive draw of stars:
Sub DrawStar(s As Double, m As Double)
    If s<m Then Return
    For i = 1 To 5
        Turtle.SetPenColor(Rnd(xui.Color_Black, xui.Color_White))
        Turtle.MoveForward(s)
        DrawStar(s*0.3,10)
        Turtle.TurnLeft(144)
    Next
End Sub
 

aeric

Expert
Licensed User
Longtime User

AnandGupta

Expert
Licensed User
Longtime User
I still prefer typing out the syntax.
That's the difference between geek and users.
I also use text editor to complete my code in B4A faster than the ide due to in built macro and script.

A student is a user at best and code looks like class work but animation and images like playground.
Remember we loved linux but our users preferred windows.

So my suggestion was on that thinking for the students ; of showing the codes from the image template made.
Anyway Erel is best to decide the future course of turtle, it is just the beginning now.

Regards,

Anand
 

LucaMs

Expert
Licensed User
Longtime User
I would add some methods, such as GetPenSize and others Gets (which I don't remember now), corresponding to the relative Sets.

Also, I thought that the GetX and GetY methods returned the current coordinates, but that's not the case (I think they return the initial location).
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
U8M73YDeMb.gif


 

Johan Schoeman

Expert
Licensed User
Longtime User
Drawing an ellipse:

ellipse.png


B4X:
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 600
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private turtle As B4XTurtle
    Dim a As Int                   'semi-major axis
    Dim b As Int                   'semi-minor axis
    Dim xui As XUI
    
    Dim xval, yval As Float
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("main") 'Load the layout file.
    MainForm.Show
    
    Dim zeroX As Float = turtle.Width/2
    Dim zeroY As Float = turtle.Height/2
    
    a = 200                       'semi-major axis
    b = 75                          'semi-minor axis
    
    turtle.SetPenColor(xui.Color_Red)
    
    turtle.SetX(zeroX + (a * Cos(0*2*cPI/360)))
    turtle.SetY(zeroY + (b * Sin(0*2*cPI/360)))
    
    For i = 0 To 360
        xval = zeroX + (a * Cos(i*2*cPI/360))
        yval = zeroY + (b * Sin(i*2*cPI/360))
        turtle.MoveTo(xval, yval)
    Next
    
    turtle.SetPenColor(xui.Color_Blue)
    
    a = a + 50
    b = b + 12
    
    turtle.SetX(zeroX + (a * Cos(0*2*cPI/360)))
    turtle.SetY(zeroY + (b * Sin(0*2*cPI/360)))
    
    For i = 0 To 360
        xval = zeroX + (a * Cos(i*2*cPI/360))
        yval = zeroY + (b * Sin(i*2*cPI/360))
        turtle.MoveTo(xval, yval)
    Next
    
    a = 250
    b = -250            'negative value = change direction of drawing
    
    turtle.SetPenColor(xui.Color_Green)
    
    turtle.SetX(zeroX + (a * Cos(0*2*cPI/360)))
    turtle.SetY(zeroY + (b * Sin(0*2*cPI/360)))
    
    For i = 0 To 360
        xval = zeroX + (a * Cos(i*2*cPI/360))
        yval = zeroY + (b * Sin(i*2*cPI/360))
        turtle.MoveTo(xval, yval)
    Next
    
    a = 125
    b = 250            'negative value = change direction of drawing
    
    turtle.SetPenColor(xui.Color_Magenta)
    
    turtle.SetX(zeroX + (a * Cos(0*2*cPI/360)))
    turtle.SetY(zeroY + (b * Sin(0*2*cPI/360)))
    
    For i = 0 To 360
        xval = zeroX + (a * Cos(i*2*cPI/360))
        yval = zeroY + (b * Sin(i*2*cPI/360))
        turtle.MoveTo(xval, yval)
    Next
    
    
    a = 75
    b = 250            'negative value = change direction of drawing
    
    turtle.SetPenColor(xui.Color_Cyan)
    
    turtle.SetX(zeroX + (a * Cos(0*2*cPI/360)))
    turtle.SetY(zeroY + (b * Sin(0*2*cPI/360)))
    
    For i = 0 To 360
        xval = zeroX + (a * Cos(i*2*cPI/360))
        yval = zeroY + (b * Sin(i*2*cPI/360))
        turtle.MoveTo(xval, yval)
    Next
    
    turtle.SetTurtleVisible(False)

End Sub

'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

a = semi-major axis
b = semi-minor axis

If a> b then ellipse major axis is in the x direction (longer in x direction)
if a < b then ellipse major axis is in the y direction (longer in y direction)
if a = b then it will be a circle
 

Attachments

  • b4jTurtleEllipse.zip
    2.4 KB · Views: 323
Top