Last active
February 23, 2019 01:51
-
-
Save aessam/48d56f4422b36fecd5cd41639e6ecf8c to your computer and use it in GitHub Desktop.
Drawing heart with math
This file contains 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
# http://mathworld.wolfram.com/HeartCurve.html | |
from math import pi, sin, cos | |
import time | |
import numpy as np | |
r = 1 | |
result = [] | |
for i in range(38): | |
result.append([]) | |
result[i] = list(map(lambda x: ' ', range(38))) | |
calc_x = lambda a: r * 16 * pow(sin(a), 3) | |
calc_y = lambda a: -r * (13 * cos(a) - 5 * cos(2 * a) - 2 * cos(3 * a) - cos(4 * a)) | |
calc_d = lambda a: (calc_x(a), calc_y(a)) | |
for a in np.arange(0.0, pi, .1): | |
x, y = calc_d(a) | |
result[int(16 + y)][int(20 + x)] = "O" | |
x, y = calc_d(pi*2 - a) | |
result[int(16 + y)][int(20 + x)] = "O" | |
time.sleep(.05) | |
print(chr(27) + "[2J") | |
for i in result: | |
print() | |
for x in i: | |
print(x, end='') | |
print("") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment