473,396 Members | 2,090 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,396 developers and data experts.

Trying out sculpt, an online python interpreter with Logo

kudos
127 Expert 100+
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 :

http://bytes.com/topic/python/answer...ng-tkinter-run

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.



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

So

Expand|Select|Wrap|Line Numbers
  1. left(30) # rotates the pen 30 degrees
  2. forward(100) # draws it for 100 pixels (i guess) forward
  3.  
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:

Expand|Select|Wrap|Line Numbers
  1. math.sqrt( (174.147 - 0)**2 + (274.455 - 0)**2 )
  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:

Expand|Select|Wrap|Line Numbers
  1. math.atan2(275.455-0,174.147)
  2.  
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:

Expand|Select|Wrap|Line Numbers
  1. (180.0 / math.pi)*rad
  2.  
Below is the python code for doing this, it's kind of slow, even when we set the speed to the fastest.

Expand|Select|Wrap|Line Numbers
  1. import math 
  2. import turtle # this seems to be Logo in python
  3.  
  4. t = turtle.Turtle() # create an istance of it
  5. t.speed(0) # full speed
  6.  
  7. theta = 0.015
  8. sx = 0
  9. sy = 0
  10.  
  11. while(theta<4*3.1415):
  12.  xt = math.sin(theta * 10) * 270 + 300 
  13.  yt = math.cos(theta * 9.5) * 270 + 300 
  14.  nthet = xt / 30 + yt / 30 
  15.  yp = yt + math.sin(nthet) * 20
  16.  xp = xt + math.cos(nthet) * 20 
  17.  gx = math.sqrt( (sx/2 - xp/2)**2 + (sy/2 - yp/2)**2) # the distance of the line
  18.  tx = (xp/2.0) - (sx/2.0)
  19.  ty = (yp/2.0) - (sy/2.0)
  20.  cx = math.atan2(-ty,tx)*(180.0 / math.pi) # the angle between the line and the horizontal axis
  21.  
  22.  t.left(cx) # set the angle
  23.  t.forward(gx) # move forwared the appropriate amount
  24.  t.left(-1*cx) # reset the angle, so next time, we start off at scratch
  25.  sx = xp
  26.  sy = yp
  27.  theta+=0.004
  28.  
That's it! Save it, and run it in your local python interpreter or paste the code into sculpt.
Attached Images
File Type: png turtle.png (5.0 KB, 4315 views)
Jun 1 '13 #1
0 6303

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Rafal Kleger-Rudomin | last post by:
Hello, I'm looking for a command to reset interpreter's environment i.e. unload all modules, delete variables etc. Regards, Rafal
12
by: Anon | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello all I am a beginner teaching myself python, and I am enjoying it immensely :) As a language it is great, I real treat to study, I actually...
13
by: Rolf Magnus | last post by:
Hi, I would like to embed a python interpreter within a program, but since that program would be able to automatically download scripts from the internet, I'd like to run those in a restricted...
12
by: Rex Eastbourne | last post by:
Hi, I'm interested in running a Python interpreter in Emacs. I have Python extensions for Emacs, and my python menu lists "C-c !" as the command to run the interpreter. Yet when I run it I get...
3
by: g.franzkowiak | last post by:
Hi everybody, my interest is for the internals of the Python interpreter. I've used up to now FORTH for something and this indirect interpreter is very smart. --- ASM...
5
by: Russ | last post by:
I wrote a simple little function for exiting with an error message: def error ( message ): print_stack(); exit ("\nERROR: " + message + "\n") It works fine for executing as a script, but when...
3
by: morris.slutsky | last post by:
So every now and then I like to mess around with hobby projects - I often end up trying to write an OpenGL video game. My last attempt aborted due to the difficulty of automating game elements and...
0
by: has | last post by:
Hi all, need a little bit of advice on dynamically binding an embedded Python interpreter. First, the code for anyone that wants a look: ...
0
by: nadeemabdulhamid | last post by:
Hello, I'm trying to write some Java code that will launch a python interpreter shell and pipe input/output back and forth from the interpreter's i/o streams and the Java program. The code posted...
5
by: walterbyrd | last post by:
I don't know much php either, but running a php app seems straight forward enough. Python seems to always use some sort of development environment vs production environment scheme. For...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.