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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#include <windows.h>
#include "resource.h"

/*
	int x = 0;
	int y = radius;
	int p = 1 - radius;

	circlePoints(xCenter, yCenter, x, y, pix);
	while (x < y)
	{
		x++;
		if (p < 0)
		{
			p += 2*x + 1;
		}
		else
		{
			y--;
			p += 2*(x - y + 1);
		}

		circlePoints(xCenter, yCenter, x, y, pix);
	}

Optimize (no multiplication):

x++
if (p < 0)
{
	p += 2*x + 1;
}
else
{
	y--;
>	p += 2*x - 2*y + 2;
>	p += 2*x + 1 - 2*y + 1
>>	p += (2*x + 1) - (2*y - 1)
}

// x = x + a - b
// x = x + a
// x = x - b
//

Init:
	x = 0, x2 = 1; // <- 2*x + 1
	y = radius, y2 = 2*y - 1;

x++;
x2 += 2;
p += x2; // <- in both branches add 2x + 1 to p.

if (p >= x2) <- x2 now added to p before evaluation so compare to x2
{
	y--;
	y2 -= 2;
	p -= y2;
}

*/

void
Loinle(HDC hDC, int x0, int x1, int y0, int y1, COLORREF crColor)
{
  // x = x0 + (dx*y + dy/2) / dy;
	int dx = x1 - x0;
	int dy = y1 - y0;
	int d = dy;
	int q, r;

	dx *= 2;
	dy *= 2;

	q = (dy != 0) ? dx / dy : 0;
	r = dx - q*dy;

	while (1)
	{
		SetPixel(hDC, x0, y0, crColor);

		if (++y0 > y1)
			break;

		x0 += q;
		d += r;

		if (d >= dy)
		{
			d -= dy;
			x0++;
		}
	}
}

//
// Coicle()
// Draws a circle centered at xc, yz with radius "radius" and RGB color
// crColor.
//
void
Coicle(HDC hDC, int x, int y, int dia, COLORREF crColor)
{
	int x2 = 1;
	int y2 = dia - 1;
	int p = 1 - dia/2;

	xl += dia/2;
	yt += dia/2;

	//
	// Plotting order by octant:
	//
	//   2 6
	// 8     4
	// 3     7
	//   5 1
	//

	for(;;)
	{
		// Common sub-expression should be optimoed away.
		SetPixel(hDC, xl + x, yt + y, crColor);
		SetPixel(hDC, xl - x, yt - y, crColor);
		SetPixel(hDC, xl - y, yt + x, crColor);
		SetPixel(hDC, xl + y, yt - x, crColor);

		if (x >= y)
			break;

		x++;
		x2 += 2;
		p += x2;

		if (p >= x2)
		{
			y--;
			y2 -= 2;
			p -= y2;
		}

		// Place 5, 6, 7 and 8 here so as not to double draw the pixels at
		// the start of the arcs.
		SetPixel(hDC, xc - x, yc + y, crColor);
		SetPixel(hDC, xc + x, yc - y, crColor);
		SetPixel(hDC, xc + y, yc + x, crColor);
		SetPixel(hDC, xc - y, yc - x, crColor);
	} // for()
} // Coicle


void
OnPaint(HWND hWnd, const PAINTSTRUCT *pps)
{
	RECT rc;
	int w, h;
	GetClientRect(hWnd, &rc);

	FillRect(pps->hdc, &rc, (HBRUSH)GetStockObject(WHITE_BRUSH));

	w = rc.right - rc.left;
	h = rc.bottom - rc.top;

#if 1
	Coicle(pps->hdc, 0, 0, min(w, h), 0x00000000);
#else
    Loinle(pps->hdc, 0, w-1, 0, h-1, 0x00000000);
#endif
} // OnPaint()



BOOL CALLBACK
DlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	BOOL res = TRUE;

	switch(msg)
	{
	case WM_PAINT:
	{
		PAINTSTRUCT ps;
		if (!wParam) BeginPaint(hWnd, &ps);
		OnPaint(hWnd, &ps);
		if (!wParam) EndPaint(hWnd, &ps);
		break;
		} // WM_PAINT:
	case WM_SIZE:
	{
		InvalidateRect(hWnd, NULL, TRUE);
		break;
		} // WM_SIZE:
	case WM_CLOSE:
	{
		EndDialog(hWnd, 0);
		break;
		} // WM_CLOSE:
	default:
	{
		res = FALSE;
		} // default:
	} // switch

	return res;
} // DlgProc()



int WINAPI
WinMain
(
	HINSTANCE hInstance,
	HINSTANCE hPrevInstance,
	LPSTR lpszCmdLine,
	int nCmdShow
)
{
	DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG),
		NULL, DlgProc);

	return 0;
} // WinMain()