1. 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()
```
2. Python 使用 matplotlib
```python
import numpy as np
import matplotlib.pyplot as plt
def heart(x):
return np.sqrt(1 - (x2)(1/3)) * np.sign(x)
x = np.linspace(-1, 1, 1000)
y = heart(x)
plt.plot(x, y, color='red', label="上半部分")
plt.plot(x, -y, color='red', label="下半部分")
plt.title("浪漫程序员的爱心")
plt.legend()
plt.axis('equal')
plt.show()
```
3. C 语言
```c
include
int main() {
int i, j, n;
printf("请输入爱心的大小(范围:3-15):");
scanf("%d", &n);
if (n < 3 || n > 15) {
printf("请输入正确的大小范围!\n");
return 0;
}
for (i = n / 2; i <= n; i += 2) {
for (j = 1; j < n - i; j += 2) {
printf(" ");
}
for (j = 1; j <= (i * 2) - 1; j++) {
printf("*");
}
printf("\n");
}
return 0;
}
```
4. C 语言使用 ASCII 码
```c
include
int main() {
int i, j;
for (i = 0; i < 20; i++) {
for (j = 0; j < 2 * i + 1; j++) {
printf(" ");
}
for (j = 0; j < (i * 2) - 1; j++) {
printf("*");
}
printf("\n");
}
return 0;
}
```
5. C 语言使用 turtle 库
```c
include
int main() {
turtle_setup(800, 500);
turtle_color("pink", "pink");
turtle_pensize(3);
turtle_speed(1.5);
turtle_begin_fill();
turtle_up();
turtle_goto(0, -100);
turtle_down();
turtle_color("red");
turtle_circle(-112, 200);
turtle_left(120);
turtle_circle(-112, 200);
turtle_forward(224);
turtle_end_fill();
turtle_hideturtle();
turtle_done();
return 0;
}
```
这些代码示例分别使用 Python 的 turtle 库、matplotlib 库和 C 语言的 turtle 库来绘制爱心。你可以根据自己的需求和编程环境选择合适的代码进行尝试。