#### Archived

This topic is now archived and is closed to further replies.

# Anti Aliased Line Algorithm

This topic is 6469 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, Can someone give me an Anti Aliased Line Algorithm, I''ve heard of a couple such as the WU algoithm. How about an explantion and or the code explaining whats going on. I am currently using the Bresenham Line Drawing Algorithm. Can this be manipulated in such a way as to produce Anti Aliased Lines? More info on this would be appreciated. It is too make my Classic Vector Asteroid game more appealing to look at. Please no answers like "Search the web for wu algorithm" ) Iain Girdwood

##### Share on other sites
If you really want to understand it, I''d recommend you try to write one yourself. Start by taking a piece of grid paper, and considering the centre of each grid square to be the centre of a pixel onscreen. Draw a straight line between A and B. Now, to consider anti-aliased lines, you would probably say that the line has a width of 1. At each end of the line, draw out at right angles to a distance of half a square, and join it all up. That''s the line you want to draw. Mathematically, you should be able to calculate how much of each pixel is covered by the line, and you can generalise from there.

(This has now inspired me to do it myself.)

##### Share on other sites
Well, I recommend searching the web for the wu algorithm.

.
.
.

If you really are feeling lazy though, here is some code ripped straight from my engine. And the assembler business is for addition of colors, you can s. This is expecting a 32 bit screen target setup in a DWORD *screen global. It also needs WIDTH defined as the width of the screen (in pixels).
  void WuLine(float x1,float y1,float x2,float y2,BYTE r,BYTE g,BYTE b){ float xd,yd,grad,xend,yend,xgap,b1,b2,yf; int ix1,ix2,iy1,iy2; DWORD c1,c2; xd = x2-x1; yd = y2-y1; if (fabs(xd)>=fabs(yd)) { // Horizontal slope if (x1>x2) { float tmp=x1; x1=x2; x2=tmp; tmp=y1; y1=y2; y2=tmp; xd = x2-x1; yd = y2-y1; } yf = y1; grad = yd/xd; ix1 = ifloor(x1); ix2 = ifloor(x2); for (int x=ix1; x<=ix2; x++) { b2 = yf-float(ifloor(yf)); b1 = 1.0f-b2; DWORD tmp=(ifloor(yf)*WIDTH)+x; c1=RGB(BYTE(b1*r),BYTE(b1*g),BYTE(b1*b)); screen[tmp]=c1; c1=RGB(BYTE(b2*r),BYTE(b2*g),BYTE(b2*b)); tmp += WIDTH; screen[tmp]=c1; yf += grad; } } else { if (y1>y2) { float tmp=x1; x1=x2; x2=tmp; tmp=y1; y1=y2; y2=tmp; xd = x2-x1; yd = y2-y1; } yf = x1; grad = xd/yd; iy1 = ifloor(y1); iy2 = ifloor(y2); for (int y=iy1; y<=iy2; y++) { b2 = yf-float(ifloor(yf)); b1 = 1.0f-b2; DWORD tmp=(y*WIDTH)+ifloor(yf); c1=RGB(BYTE(b1*r),BYTE(b1*g),BYTE(b1*b)); screen[tmp]=c1; c1=RGB(BYTE(b2*r),BYTE(b2*g),BYTE(b2*b)); tmp++; screen[tmp]=c1; yf += grad; } }}

PreManDrake

##### Share on other sites
Hi,

Thanks for the reply.

I managed to code a WU line drawing function. My code is very similar to yours as it would be but I''m gonna quickly try your code to see if it looks better in any way. Thanks for sharing it with me.

Regards

Iain

##### Share on other sites
This site has a good bit of information on Wu antialiased lines:
http://freespace.virgin.net/hugo.elias/.

Also you could look at the recently released Big Black Book by Mike Abrash. He talks about Wu antialiased lines in chapter 42. You can get it here: http://www.ddj.com/articles/2001/0165/0165f/0165f.htm

Stay Lucky, Graham "Mournblade" Reeds,
ICQ: 30514803
http://homepage.dtn.ntl.com/grahamr

##### Share on other sites
Since Abrash has released his black book on the net and it contains a very good tutorial about WU you should probably check it out. For link check news history here a gamedev.

1. 1
2. 2
3. 3
Rutin
15
4. 4
khawk
14
5. 5
frob
12

• 9
• 11
• 11
• 23
• 12
• ### Forum Statistics

• Total Topics
633660
• Total Posts
3013221
×

## Important Information

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!