Make 3D heart with Happy Valentine's Day inside using python code
'''
===============================================================
3D heart shape with Happy Valentine's Day inside in matplotlib
===============================================================
Demonstrates how to plot a 3D function in cartesian coordinates.
Uses the marching cubes algorithm in scikit-image to obtain a isosurface.
'''
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import pyplot as plt
import numpy as np
from skimage import measure
# Set up mesh
n = 100
x = np.linspace(-3,3,n)
y = np.linspace(-3,3,n)
z = np.linspace(-3,3,n)
X, Y, Z = np.meshgrid(x, y, z)
# Create cardioid function
def f_heart(x,y,z):
F = 320 * ((-x**2 * z**3 -9*y**2 * z**3/80) +
(x**2 + 9*y**2/4 + z**2-1)**3)
return F
# Obtain value to at every point in mesh
vol = f_heart(X,Y,Z)
# Extract a 2D surface mesh from a 3D volume (F=0)
verts, faces, normals, values = measure.marching_cubes_lewiner(vol, 0, spacing=(0.1, 0.1, 0.1))
# Create a 3D figure
fig = plt.figure(figsize=(12,8))
ax = fig.add_subplot(111, projection='3d')
# Plot the surface
ax.plot_trisurf(verts[:, 0], verts[:,1], faces, verts[:, 2],
cmap='Spectral', lw=1)
# Change the angle of view and title
ax.view_init(15, -15)
# ax.set_title(u"Made with ❤ (and Python)", fontsize=15) # if you have Python 3
ax.set_title("Made with <3 (and Python)", fontsize=15)
# place a text box in upper left in axes coords
ax.text("Happy Valentines Day", 0.50, 0.50, textstr, transform=ax.transAxes, fontsize=15,
verticalalignment='centre')
# Show me some love ^^
plt.show()
'''
==================================================================
The commented code below creates a 2D Heart with valentines inside
==================================================================
# Python3 code to print happy valentine's
# day
import math
n = 10
# Initializing String to print in heart
message = " HappY Valentines DaY "
# Position from where from top
# message box would be placed.
print_message = 4
# Add space if message length is odd
if (len(message) % 2 != 0):
message += " "
# Outer loop to adjust length of upper
# part message is not handled in this part
for a in range(n):
# To print space and variable accordingly
for b in range(4 * n + 1):
# Computing distance to print variable
distance1 = math.sqrt(pow(a - n, 2) +
pow(b - n, 2))
distance2 = math.sqrt(pow(a - n, 2) +
pow(b - 3 * n, 2))
if (distance1 < n + 0.5 or
distance2 < n + 0.5):
print("S", end = "")
else:
print(" ", end = "")
# Ending line after each iteration
print()
# Printing the message part and lower
# part of heart. Outer loop handles
# depth of the heart.
for a in range(1, 2 * n):
# For getting the lower curve of heart
for b in range(a):
print(" ", end = "")
# Inner loop
# handles the message and spaces accordingly
for b in range(4 * n + 1 - 2 * a):
# Checks if the height is in range of
# message space
if (a >= print_message - 1 and
a <= print_message + 1):
point = b - (4 * n - 2 *
a - len(message)) // 2
# Prints message after leaving
# appropriate space
if (point < len(message) and point >= 0):
if (a == print_message):
print(message[point], end = "")
else:
print(" ", end = "")
else:
print("S", end = "")
else:
print("S", end = "")
print()
# This code is contributed by subhammahato348
'''
Comments
Leave a comment