式を変えてランダムにハート型を描画するプログラムです
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import numpy as np | |
import random | |
import scipy as sp | |
sizex = 1000 | |
sizey = 1000 | |
en = np.zeros([sizex,sizey]) | |
han = np.zeros([sizex,sizey]) | |
for m in range(sizex): | |
for n in range(sizey): | |
seeds = random.randint(1,10000) | |
hsize = random.randint(10,70) | |
if seeds == 1: | |
en += np.fromfunction(lambda x,y: (x-m)**2+((y-n)-(abs(x-m))**0.95)**2 <hsize**2, (sizex,sizey)) | |
for m in range(sizex): | |
for n in range(sizey): | |
han[m][n]=en[n][sizex-1-m] | |
sp.misc.imsave("testing.png",han) |
変えたところを主に紹介いたします
hsize = random.randint(10,70)
#ランダムでハートの大きさを変える設定です
en += np.fromfunction(lambda x,y: (x-m)**2+((y-n)-(abs(x-m))**0.95)**2 <hsize**2, (sizex,sizey))
#複雑な式になってしまいました。順に説明します
円の基本式は x^2+y^2=r^2
円の内側を描画するために x^2+y^2 < r^2
ハートの式 x^2+(y-|x|^0.95)^2<r^2
#0.95を小さくすると円に近づきます
座標を入れると
(x-m)^2+((y-n)-|x-m|^0.95)^2 < r^2
実はこのままだとハートを横倒しにした形❥になるので、座標変換を行いました
単純に
en.transpose()を使うと逆ハート型になってしまいましたので、自力で反転しました
for m in range(sizex):
for n in range(sizey):
han[m][n]=en[n][sizex-1-m]
重なるマークは省くとか考えたらいろんな画像を作れちゃいそうですね
そのうちブログの背景とかサムネがpythonでプログラムした画像になっているかもしれませんねw
<<< ランダムに円を描画する【Python】
0 件のコメント:
コメントを投稿