慢羊羊的空间

无为,无我,无欲,居下,清虚,自然

[图形学] 画圆(基于 Bresenham 算法) 铜牌收录

图形学中的 Bresenham 画圆算法是基于中点画圆算法的派生,以下是该算法的 C 语言实现:

///////////////////////////////////////////////////
// 程序名称:基于 Bresenham 算法画圆
// 编译环境:Visual C++ 6.0 / 2010,EasyX_20210730
// 作  者:YangW <yw80@qq.com>
// 最后修改:2011-5-3
//
#include <graphics.h>
#include <conio.h>

// 使用 Bresenham 画圆法
void Circle_Bresenham(int x, int y, int r, int color)
{
	int tx = 0, ty = r, d = 3 - 2 * 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 += 4 * tx + 6;
		else			// 取下面的点
			d += 4 * (tx - ty) + 10, ty--;

		tx++;
	}
}

// 主函数
int main()
{
	initgraph(640, 480);
	
	// 测试画圆
	Circle_Bresenham(320, 240, 200, RED);
	Circle_Bresenham(320, 240, 101, RED);
	
	// 按任意键退出
	_getch();
	closegraph();
	return 0;
}

评论 (4) -

  • 想请问什么时候macos也可以安装啊
    • 你得使用别一个图形库,是基于SDL开发的,是类easyx 的库,安卓,linux,苹果都可以使用,语法上都类似,并且稍微修改下就能运行
      • 具体名字有吗
        • 这种库有很多很多的类库数不胜数,https://sdl-bgi.sourceforge.io/ 这是该绘图库的官网,里头有很多类 graphics 绘图的下载别下错了,还有就是该基于SDL的绘图库只支持SDL 好像SDL2不清楚支不支持,

添加评论