Python
使用turtle库
```python
import turtle
设置画布大小
turtle.setup(600, 400)
设置画笔颜色和粗细
turtle.color("red", "pink")
turtle.pensize(3)
绘制左侧半个心形
turtle.begin_fill()
turtle.left(45)
turtle.forward(100)
turtle.circle(50, 180)
turtle.right(90)
turtle.circle(50, 180)
turtle.forward(100)
turtle.end_fill()
绘制右侧半个心形
turtle.penup()
turtle.goto(0, 0)
turtle.pendown()
turtle.begin_fill()
turtle.right(135)
turtle.forward(100)
turtle.circle(-50, 180)
turtle.left(90)
turtle.circle(-50, 180)
turtle.forward(100)
turtle.end_fill()
隐藏画笔
turtle.hideturtle()
显示绘制结果
turtle.done()
```
使用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语言 使用嵌套循环打印心形
```c
include
int main() {
int i, j;
int n = 6; // 心形的大小,可以根据需要调整
// 上半部分心形图案
for (i = n / 2; i >= 1; i -= 2) {
for (j = 1; j <= n - i; j++) {
printf(" ");
}
for (j = 1; j <= 2 * i - 1; j++) {
printf("*");
}
printf("\n");
}
// 下半部分心形图案
for (i = n / 2; i <= n; i++) {
for (j = 1; j <= n - i; j++) {
printf(" ");
}
for (j = 1; j <= 2 * i - 1; j++) {
printf("*");
}
printf("\n");
}
return 0;
}
```
使用ASCII字符绘制心形
```c
include
int main() {
int i, j, n = 100; // 心形的大小
// 绘制心形的上半部分
for (i = 0; i < n; i++) {
int x = n - i;
int y = i * 2 - n + 1;
int char_code = (x * 3 + y) % 256;
putchar(char_code);
}
// 绘制心形的下半部分
for (i = n - 1; i >= 0; i--) {
int x = n - i;
int y = i * 2 - n + 1;
int char_code = (x * 3 + y) % 256;
putchar(char_code);
}
return 0;
}
```
总结