天天看点

中点画圆法(计算机图形学)

效果展示

中点画圆法(计算机图形学)
中点画圆法(计算机图形学)

c/c++代码

#include <graphics.h>
#include <conio.h>
#include<stdio.h>

// 中点画圆法
void Circle_Midpoint(int x, int y, int r, int color)
{
	int tx = 0, ty = r, d = 1 - r;

	while(tx <= ty)
	{
		// 利用圆的八分对称性画点
		putpixel(x + tx, y + ty, color);
		putpixel(x + tx, y - ty, color);
		putpixel(x - tx, y + ty, color);
		putpixel(x - tx, y - ty, color);
		putpixel(x + ty, y + tx, color);
		putpixel(x + ty, y - tx, color);
		putpixel(x - ty, y + tx, color);
		putpixel(x - ty, y - tx, color);

		if(d < 0)
			d += 2 * tx + 3;
		else
			d += 2 * (tx - ty) + 5, ty--;

		tx++;
	}
}


// 主函数
void main()
{

	//请输入圆心坐标和半径
	int x,y,r;
	printf("请输入圆心坐标和半径:\n");
	scanf("%d%d%d",&x,&y,&r);
	
	initgraph(640, 480);
	
	// 测试画圆
	Circle_Midpoint((x,y,r, RED);
	
	// 按任意键退出
	getch();
	closegraph();
}