使用Python的turtle库
```python
import turtle
def draw_heart():
penup()
goto(0, -100)
pendown()
color('red')
begin_fill()
setheading(150)
circle(200, 90)
left(90)
circle(200, 90)
end_fill()
hideturtle()
draw_heart()
turtle.done()
```
使用Python的matplotlib库
```python
import numpy as np
import matplotlib.pyplot as plt
t = np.linspace(0, 2 * np.pi, 1000)
x = 16 * np.sin(t)3 y = 13 * np.cos(t) - 5 * np.cos(2*t) - 2 * np.cos(3*t) - np.cos(4*t) plt.figure(figsize=(8, 6)) plt.plot(x, y, color='red') plt.fill(x, y, color='red', alpha=0.6) plt.title('Python', fontsize=18) plt.axis('equal') plt.grid(True) plt.show() ``` 使用C语言的pygame库 ```python import pygame import math import random pygame.init() width = 800 height = 600 screen = pygame.display.set_mode((width, height)) pygame.display.set_caption('520 表白程序') RED = (255, 0, 0) PINK = (255, 192, 203) WHITE = (255, 255, 255) class Heart: def __init__(self): self.x = width // 2 self.y = height // 2 self.size = 1 self.growing = True def draw(self): for t in range(0, 628): t = t / 100 x = self.x + self.size * 16 * math.sin(t)
y = self.y - self.size * (13 * math.cos(t) - 5 * math.cos(2*t) - 2 * math.cos(3*t) - math.cos(4*t))
if self.growing:
self.size += 0.5
else:
self.size -= 0.5
if self.size <= 0:
self.growing = True
screen.fill(WHITE)
pygame.draw.circle(screen, RED, (self.x, self.y), self.size, 1)
pygame.display.flip()
heart = Heart()
running = True
while running:
for event in pygame.event.get():
if event.type == pygame.QUIT:
running = False
heart.draw()
pygame.quit()
```
这些代码示例分别使用Python的turtle库、matplotlib库和C语言的pygame库来绘制心形。你可以根据自己的需求和编程环境选择合适的方法。