Python 使用 turtle 模块
```python
import turtle
def draw_hexagon(color: str, size: int):
turtle.color(color)
turtle.begin_fill()
for i in range(6):
turtle.forward(size)
turtle.right(60)
turtle.end_fill()
调用函数绘制六边形
draw_hexagon("red", 200)
turtle.done()
```
Python 使用 matplotlib 库
```python
import matplotlib.pyplot as plt
from matplotlib.patches import RegularPolygon
def draw_hexagon(ax, x, y, radius):
hexagon = RegularPolygon((x, y), numVertices=6, radius=radius)
ax.add_patch(hexagon)
创建画布和坐标轴
fig, ax = plt.subplots()
绘制六边形
draw_hexagon(ax, 0, 0, 200)
显示图形
plt.show()
```
Java 使用 Swing
```java
import javax.swing.JPanel;
import java.awt.Graphics;
import java.awt.Polygon;
public class PolygonsPanel extends JPanel {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int width = getWidth();
int height = getHeight();
int radius = 100;
int x = width / 2;
int y = height / 2;
Polygon hexagon = new Polygon();
hexagon.addPoint(x + radius, y);
for (int i = 1; i <= 6; i++) {
hexagon.addPoint(x + radius * Math.cos(Math.toRadians(60 * i)), y + radius * Math.sin(Math.toRadians(60 * i)));
}
g.setColor(Color.RED);
g.fillPolygon(hexagon);
}
}
```
C++ 使用 SFML
```cpp
include
int main() {
sf::RenderWindow window(sf::VideoMode(800, 600), "Hexagon");
sf::VertexArray hexagon(sf::Points, 6);
hexagon = sf::Vector2f(400, 300);
for (int i = 1; i < 6; ++i) {
hexagon[i] = sf::Vector2f(400 + 100 * cos(i * M_PI / 3), 300 + 100 * sin(i * M_PI / 3));
}
sf::CircleShape shape(100);
shape.setFillColor(sf::Color::Red);
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
window.draw(hexagon);
window.draw(shape);
window.display();
}
return 0;
}
```
这些示例展示了如何使用不同的编程语言和库来绘制六边形。你可以根据自己的需求和熟悉程度选择合适的编程语言和工具来实现六边形的绘制。