C++用类实现角度画圆和中点画圆
时间: 2024-06-13 16:09:05 浏览: 92
以下是C++用类实现角度画圆和中点画圆的示例代码:
```c++
#include <graphics.h>
#include <math.h>
class Circle {
private:
int x, y, r;
public:
Circle(int x, int y, int r) {
this->x = x;
this->y = y;
this->r = r;
}
void drawMidPointCircle() {
int x0 = 0, y0 = r;
int d = 1 - r;
while (x0 <= y0) {
putpixel(x + x0, y + y0, WHITE);
putpixel(x + y0, y + x0, WHITE);
putpixel(x + y0, y - x0, WHITE);
putpixel(x + x0, y - y0, WHITE);
putpixel(x - x0, y - y0, WHITE);
putpixel(x - y0, y - x0, WHITE);
putpixel(x - y0, y + x0, WHITE);
putpixel(x - x0, y + y0, WHITE);
if (d < 0) {
d += 2 * x0 + 3;
} else {
d += 2 * (x0 - y0) + 5;
y0--;
}
x0++;
}
}
void drawAngleCircle(int angle) {
int x0 = 0, y0 = r;
int d = 1 - r;
while (x0 <= y0) {
double rad = atan2(y0, x0);
int deg = (int)(rad * 180 / M_PI);
if (deg % angle == 0) {
putpixel(x + x0, y + y0, WHITE);
putpixel(x + y0, y + x0, WHITE);
putpixel(x + y0, y - x0, WHITE);
putpixel(x + x0, y - y0, WHITE);
putpixel(x - x0, y - y0, WHITE);
putpixel(x - y0, y - x0, WHITE);
putpixel(x - y0, y + x0, WHITE);
putpixel(x - x0, y + y0, WHITE);
}
if (d < 0) {
d += 2 * x0 + 3;
} else {
d += 2 * (x0 - y0) + 5;
y0--;
}
x0++;
}
}
};
int main() {
initgraph(640, 480);
Circle c1(200, 200, 100);
c1.drawMidPointCircle();
Circle c2(400, 200, 100);
c2.drawAngleCircle(30);
getch();
closegraph();
return 0;
}
```
其中,`Circle`类表示一个圆,包含圆心坐标和半径。`drawMidPointCircle`方法使用中点画圆算法绘制圆,`drawAngleCircle`方法使用角度画圆算法绘制圆,其中`angle`参数表示每隔多少度绘制一个点。在`main`函数中,我们创建了两个圆对象,一个使用中点画圆算法绘制,一个使用角度画圆算法绘制。
阅读全文