Re: More on antialiasing...

Search this archive.

From: Sergio Masci (sergio@titan.demon.co.uk)
Date: Tue 01 Feb 2000 - 21:28:37 IST


Here's some code I knocked up to draw anti-aliased lines. I hope it helps.
It uses 32 bit fixed point arithmatic and only allows 8 levels of
brightness, but its really simple. I've also produced a simple picture
showing lines that are drawn with it as pairs of anti-aliased and 
non-anti-aliased lines.

The original line drawing code was taken from IPAD and hacked to removed
IPAD dependentcies.

Sergio Masci
http://www.demon.co.uk/titan




for a simple test define colour_tbl as

int	colour_tbl[] =
	{
// standard VGA palette grey scale
		30, 29, 28, 27, 26, 25, 24, 23, 22
	};


/*----------------------------------------------------------------------*/
// colour_tbl should contain 9 entries corresponding to the
// brightnesses of the required colour in steps of 1/8 from 0 to 8
//
// colour_tbl[0] = colour * 8/8
// colour_tbl[1] = colour * 7/8
// colour_tbl[2] = colour * 6/8
// ...
// colour_tbl[7] = colour * 1/8
// colour_tbl[8] = colour * 0/8
/*----------------------------------------------------------------------*/
void draw_line(int x1, int y1, int x2, int y2, int colour_tbl[])
{
	int	j, cnt,
		ixstep, iystep;

	long	fx1, fy1,
		xstep, ystep;

	int	p1, p2;


	dx = x2 - x1;
	dy = y2 - y1;
        
	ixstep = sign(dx);
	iystep = sign(dy);

	xstep = ixstep << 16;
	ystep = iystep << 16;

	if (abs(dx) <= abs(dy))
	{
		xstep = (dx << 16) / abs(dy);
	}
	else
	{	ystep = (dy << 16) / abs(dx);
	}

	fx1 = x1 << 16;
	fy1 = y1 << 16;


	// include end point i.e. x2-x1+1 where cnt = x2-x1
	cnt++;

>>>>>>
>>>>>>	replace this section of code
>>>>>>	

	int colour = colour_tbl[0];

	for (j=cnt; j>0; j--)
	{
		x1 = fx1 >> 16;
		y1 = fy1 >> 16;

		set_xpixel_colour(x1, y1, colour);

		fx1 += xstep;
		fy1 += ystep;
	}

>>>>>>
>>>>>>	with this
>>>>>>

	if (abs(dx) <= abs(dy))
	{
		for (j=cnt; j>0; j--)
		{
			x1 = fx1 >> 16;

			// fraction of pixel between x and x+1
			// as n/8 and n1/8

			p1 = (fx1 >> 13) & 7;

			p2 = 9 - p1;

			set_xpixel_colour(x1,   y1, colour_tbl[p1]);
			set_xpixel_colour(x1+1, y1, colour_tbl[p2]);

			fx1 += xstep;
			y1  += iystep;
		}
	}
	else
	{
		for (j=cnt; j>0; j--)
		{
			y1 = fy1 >> 16;

			// fraction of pixel between y and y+1
			// as n/8 and n1/8

			p1 = (fy1 >> 13) & 7;

			p2 = 8 - p1;

			set_xpixel_colour(x1, y1,   colour_tbl[p1]);
			set_xpixel_colour(x1, y1+1, colour_tbl[p2]);

			x1  += ixstep;
			fy1 += ystep;
		}
	}
}






This archive was generated by hypermail 2.1.4 : Wed 21 Jan 2004 - 22:10:23 IST