逐点比较法生成圆弧

算法推导

《计算机图形学教程》中推导过程如下所示:
逐点比较法生成圆弧_第1张图片逐点比较法生成圆弧_第2张图片
逐点比较法生成圆弧_第3张图片
参考文章

算法代码

bool cb_arc(int x0, int y0, int x1, int y1, int x2, int y2, int x3, int y3, int a)
{
	// 参数分别为:圆心 圆弧端点1 圆弧端点2 圆弧起点3(3=2或3=1) a为画笔颜色
	int x, y, k;
	int x4;
	int f = 0;
	int s = 0;
	if (pow(x1 - x0, 2) + pow(y1 - y0, 2) != pow(x2 - x0, 2) + pow(y2 - y0, 2))
		return false;
	if(x2>=x1)
		if (y2 >= y1) {
			if ((x3 == x2) && (y3 == y2)) {
				k = 1; s = 1; x4 = x1; x = x2; y = y2;
			}
			else if ((x3 == x1) && (y3 == y1)){
				k = 5; s = 1; x4 = x2; x = x1; y = y1;
			}
		}
		else{
			if ((x3 == x2) && (y3 == y2)) {
				k = 2; s = 1; x4 = x1; x = x2; y = y2;
			}
			else if ((x3 == x1) && (y3 == y1)) {
				k = 6; s = 1; x4 = x2; x = x1; y = y1;
			}
		}
	else {
		if (y2 >= y1) {
			if ((x3 == x2) && (y3 == y2)) {
				k = 7; s = 1; x4 = x1; x = x2; y = y2;
			}
			else if ((x3 == x1) && (y3 == y1)) {
				k = 3; s = 1; x4 = x2; x = x1; y = y1;
			}
		}
		else {
			if ((x3 == x2) && (y3 == y2)) {
				k = 4; s = 1; x4 = x1; x = x2; y = y2;
			}
			else if ((x3 == x1) && (y3 == y1)) {
				k = 8; s = 1; x4 = x2; x = x1; y = y1;
			}
		}
	}
	if (s == 0)return false;
	putpixel(x, y, a);
	while (abs(x4 - x) > 1)
	{
		if (f >= 0)
			switch (k)
			{
			case 1:
				putpixel(x--, y, a);
				f = f - 2 * (x - x0) + 1;
				break;
			case 2:
				putpixel(x--, y, a);
				f = f - 2 * (x - x0) + 1;
				break;
			case 3:
				putpixel(x, y++, a);
				f = f - 2 * (y - y0) + 1;
				break;
			case 4:
				putpixel(x++, y, a);
				f = f - 2 * (x - x0) + 1;
				break;
			case 5:
				putpixel(x, y++, a);
				f = f - 2 * (y - y0) + 1;
				break;
			case 6:
				putpixel(x, y--, a);
				f = f - 2 * (y - y0) + 1;
				break;
			case 7:
				putpixel(x++, y, a);
				f = f - 2 * (x - x0) + 1;
				break;
			case 8:
				putpixel(x, y--, a);
				f = f - 2 * (y - y0) + 1;
				break;
			}
		else
			switch (k)
			{
			case 1:
				putpixel(x, y--, a);
				f = f + 2 * (y - y0) + 1;
				break;
			case 2:
				putpixel(x, y++, a);
				f = f + 2 * (y - y0) + 1;
				break;
			case 3:
				putpixel(x--, y, a);
				f = f + 2 * (x - x0) + 1;
				break;
			case 4:
				putpixel(x, y++, a);
				f = f + 2 * (y - y0) + 1;
				break;
			case 5:
				putpixel(x++, y, a);
				f = f + 2 * (x - x0) + 1;
				break;
			case 6:
				putpixel(x++, y, a);
				f = f + 2 * (x - x0) + 1;
				break;
			case 7:
				putpixel(x, y--, a);
				f = f + 2 * (y - y0) + 1;
				break;
			case 8:
				putpixel(x--, y, a);
				f = f + 2 * (x - x0) + 1;
				break;
			}
	}
	return true;
}

你可能感兴趣的:(计算机图形学,算法,c++,数据结构)