#### Archived

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

# Drawing a line

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

## Recommended Posts

How do I draw a simple line given (x1, y1) and (x2, y2) coordinates using DirectX? Thanks All

##### Share on other sites
You will have to write you own function. There are a few algorithms out there that deal with this kind stuff, for example like the Bresenham''s algorithnm. The easiest way to do it get a graphic libary that does the job for you. CDX for DX comes to mind(For dos programming Allegro is a good one). Check them out at:
[url]http://www.cdx.sk[/url]

##### Share on other sites
try DrawPrimitive(...)
im using my own rootines, but i think
you can use DrawPrimitive to draw staff like lines etc...

##### Share on other sites
if you want a really slow method you could get a DC and make GDI calls(moveto(hdc,x,y);LineTo(hdc,x,y), but somehow I think making your own function to find yer slope and blit pixels would work a little faster.

##### Share on other sites
void BresenhamLine(int x0, int y0, int x1, int y1){	int XUnit, YUnit, Offset = y0 * Pitch + (x0*3);	int dy = y1 - y0;	if(dy < 0)	{		dy = -dy;		YUnit = -Pitch;	}	else		YUnit = Pitch;	int dx = x1 - x0;	if(dx < 0)	{		dx = -dx;		XUnit = -3;	}	else XUnit = 3;	int ErrorTerm = 0;	if(dx > dy)	{		for(int i = 0; i <= dx; i++)		{			Dest[Offset] = 255;			Dest[Offset+1] = 0;			Dest[Offset+2] = 0;			Offset += XUnit;			ErrorTerm += dy;			while(ErrorTerm > dx)			{				ErrorTerm -= dx;				Offset += YUnit;			}		}	}	else	{		for(int i = 0; i <= dy; i++)		{			Dest[Offset] = 255;			Dest[Offset+1] = 0;			Dest[Offset+2] = 0;			Offset += YUnit;			ErrorTerm += dx;			while(ErrorTerm > 0)			{				ErrorTerm -= dy;				Offset += XUnit;			}		}	}}[/CODE]This bit of source can show a line on the screen using a 24 bits surface.....Dest is the surface pointer

Dance with me......

http://members.xoom.com/CJdeVos/index.htm