標籤:

如何用python畫一朵花?

如題。


本王畫的彩虹花呦(????ω????)

快喊本寶寶大王就解鎖代碼呦寶寶破百贊(≧▽≦)/啦

from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.ticker import LinearLocator
import matplotlib.pyplot as plt
import numpy as np
# 很多寶寶要我注釋代碼,額其實這個很簡單的,主要是要有numpy和matplotlib
# 剩餘的就是數學公式了
fig = plt.figure()
ax = fig.gca(projection="3d")
# ax.set_aspect("equal")
# ax.set_axis_off()
[x, t] = np.meshgrid(np.array(range(25))/24.0, np.arange(0, 575.5, 0.5)/575*17*np.pi-2*np.pi)
p = (np.pi/2)*np.exp(-t/(8*np.pi))
u = 1-(1-np.mod(3.6*t, 2*np.pi)/np.pi)**4/2
y = 2*(x**2-x)**2*np.sin(p)
r = u*(x*np.sin(p)+y*np.cos(p))

surf = ax.plot_surface(r*np.cos(t), r*np.sin(t), u*(x*np.cos(p)-y*np.sin(p)), rstride=1, cstride=1, cmap=cm.gist_rainbow_r,
linewidth=0, antialiased=True)
plt.show()

_(?_?」∠)_我是寂寞的分割線

我發覺我放碼就是個錯誤,你們都拿完就走了,一群娃娃都拿去撩妹撩漢子,卻沒有一個人撩我

感動啊,F大居然看了我的彩虹花,在此感謝F大的花

福利時間到,恩,對,本寶寶就是這麼懶,不肯把它轉成python,下面是MATLAB的代碼

至於作用嘛,嘿嘿嘿,據說是世界上最(wo)完(xia)美(bian)的胸哦

[X, Y] = meshgrid(0.01:0.01:1, 0.01:0.01:1);
Zfun_yuru =@(x,y)12.5*x.*log10(x).*y.*(y-1)+exp(-((25 ...
*x - 25/exp(1)).^2+(25*y-25/2).^2).^3)./25;
Z = Zfun_yuru(X,Y);
figure;
surf(Y,Z,X,"FaceColor",[1 0.75 0.65],"linestyle","none");
hold on
surf(Y+0.98,Z,X,"FaceColor",[1 0.75 0.65],"linestyle","none");
axis equal;
view([116 30]);
camlight;
lighting phong; % 設置光照和光照模式


在《Think Python》中有一個畫花的例子(http://www.greenteapress.com/thinkpython/code/flower.py),它的效果是一筆一划給你花出三朵花,效果如下:

我精簡下代碼:

import math

try:
from swampy.TurtleWorld import * # 需要執行 pip install Swampy
except ImportError:
from TurtleWorld import *

def polyline(t, n, length, angle):
for i in range(n):
fd(t, length)
lt(t, angle)

def arc(t, r, angle):
arc_length = 2 * math.pi * r * abs(angle) / 360
n = int(arc_length / 4) + 1
step_length = arc_length / n
step_angle = float(angle) / n

# making a slight left turn before starting reduces
# the error caused by the linear approximation of the arc
lt(t, step_angle/2)
polyline(t, n, step_length, step_angle)
rt(t, step_angle/2)

def petal(t, r, angle):
for i in range(2):
arc(t, r, angle)
lt(t, 180-angle)

def flower(t, n, r, angle):
for i in range(n):
petal(t, r, angle)
lt(t, 360.0/n)

def move(t, length):
pu(t)
fd(t, length)
pd(t)

world = TurtleWorld()
bob = Turtle()
bob.delay = 0.01

move(bob, -100)
flower(bob, 7, 60.0, 60.0)

move(bob, 100)
flower(bob, 10, 40.0, 80.0)

move(bob, 100)
flower(bob, 20, 140.0, 20.0)

die(bob)

world.canvas.dump()

wait_for_user()


用python 畫一朵跟上面那樣明媚的太陽花:

#!/usr/bin/python

import Image
chars = " ...,,,DSAH2KJHSDIO 2USDFKJKLEJKCG^^$%#!*(!#JHASKHDFJASHQIWSNFJASCBJ "
fn = r"D:mycodePythonflower.png"
f1 = lambda F: "".join([(k % 100 != 0) and m or m+"
" for k, m in enumerate(apply(lambda x:[chars[x[j, i] % len(chars)] for i in xrange(80) for j in xrange(100)], (Image.open(F).resize((100, 80)).convert("L").load(),)), 1)])
f = open(r"D:mycodePythonflower.txt", "w")
f.write(f1(fn))
f.close()

效果截圖:

字元畫也是畫~!


來,太陽花

from turtle import *
shape("turtle")
color("red", "yellow")
begin_fill()
while True:
forward(200)
left(170)
if abs(pos()) &< 1: break end_fill() done()


肯定是用分形啦,先佔個坑,有時間了就來畫分形花

安利下這本書,可以參考下

https://book.douban.com/subject/3117569/#!/i!/ckDefault

誰知道手機上插鏈接怎麼變文字啊

(ノ`⊿′)ノ


借用的 @一隻聰明的傻瓜 的程序,自己稍作了修改。效果如下

需要程序的,去github地址下載


原本想畫一朵花的,最後咋感覺畫出了一個輪子???

import turtle

turtle.speed(10)

def draw(n):

for i in range(5):

turtle.forward(80)

turtle.left(n)

for i in range(1500):

draw(36*(i+1.0016))


我就默默的仰望你們這幫大神丟代碼,不說話........


本寶用Python畫
的花,會動的~~

http://chaos.ac.cn:8000/

源碼:

https://github.com/gamijin0/Beauty


推薦閱讀:

學Python web開發框架到什麼程度可以找到開發的工作?
print or plan and not print()()() 的疑問?
如何在mac版本的python里安裝pip?
新學python,編寫helloword.py提示無效語法。?
Youtube上有哪些好的python視頻教程?

TAG:Python |