试用带有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度),然后指定应绘制多长时间。
所以
- left(30) # rotates the pen 30 degrees
- forward(100) # draws it for 100 pixels (i guess) forward
让我们修改我们的原始程序以执行此操作。第一件事是松散所有TKINTE例程。下一步是介绍似乎是Python的徽标接口。下一步将是将起始位置设置为0,0,即屏幕的中间...(这是徽标组织屏幕的方式)。
现在,让我们做一些示例计算以查看所有操作如何完成:
开始坐标为0,0。第一行的端坐标为:174.147,274.455。这是一条正确的行,所以让我们弄清楚它的长度,可以按照以下方式完成:
- math.sqrt( (174.147 - 0)**2 + (274.455 - 0)**2 )
现在,让我们找出该线和水平轴之间的角度。为此,我们可以使用Python中数学库的一部分的AtAN2函数。因此,线和水平轴之间的角度是:
- math.atan2(275.455-0,174.147)
请注意,我们通过第一个坐标减去它,因此我们将行转换为0,0。
atan2返回弧度的角度,但是由于徽标使用度,我们必须转换为学位。这是通过以下方式完成的:
- (180.0 / math.pi)*rad
以下是用于执行此操作的Python代码,即使我们将速度设置为最快,也有点慢。
- 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
而已!保存它,并将其运行在您的本地Python解释器中,或将代码粘贴到雕刻中。
附件图像
海龟
(5.0 kb,4198视图)
评论已关闭