• 技术文章 >后端开发 >Python教程

    求问怎样用python/python turtle画“心”呢?

    2016-06-06 16:24:18原创3965
    刚刚起步python,觉得python turtle真是萌神一般的存在呀~ 试着画了颗“心”,不过觉得方法实在是太笨了> < 想求问各位老小伙伴,还有什么方法可以python/python turtle画“心”呢?

    回复内容:

    from turtle import *
    def curvemove():
        for i in range(200):
            right(1)
            forward(1)
    color('red','pink')        
    begin_fill()
    left(140)
    forward(111.65)
    curvemove()
    left(120)
    curvemove()
    forward(111.65)
    end_fill()
    done()
    
    from turtle import *
    pensize(1)
    color('black','red')
    speed(2)
    up()
    goto(-12,100)
    down()
    begin_fill()
    left(90)
    circle(120,180)
    circle(360,70.529)
    left(38.942)
    circle(360,70.529)
    circle(120,180)
    end_fill()
    up()
    goto(-250,-150)
    down()
    from pylab import*
    t=linspace(0,2*pi,100)
    x=sin(2*t) + 2*sin(t)
    y=-cos(2*t)-2*cos(t)
    fill(x,y,'r')
    show()
    
    心没画,玫瑰花倒是有一个之前忘了帖代码,现在补上。
    for i in range (1):
    def paint(ang,r,ang2): #画图函数
    turtle.penup()
    turtle.setheading(ang)
    turtle.pendown()
    turtle.circle(r,ang2)


    import turtle
    turtle.speed(9)
    turtle.color("white") #设置
    turtle.pensize(7)
    turtle.penup()
    turtle.goto(50,-50)
    turtle.pendown()
    turtle.dot(200,"pink") #画背景
    turtle.penup()
    turtle.goto(50,86.6)
    ang=-150
    r=300
    ang2=46
    for j in range (21): #循环

    paint(ang,r,ang2) #画弧
    ang2-=25
    paint(ang+ang2+25,r,-ang2) #回退
    ang2+=25
    ang+=66
    r=r*0.9


    思路大概就是:画圆弧,回退大约1/3,转向,减小半径,画圆弧......一直循环...

    视频传送门Python玫瑰花_生活

    提醒我贴代码的那个小同学,我看了你资料,看来你和我一个学校而且选的同一个选修课呢。不过我这个作业交过了,所以你参考一下,不懂的可以问我。
    照抄的话老师会打你屁屁的
    (╯‵□′)╯︵┻━┻。35道哪个不会可以私信我。我给你思路~不过最近考试周,我不一定都能帮得上忙。。。
    --------------15.12.31-----------------
    import sys
    import math 
    
    def frange(start, end, step=1.0):
    	if step > 0:
    		while start < end:
    			yield start 
    			start += step
    	elif step < 0:
    		while start > end:
    			yield start 
    			start += step
    	else:
    		raise ValueError('range() step must not be zero')
    
    def f(x, y, z):
    	a = x*x + 9.0/4*y*y + z*z - 1 
    	return a*a*a - x*x*z*z*z - 9.0/80*y*y*z*z*z 
    
    def h(x, z):
    	for y in frange(1.0, 0.0, -0.001):
    		if f(x, y, z) <= 0:
    			return y 
    	return 0.0
    
    if __name__ == '__main__':
    	for z in frange(1.5, -1.5, -0.1):
    		for x in frange(-1.5, 1.5, 0.05):
    			v = f(x, 0, z)
    			if v <= 0:
    				y0 = h(x, z)
    				ny = 0.01
    				nx = h(x + ny, z) - y0 
                                    nz = h(x, z + ny) - y0 
    				nd = 1.0/math.sqrt(nx*nx+ny*ny+nz*nz)
    				d = (nx + ny - nz)*nd*0.5 + 0.5
    				sys.stdout.write('.:-=+*#%@'[int(d*5)])
    			else:
    				sys.stdout.write(' ')
    		sys.stdout.write('\n')		
    
    用python matplotlib画笛卡尔的心形线
    import numpy as np
    import pylab as plt
    from matplotlib import colors

    a = [[] for i in range(1000)]

    i = 0
    while i < 1000:
    j = 0
    while j < 1000:
    x = -1.8 + 0.003*i
    y = -1.4 + 0.0028*j
    z = y**2 + (-x - (y**2)**0.33333)**2
    if z <= 1:
    a[i].append(0.9)
    else:
    a[i].append(0.0)
    j = j + 1
    i = i + 1

    cmap = colors.ListedColormap(['white', 'pink'])

    im = plt.imshow(a, cmap = cmap, interpolation="bicubic" )
    plt.show()
    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    专题推荐:python turtle lt gt
    VIP课程(WEB全栈开发)

    相关文章推荐

    • 【腾讯云】年中优惠,「专享618元」优惠券!• 一文详解python生成器• Python自动化实践之筛选简历• 图文详解Python冒泡排序算法• Python 3.11中的最佳新功能和功能修复• Python接口自动化测试必备基础之http协议详解
    1/1

    PHP中文网