编程几何图形怎么画的

时间:2025-01-27 03:46:22 网络游戏

编程绘制几何图形的方法取决于所使用的编程语言和库。以下是一些常见编程语言及其绘制几何图形的示例:

Python

使用turtle库

```python

import turtle

screen = turtle.Screen()

pen = turtle.Turtle()

pen.circle(100) 绘制半径为100的圆形

turtle.done()

```

使用matplotlib库

```python

import numpy as np

import matplotlib.pyplot as plt

def draw_circle(radius, center=(0, 0), num_points=1000):

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

x = center + radius * np.cos(theta)

y = center + radius * np.sin(theta)

plt.plot(x, y)

plt.axis('equal')

plt.grid(True)

plt.show()

draw_circle(radius=5)

```

C++

使用OpenCV库

```cpp

include

int main() {

cv::Mat img = cv::Mat::ones(240, 240, CV_8UC3);

cv::rectangle(img, cv::Rect(20, 20, 100, 100), cv::Scalar(0, 0, 255), 7);

cv::imshow("www", img);

cv::waitKey(0);

cv::destroyAllWindows();

return 0;

}

```

Java

使用Java AWT库

```java

import java.awt.*;

import java.awt.geom.*;

import javax.swing.*;

public class Main {

public static void main(String[] args) {

JFrame frame = new JFrame("Draw Geometry");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setSize(400, 400);

Graphics g = frame.getGraphics();

g.setColor(Color.RED);

g.fillRect(20, 20, 100, 100);

g.setColor(Color.BLUE);

g.drawOval(200, 200, 100, 50);

frame.setVisible(true);

}

}

```

JavaScript

使用HTML5 Canvas

```html

Your browser does not support the HTML5 canvas tag.