aboutsummaryrefslogtreecommitdiff
path: root/Src/Plugins/Visualization/vis_nsfs/linedraw.cpp
blob: a240cf65cbd75ec140dc4198405eb517dbfbf69b (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
#include <windows.h>

static void __inline DrawPixel(int color, unsigned char *fb)
{
  int a=*fb;
  a+=color;
  if (a>255)a=255;
  *fb=(unsigned char)a;
}

// based on abrash's wu antialiasing routine
void line(unsigned char *fb, int X0, int Y0, int X1, int Y1, int w, int h)
{
	unsigned int ErrorAdj=0, ErrorAcc;
	int DeltaX, DeltaY, XDir=1;

	if (Y0 > Y1) 
	{
		int t = Y0; Y0 = Y1; Y1 = t;
		t = X0; X0 = X1; X1 = t;
	}

	DeltaY = Y1 - Y0;
	DeltaX = X1 - X0;
	if (DeltaX < 0) 
	{
		XDir = -1;
		DeltaX = -DeltaX;
	}

	if (DeltaY > DeltaX) {

		ErrorAcc = (X0&0xffff);
		Y0>>=16;
		X0>>=16;
		DeltaY += 65535;   
		DeltaY >>= 16;
		if (DeltaY) ErrorAdj = DeltaX/DeltaY;
		fb+=Y0*w+X0;
		while (DeltaY-- >= 0) {
			if (X0 > 0 && Y0 >= 0 && X0 < w-1 && Y0 < h)
			{
				int Weighting = ErrorAcc >> 10;
				DrawPixel(Weighting^63,fb);
				DrawPixel(Weighting,fb+XDir);
			}
	        
			ErrorAcc += ErrorAdj;
			if (ErrorAcc >= 65536) {
				X0 += XDir;
				fb += XDir;
				ErrorAcc -= 65536;
			}
			fb+=w;
			Y0++;
		}
	}
	else
	{
		ErrorAcc = (Y0&0xffff);
		X0>>=16;
		Y0>>=16;
		DeltaX += 65535;   
		DeltaX >>= 16;
		if (DeltaX) ErrorAdj = DeltaY/DeltaX;

		fb+=Y0*w+X0;
		while (DeltaX-- >= 0) {
			if (Y0 >= 0 && Y0 < h-1 && X0 >= 0 && X0 < w)
			{
				int Weighting = ErrorAcc >> 10;
				DrawPixel(Weighting^63,fb);
				DrawPixel(Weighting,fb+w);
			}

			ErrorAcc += ErrorAdj;
			if (ErrorAcc >= 65536) {
				Y0++;
				fb+=w;
				ErrorAcc-=65536;
			}
			fb+=XDir;
			X0+=XDir;
		}
	}
}