试用带有Logo的在线巨蟒翻译器Sculpt

昨天,我检查了雕塑(
http://www.skulpt.org/
)原来是写给JavaScript的在线python解释器。还有其他示例,但是雕刻的方式是,它使程序员通过Pythons徽标接口使程序员访问HTML5画布。
Python真的有徽标界面吗?上次我触摸这种编程语言是在大约25年前的中学!
几年前,我创建了这个:
http://bytes.com/topic/python/answer...ng-tkinter-run
用Tkinter绘制有趣的形状。现在,我想修改此程序,以便使用Pythons徽标接口绘制相同的形状,并可以直接粘贴到雕刻中。

首先,与TKINTER不同,您无法绘制具有起始位置和终端位置的线(例如(x1,y1,x2,y2))。在徽标中,您指定起始方向(从0到360度),然后指定应绘制多长时间。
所以

选择 | 换行 | 行号
  1. left(30) # rotates the pen 30 degrees
  2. forward(100) # draws it for 100 pixels (i guess) forward
  3.  

让我们修改我们的原始程序以执行此操作。第一件事是松散所有TKINTE例程。下一步是介绍似乎是Python的徽标接口。下一步将是将起始位置设置为0,0,即屏幕的中间...(这是徽标组织屏幕的方式)。
现在,让我们做一些示例计算以查看所有操作如何完成:
开始坐标为0,0。第一行的端坐标为:174.147,274.455。这是一条正确的行,所以让我们弄清楚它的长度,可以按照以下方式完成:

选择 | 换行 | 行号
  1. math.sqrt( (174.147 - 0)**2 + (274.455 - 0)**2 )
  2.  

现在,让我们找出该线和水平轴之间的角度。为此,我们可以使用Python中数学库的一部分的AtAN2函数。因此,线和水平轴之间的角度是:

选择 | 换行 | 行号
  1. math.atan2(275.455-0,174.147)
  2.  

请注意,我们通过第一个坐标减去它,因此我们将行转换为0,0。
atan2返回弧度的角度,但是由于徽标使用度,我们必须转换为学位。这是通过以下方式完成的:

选择 | 换行 | 行号
  1. (180.0 / math.pi)*rad
  2.  

以下是用于执行此操作的Python代码,即使我们将速度设置为最快,也有点慢。

选择 | 换行 | 行号
  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.  

而已!保存它,并将其运行在您的本地Python解释器中,或将代码粘贴到雕刻中。

附件图像

File Type: png

海龟

(5.0 kb,4198视图)

标签: python

评论已关闭