This is the third in a series that draws a heart. This time, the Python code of the second Drawing a Heart with Python Part 2 (SymPy Edition) is used as PyCall. I tried porting it to Ruby code using a Gem called / mrkn / pycall).
Please note that PyCall has installed the latest version 0.1.0.alpha.20170419'
at this time (2017/04/24 23:30).
draw_heart.py
from sympy.plotting import plot_parametric
from sympy import Symbol, cos, sin
def draw_heart():
t = Symbol('t')
x = 16 * sin(t)**3
y = 13 * cos(t) - 5 * cos(2 * t) - 2 * cos(3 * t) - cos(4 * t)
plot_parametric(x, y,
autoscale=True, title='heart', line_color='pink')
if __name__ == '__main__':
try:
draw_heart()
except KeyboardInterrupt:
pass
draw_heart.rb
require 'pycall/import'
include PyCall::Import
pyimport 'sympy'
pyfrom 'sympy', import: %i[cos sin]
pyfrom 'sympy.plotting', import: :plot_parametric
def draw_heart
t = sympy.Symbol.('t')
x = 16 * sin.(t)**3
y = 13 * cos.(t) - 5 * cos.(2 * t) - 2 * cos.(3 * t) - cos.(4 * t)
plot_parametric.(x, y, autoscale: true, title: 'heart', line_color: 'pink')
end
draw_heart
It worked: heart_eyes:
There is no documentation on how to use it in the official repository README.md, so I wrote it with reference to Code examples. It moved unexpectedly smoothly! It's strange that Python code works almost exactly in Ruby: sparkles:
Recommended Posts