绘制椭圆的程序可以根据不同的编程语言和绘图库来实现。以下是几种不同编程语言绘制椭圆的示例代码:
使用OpenGL(C语言)
```c
include
void setpixel(int x, int y) {
glBegin(GL_POINTS);
glVertex2i(x, y);
glEnd();
}
void init(void) {
glClearColor(1.0, 1.0, 1.0, 0.0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0, 200.0, 0.0, 150.0);
}
inline int round(float a) {
return (int)(a + 0.5);
}
void ellipseplotpoints(int xcenter, int ycenter, int rx, int ry) {
int px = 0, py = ry;
int two_rx2 = 2 * rx * rx;
int two_ry2 = 2 * ry * ry;
int dx = 4 * rx * ry;
int dy = -2 * rx * ry * 2;
while (px < rx && py > -ry) {
setpixel(round(xcenter + rx), round(ycenter + py));
setpixel(round(xcenter + rx), round(ycenter - py));
setpixel(round(xcenter - rx), round(ycenter + py));
setpixel(round(xcenter - rx), round(ycenter - py));
px++;
py--;
if (px + py > 0) {
setpixel(round(xcenter + py), round(ycenter + rx));
setpixel(round(xcenter + py), round(ycenter - rx));
setpixel(round(xcenter - py), round(ycenter + rx));
setpixel(round(xcenter - py), round(ycenter - rx));
px--;
py++;
}
}
}
void draw_ellipse(int xcenter, int ycenter, int rx, int ry) {
ellipseplotpoints(xcenter, ycenter, rx, ry);
}
```
使用Python和Matplotlib
```python
import numpy as np
import matplotlib.pyplot as plt
def draw_ellipse(center_x, center_y, radius_x, radius_y):
theta = np.linspace(0, 2 * np.pi, 100)
x = center_x + radius_x * np.cos(theta)
y = center_y + radius_y * np.sin(theta)
plt.plot(x, y)
plt.axis('equal')
plt.title('Ellipse')
plt.xlabel('x-axis')
plt.ylabel('y-axis')
plt.grid(True)
plt.show()
示例调用
draw_ellipse(0, 0, 4, 2)
```