Skip to main content
GameDev.net gamedev.net
🔒 Locked

Drawing a line

Started by Emagen Jan 24, 2000 at 10:24 PM 3 replies 750+ views
Original Post
Emagen
Emagen
How do I draw a simple line given (x1, y1) and (x2, y2) coordinates using DirectX? Thanks All
sayshell
sayshell
try DrawPrimitive(...)
im using my own rootines, but i think
you can use DrawPrimitive to draw staff like lines etc...
Fnjord
Fnjord
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.
--- Incompetence is a double edged banana---
CJ
CJ

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

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.