summaryrefslogtreecommitdiff
path: root/src/draw/line.5c
blob: 747768b0e96f14071dbfe5564cd69294def1cc27 (plain) (blame)
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
#!/usr/bin/nickle

autoimport Cairo;
autoload PRNG;

int
sign(int x)
{
	return x == 0 ? 0 : x < 0 ? -1 : 1;
}

int X_AXIS = 0;
int Y_AXIS = 1;

typedef struct {
	int	major;
	int	minor;
	int	sign_major;
	int	sign_minor;
	int	e;
	int	e1;
	int	e3;
	bool	first;
} clip_context;

typedef struct {
	int	maj1, min1, maj2, min2;
} clip_box;

typedef struct {
	int	x1, y1, x2, y2;
} box;

typedef struct {
	int	x, y;
} point;

typedef struct {
	int	x1, y1, x2, y2;
	box	b;
	point[]	clipped;
	point[]	run;
} test;

box	bounds = { .x1 = 10, .x2 = 30, .y1 = 10, .y2 = 30 };

int
div_ceil(a, b) {
	a += b;
	assert(a >= 0 && b > 0, "bad divide args %d %d\n", a, b);
	return (a + b - 1) // b - 1;
}

int
div_floor_plus_one(a, b) {
	a += b;
	assert(a >= 0 && b > 0, "bad divide args %d %d\n", a, b);
	return a // b;
}

bool
clip(*clip_context c, *clip_box b)
{
	int	adjust_major = 0, adjust_minor = 0;

	/* Clip major axis */
	if (c->major < b->maj1) {
		if (c->sign_major <= 0)
			return false;
		adjust_major = b->maj1 - c->major;
	} else if (c->major >= b->maj2) {
		if (c->sign_major >= 0)
			return false;
		adjust_major = c->major - (b->maj2-1);
	}

	/* Clip minor axis */
	if (c->minor < b->min1) {
		if (c->sign_minor <= 0)
			return false;
		adjust_minor = b->min1 - c->minor;
	} else if (c->minor >= b->min2) {
		if (c->sign_minor >= 0)
			return false;
		adjust_minor = c->minor - (b->min2-1);
	}

	/* If unclipped, we're done */
	if (adjust_major == 0 && adjust_minor == 0)
		return true;

	/* See how much minor adjustment would happen during
	 * a major clip. This is a bit tricky because line drawing
	 * isn't symmetrical when the line passes exactly between
	 * two pixels, we have to pick which one gets drawn
	 */
	int	adj_min;

	if (!c->first)
		adj_min = div_ceil(c->e + adjust_major * c->e1, -c->e3);
	else
		adj_min = div_floor_plus_one(c->e + adjust_major * c->e1, -c->e3);

	/* Compare that to the minor clip and pick
	 * the larger amount.
	 */
	printf ("\tinitial major %d minor %d error %d e1 %d e3 %d\n", c->major, c->minor, c->e, c->e1, c->e3);

	if (adj_min < adjust_minor) {
		printf("\tminor clip dominates %d < %d. adjust major %d -> ",
		       adj_min, adjust_minor, adjust_major);
		if (c->first)
			adjust_major = div_ceil(c->e - adjust_minor * c->e3, c->e1);
		else
			adjust_major = div_floor_plus_one(c->e - adjust_minor * c->e3, c->e1);
		printf("%d\n", adjust_major);
	} else {
		printf("\tminor clip dominates %d > %d. adjust minor %d -> ",
		       adj_min, adjust_minor, adjust_minor);
		adjust_minor = adj_min;
		printf("%d\n", adjust_minor);
	}

	c->e += adjust_major * c->e1 + adjust_minor * c->e3;

	c->major += c->sign_major * adjust_major;
	c->minor += c->sign_minor * adjust_minor;

	printf ("\tadjust major %d adjust minor %d e %d e1 %d e3 %e\n",
		adjust_major, adjust_minor, c->e, c->e1, c->e3);

	if (c->e >= 0)
		printf ("error positive e %d e1 %d e3 %d\n",
			c->e, c->e1, c->e3);
	if (c->e < c->e3)
		printf ("error magnitude too large e %d e1 %d e3 %d\n", c->e, c->e1, c->e3);

	return true;
}

test
line(int x1, int y1, int x2, int y2, *box b) {

	int	dx = x2 - x1;
	int	dy = y2 - y1;
	int	signdx = sign(dx);
	int	signdy = sign(dy);
	int	adx = abs(dx);
	int	ady = abs(dy);
	int	axis;
	int	e, e1, e2, e3;
	int	len;
	clip_context	clip_1, clip_2;
	clip_box	c;
	bool		clipped = false;
	test		t = {
		.x1 = x1,
		.y1 = y1,
		.x2 = x2,
		.y2 = y2,
		.b = *b,
		.clipped = (point[...]) {},
		.run = (point[...]) {}
	};

	if (adx >= ady) {
		axis = X_AXIS;
		e1 = ady << 1;
		e2 = e1 - (adx << 1);
		e = e1 - adx;
		len = adx;

		clip_1.major = x1;
		clip_1.minor = y1;
		clip_2.major = x2;
		clip_2.minor = y2;
		clip_1.sign_major = signdx;
		clip_1.sign_minor = signdy;

		c.maj1 = b->x1;
		c.maj2 = b->x2;
		c.min1 = b->y1;
		c.min2 = b->y2;
	} else {
		axis = Y_AXIS;
		e1 = adx << 1;
		e2 = e1 - (ady << 1);
		e = e1 - ady;
		len = ady;

		clip_1.major = y1;
		clip_1.minor = x1;
		clip_2.major = y2;
		clip_2.minor = x2;
		clip_1.sign_major = signdy;
		clip_1.sign_minor = signdx;
		c.maj1 = b->y1;
		c.maj2 = b->y2;
		c.min1 = b->x1;
		c.min2 = b->x2;
	}

	e3 = e2 - e1;
	e = e - e1;

	clip_1.first = true;
	clip_2.first = false;
	clip_2.e = clip_1.e = e;
	clip_2.e1 = clip_1.e1 = e1;
	clip_2.e3 = clip_1.e3 = e3;
	clip_2.sign_major = -clip_1.sign_major;
	clip_2.sign_minor = -clip_1.sign_minor;

	printf ("clip start:\n");
	if (!clip(&clip_1, &c))
		clipped = true;

	printf("clip end:\n");
	if (!clip(&clip_2, &c))
		clipped = true;

	int	clip_len;
	int	clip_x, clip_y;
	int	clip_e;
	int	x_major, x_minor;
	int	y_major, y_minor;

	clip_len = clip_1.sign_major * (clip_2.major - clip_1.major);
	if (clip_len < 0)
		clipped = true;

	int x, y;

	if (axis == X_AXIS) {
		x = clip_1.major;
		y = clip_1.minor;
		x_major = clip_1.sign_major;
		x_minor = 0;
		y_major = 0;
		y_minor = clip_1.sign_minor;
	} else {
		x = clip_1.minor;
		y = clip_1.major;
		x_major = 0;
		x_minor = clip_1.sign_minor;
		y_major = clip_1.sign_major;
		y_minor = 0;
	}

	clip_e = clip_1.e;

	if (clipped)
		clip_len = -1;

	while (clip_len-- >= 0) {
		t.clipped[dim(t.clipped)] = (point) { .x = x, .y = y };
		x += x_major;
		y += y_major;
		clip_e += e1;
		if (clip_e >= 0) {
			x += x_minor;
			y += y_minor;
			clip_e += e3;
		}
	}

	x = x1;
	y = y1;

	while (len-- >= 0) {
		if (bounds.x1 <= x && x < bounds.x2 &&
		    bounds.y1 <= y && y < bounds.y2) {
			t.run[dim(t.run)] = (point) { .x = x, .y = y };
		}
		x += x_major;
		y += y_major;
		e += e1;
		if (e >= 0) {
			x += x_minor;
			y += y_minor;
			e += e3;
		}
	}
	return t;
}

void read_events (Cairo::cairo_t cr)
{
	file	event = Cairo::open_event(cr);

	while (!File::end(event)) {
		string	event_line = File::fgets(event);
		if (String::index(event_line, "delete") >= 0)
			exit(0);
	}
}

#for (int y = 0; y < 20; y++)

void
show(cairo_t cr, test t)
{
	rectangle(cr, 0, 0, 40, 40);
	set_source_rgba(cr, 1, 1, 1, 1);
	fill(cr);

	set_source_rgba(cr, 0, 1, 0, .2);
	set_line_width(cr, 0.1);
	for (int x = 0; x < 40; x++) {
		move_to(cr, 0, x);
		line_to(cr, 40, x);
		move_to(cr, x, 0);
		line_to(cr, x, 40);
	}
	stroke(cr);

	rectangle(cr, t.b.x1, t.b.y1, t.b.x2 - t.b.x1, t.b.y2 - t.b.y1);
	set_line_width(cr, 0.1);
	set_source_rgba(cr, 0, 0, 0, 1);
	stroke(cr);

	move_to(cr, t.x1+.5, t.y1+.5);
	line_to(cr, t.x2+.5, t.y2+.5);
	move_to(cr, t.x2, t.y2);
	line_to(cr, t.x2+1, t.y2+1);
	move_to(cr, t.x2+1, t.y2);
	line_to(cr, t.x2, t.y2+1);
	stroke(cr);

	void pixels(point[] pt) {
		for (int i = 0; i < dim(pt); i++) {
			rectangle(cr, pt[i].x, pt[i].y, 1, 1);
		}
		fill(cr);
	}

	set_source_rgba(cr, 1, 0, 0, .5);
	pixels(t.clipped);

	set_source_rgba(cr, 0, 0, 1, .5);
	pixels(t.run);
}

bool
compare(test t)
{
	if (dim(t.clipped) != dim(t.run))
		return false;

	for (int i = 0; i < dim(t.clipped); i++)
		if (t.clipped[i] != t.run[i])
			return false;
	return true;
}

void
doit(int i)
{
	int	n;
	*box	b = &bounds;

	cairo_t cr = new(800, 800);

	scale(cr, 20, 20);

	for (;;) {
		PRNG::srandom(i);
		int	x1 = PRNG::randint(40);
		int	x2 = PRNG::randint(40);
		int	y1 = PRNG::randint(40);
		int	y2 = PRNG::randint(40);

		test t = line (x1, y1, x2, y2, &bounds);
		show(cr, t);
		if (!compare(t)) {
			printf("line %d -- %d x %d - %d x %d\n", i, x1, y1, x2, y2);
			gets();
		}
		i++;
	}

	read_events(cr);
}

int i = 0;
if (dim(argv) > 1)
	i = atoi(argv[1]);

doit(i);