程序怎么写一个圆形

时间:2025-01-24 19:03:18 单机游戏

1. Java

使用Java的Swing和AWT库来绘制圆形:

```java

import javax.swing.*;

import java.awt.*;

public class CircleDrawer extends JPanel {

public void paintComponent(Graphics g) {

super.paintComponent(g);

int width = getWidth();

int height = getHeight();

int radius = Math.min(width, height) / 2;

int x = (width - radius) / 2;

int y = (height - radius) / 2;

g.drawOval(x, y, radius, radius);

}

public static void main(String[] args) {

JFrame frame = new JFrame("Circle Drawer");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.add(new CircleDrawer());

frame.setSize(400, 400);

frame.setVisible(true);

}

}

```

2. Python (使用turtle库)

使用turtle库绘制圆形:

```python

import turtle

def draw_circle(radius):

turtle.circle(radius)

turtle.done()

radius = int(input("请输入圆的半径:"))

draw_circle(radius)

```

3. Python (使用matplotlib库)

使用matplotlib库绘制圆形:

```python

import matplotlib.pyplot as plt

import numpy as np

def draw_circle(radius):

theta = np.linspace(0, 2 * np.pi, 100)

x = radius * np.cos(theta)

y = radius * np.sin(theta)

plt.plot(x, y, '-o')

plt.axis('equal')

plt.show()

radius = 5

draw_circle(radius)

```

4. Python (使用easyX库)

使用easyX库绘制圆形:

```cpp

include

int main() {

initgraph(640, 480); // 创建绘图窗口,大小为 640x480 像素

circle(200, 200, 50); // 画圆心在(200, 200),半径为50的圆

getch(); // 按任意键继续

closegraph(); // 关闭绘图窗口

return 0;

}

```

5. CNC加工中心的G代码

使用CNC加工中心的G代码绘制圆形:

```gcode

G90 ; 设置绝对坐标系

G54 ; 设置工作坐标系

T1 M06 ; 选择刀具

S5000 M03 ; 启动主轴

G00 X20 Y20 ; 移动到起点

G02 X10 Y20 I0 J10 ; 绘制圆形

G00 X0 Y0 ; 回到原点

M05 ; 停止主轴

M30 ; 程序结束

```

这些示例展示了如何在不同的编程环境中绘制圆形。你可以根据自己的需求和使用的工具选择合适的代码示例。