Trying out sculpt, an online python interpreter with Logo

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kudos
    Recognized Expert New Member
    • Jul 2006
    • 127

    Trying out sculpt, an online python interpreter with Logo

    Yesterday, I checked out sculpt (http://www.skulpt.org/) which turned out to be an online python interpreter written (or compiled?) to JavaScript. There are other examples, but the way sculpt differs, is that it gives the programmer access to a html5 canvas throught pythons Logo interface.

    Does python really have a logo interface? Last time I touched this programming language was in middle school almost 25 years ago!

    Some years ago, I create this :



    where a funny shape was drawn with Tkinter. Now I want to modify this program so it draws the same shape with pythons Logo interface and could be pasted directly into sculpt.

    [IMGnothumb]http://bytes.com/attachments/attachment/7032d1370123765/turtle.png[/IMGnothumb]

    First of all, unlike Tkinter, you cannot draw a line with a start position and an end position (like line(x1,y1,x2,y 2)). In Logo you specify the start direction (from 0 to 360 degrees), then you specify how long it should be drawn.

    So

    Code:
    left(30) # rotates the pen 30 degrees
    forward(100) # draws it for 100 pixels (i guess) forward
    Let's modify our original program to do just this. First thing would be to loose all Tkinter routines. The next step would be to introduce turtle which seems to be Python's logo interface. The next step would be to set the start position to 0,0 which is the middle of the screen...(that is how Logo organizes the screen).

    Now, let's do some example calculations to see how it's all done:

    The start coordinates is 0,0. The end coordinates of the first line is : 174.147, 274.455. This is a line right, so let's figure out the length of it, which can be done the following way:

    Code:
    math.sqrt( (174.147 - 0)**2 + (274.455 - 0)**2 )
    Now let's figure out the angle between this line and the horizontal axis. To do this, we can use the atan2 function which is part of the math library in python. So the angle between the line and horizontal axis would be:

    Code:
    math.atan2(275.455-0,174.147)
    Notice that we subtract it by the first coordinate, so that we translate the line so it starts at 0,0.

    atan2 return the angle in radians, but since Logo is using degrees, we have to convert to degrees. This is done by the following way:

    Code:
    (180.0 / math.pi)*rad
    Below is the python code for doing this, it's kind of slow, even when we set the speed to the fastest.

    Code:
    import math 
    import turtle # this seems to be Logo in python
    
    t = turtle.Turtle() # create an istance of it
    t.speed(0) # full speed
    
    theta = 0.015
    sx = 0
    sy = 0
    
    while(theta<4*3.1415):
     xt = math.sin(theta * 10) * 270 + 300 
     yt = math.cos(theta * 9.5) * 270 + 300 
     nthet = xt / 30 + yt / 30 
     yp = yt + math.sin(nthet) * 20
     xp = xt + math.cos(nthet) * 20 
     gx = math.sqrt( (sx/2 - xp/2)**2 + (sy/2 - yp/2)**2) # the distance of the line
     tx = (xp/2.0) - (sx/2.0)
     ty = (yp/2.0) - (sy/2.0)
     cx = math.atan2(-ty,tx)*(180.0 / math.pi) # the angle between the line and the horizontal axis
     
     t.left(cx) # set the angle
     t.forward(gx) # move forwared the appropriate amount
     t.left(-1*cx) # reset the angle, so next time, we start off at scratch
     sx = xp
     sy = yp
     theta+=0.004
    That's it! Save it, and run it in your local python interpreter or paste the code into sculpt.
    Attached Files
    Last edited by Niheel; Aug 23 '13, 12:40 PM. Reason: Fixed minor grammar and spelling.
Working...