编程绘制月亮的方法有多种,以下是几种使用不同编程语言和库实现月亮绘制的示例:
使用Python的turtle库
```python
import turtle
创建画布
screen = turtle.Screen()
screen.bgcolor("black")
创建画笔
pen = turtle.Turtle()
pen.color("white")
pen.shape("turtle")
pen.width(3)
绘制月亮轮廓
pen.penup()
pen.goto(-100, 0)
pen.pendown()
pen.circle(50)
填充月亮的内部
pen.begin_fill()
pen.circle(50)
pen.end_fill()
隐藏画笔
pen.hideturtle()
结束绘制
turtle.done()
```
使用Python的matplotlib库
```python
import matplotlib.pyplot as plt
import numpy as np
设置画布大小
fig, ax = plt.subplots(figsize=(8, 8))
绘制月亮
circle = plt.Circle((0, 0), 0.5, color='white')
ax.add_artist(circle)
设置坐标轴范围
ax.set_xlim(-1.1, 1.1)
ax.set_ylim(-1.1, 1.1)
显示图形
plt.show()
```
使用Java的Swing库
```java
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Mid_autumn extends JFrame {
public static void main(String[] args) {
JFrame jf = new JFrame();
jf.setTitle("中秋节");
jf.setSize(1024, 768);
jf.setBackground(Color.BLACK);
jf.setLocationRelativeTo(null);
MyJPanel mj = new MyJPanel();
jf.add(mj);
jf.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
jf.setVisible(true);
}
}
class MyJPanel extends JPanel {
public MyJPanel() {
setPreferredSize(new Dimension(800, 600));
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.BLACK);
g2d.fillRect(0, 0, getWidth(), getHeight());
g2d.setColor(Color.YELLOW);
g2d.fillOval(100, 300, 200, 100);
g2d.setColor(Color.BLACK);
g2d.fillOval(150, 300, 200, 100);
}
}
```
使用C语言的OpenGL
```c
include
void display() {
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(1.0, 1.0, 1.0);
glutSolidSphere(0.5, 20, 20);
glColor3f(0.0, 0.0, 0.0);
glutSolidSphere(0.4, 20, 20);
glutSwapBuffers();
}
void initGL() {
glClearColor(0.0, 0.0, 0.0, 0.0);
glMatrixMode(GL_PROJECTION);
gluOrtho2D(-1.0, 1.0, -1.0, 1.0);
}
int main(int argc, char argv) {
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
glutInitWindowSize(800, 600);
glutInitWindowPosition(100, 100);
glutCreateWindow("Sun, Earth, and Moon");
initGL();
glutDisplayFunc(display);
glutMainLoop();
return 0;
}
```
使用3