试用Sculpt,带有徽标的在线python解释器

昨天,我检查了造型(

http://www.skulpt.org/ ),它原来是一个在线python解释器,是用JavaScript编写(或编译?)的。 还有其他示例,但是雕刻的方式有所不同,它使程序员可以通过python的Logo界面访问html5 canvas。

python真的有徽标界面吗? 我上一次接触这种编程语言是在25年前的中学里!

几年前,我创建了这个:

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

用Tkinter绘制了一个有趣的形状。 现在,我想修改此程序,使其与python Logo界面绘制相同的形状,并可以直接粘贴到造型中。

首先,与Tkinter不同,您不能绘制一条具有起始位置和结束位置的线(如line(x1,y1,x2,y2))。 在徽标中,指定起始方向(从0到360度),然后指定应绘制多长时间。

所以


left(30) # rotates the pen 30 degrees
forward(100) # draws it for 100 pixels (i guess) forward 
让我们修改我们的原始程序来做到这一点。 首先是要松开所有的Tkinter例程。 下一步将引入乌龟,它似乎是Python的徽标界面。 下一步是将开始位置设置为屏幕中间的0,0 ...(这是徽标组织屏幕的方式)。

现在,让我们做一些示例计算,看看它们是如何完成的:

起始坐标为0,0。 第一行的结束坐标为:174.147、274.455。 这是一条右行,因此让我们弄清楚它的长度,可以通过以下方式完成:


math.sqrt( (174.147 - 0)**2 + (274.455 - 0)**2 ) 
现在让我们找出这条线和水平轴之间的角度。 为此,我们可以使用atan2函数,该函数是python数学库的一部分。 因此,线与水平轴之间的角度为:

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解释器中运行它,或将代码粘贴到Sculpt中。
附加图片
文件类型:png turtle.png (5.0 KB,3837观看次数)

From: https://bytes.com/topic/python/insights/949750-trying-out-sculpt-online-python-interpreter-logo

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值