中点画圆算法_C和C ++中的中点圆算法

中点画圆算法

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)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#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;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#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++)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#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;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#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

输出量

中点画圆算法_C和C ++中的中点圆算法

翻译自: https://www.thecrazyprogrammer.com/2016/12/bresenhams-midpoint-circle-algorithm-c-c.html

中点画圆算法