中点画圆算法
Here you will get program for midpoint circle algorithm in C and C++. It is an algorithm used in computer graphics for drawing circle.
在这里,您将获得C和C ++中的中点圆算法的程序。 它是计算机图形学中用于绘制圆的算法。
This program will work in Turbo C or Turbo C++ compiler as it uses graphics.h header file.
该程序将使用graphics.h头文件,因此可在Turbo C或Turbo C ++编译器中运行。
Change the path of BGI file inside initgraph() function according to your system to make this program run.
根据您的系统更改initgraph()函数中BGI文件的路径,以使该程序运行。
C语言中点圆算法程序 (Program for Midpoint Circle Algorithm in C)
#include<stdio.h>
#include<graphics.h>
void drawcircle(int x0, int y0, int radius)
{
int x = radius;
int y = 0;
int err = 0;
while (x >= y)
{
putpixel(x0 + x, y0 + y, 7);
putpixel(x0 + y, y0 + x, 7);
putpixel(x0 - y, y0 + x, 7);
putpixel(x0 - x, y0 + y, 7);
putpixel(x0 - x, y0 - y, 7);
putpixel(x0 - y, y0 - x, 7);
putpixel(x0 + y, y0 - x, 7);
putpixel(x0 + x, y0 - y, 7);
if (err <= 0)
{
y += 1;
err += 2*y + 1;
}
if (err > 0)
{
x -= 1;
err -= 2*x + 1;
}
}
}
int main()
{
int gdriver=DETECT, gmode, error, x, y, r;
initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");
printf("Enter radius of circle: ");
scanf("%d", &r);
printf("Enter co-ordinates of center(x and y): ");
scanf("%d%d", &x, &y);
drawcircle(x, y, r);
return 0;
}
C ++中点圆算法程序 (Program for Midpoint Circle Algorithm in C++)
#include<iostream.h>
#include<graphics.h>
void drawcircle(int x0, int y0, int radius)
{
int x = radius;
int y = 0;
int err = 0;
while (x >= y)
{
putpixel(x0 + x, y0 + y, 7);
putpixel(x0 + y, y0 + x, 7);
putpixel(x0 - y, y0 + x, 7);
putpixel(x0 - x, y0 + y, 7);
putpixel(x0 - x, y0 - y, 7);
putpixel(x0 - y, y0 - x, 7);
putpixel(x0 + y, y0 - x, 7);
putpixel(x0 + x, y0 - y, 7);
if (err <= 0)
{
y += 1;
err += 2*y + 1;
}
if (err > 0)
{
x -= 1;
err -= 2*x + 1;
}
}
}
int main()
{
int gdriver=DETECT, gmode, error, x, y, r;
initgraph(&gdriver, &gmode, "c:\\turboc3\\bgi");
cout<<"Enter radius of circle: ";
cin>>r;
cout<<"Enter co-ordinates of center(x and y): ";
cin>>x>>y;
drawcircle(x, y, r);
return 0;
}
Output
输出量
翻译自: https://www.thecrazyprogrammer.com/2016/12/bresenhams-midpoint-circle-algorithm-c-c.html
中点画圆算法