C语言和Python实现万花筒图形绘制
用C语言和Python分别画出一个万花筒
本文将使用C语言和Python两种编程语言分别实现一个简单的万花筒图形绘制,并附带完整代码示例,方便读者学习和参考。
C语言代码
#include <graphics.h>
#include <math.h>
#define PI 3.1415926535
int main() {
initgraph(640, 480); // 初始化图形窗口
setbkcolor(WHITE); // 设置背景颜色为白色
int centerX = 320, centerY = 240; // 中心点坐标
int radius = 150; // 半径
for (int i = 0; i < 360; i += 10) { // 循环绘制不同角度的圆形
int angle = i * PI / 180; // 角度转弧度
int x = centerX + radius * cos(angle); // 计算圆心坐标
int y = centerY + radius * sin(angle);
setcolor(i % 16 + 1); // 设置不同的颜色
circle(x, y, radius - i / 10 * 10); // 绘制圆形
}
getch(); // 等待用户按键
closegraph(); // 关闭图形窗口
return 0;
}
Python代码
import turtle
import math
radius = 150 # 半径
centerX, centerY = 0, 0 # 中心点坐标
turtle.speed(0) # 设置画笔速度为最快
turtle.bgcolor('white') # 设置背景颜色为白色
for i in range(0, 360, 10): # 循环绘制不同角度的圆形
angle = i * math.pi / 180 # 角度转弧度
x = centerX + radius * math.cos(angle) # 计算圆心坐标
y = centerY + radius * math.sin(angle)
turtle.penup() # 抬起画笔
turtle.goto(x, y) # 移动到圆心坐标
turtle.pendown() # 落下画笔
turtle.pencolor(i % 16 + 1) # 设置不同的颜色
turtle.circle(radius - i / 10 * 10) # 绘制圆形
turtle.done() # 等待用户关闭窗口
原文地址: https://www.cveoy.top/t/topic/mIWe 著作权归作者所有。请勿转载和采集!