Line drawing

Programming, for all ages and all languages.
Post Reply
areo

Line drawing

Post by areo »

Hello,
I need to draw line.
My putpixel function is

void pixel(unsigned long colour, unsigned short posX, unsigned short posY) {
   video[posY * 1024 + posX] = colour;
}

Please help. If you can then write source code. :)
Dex4u

Re:Line drawing

Post by Dex4u »

I am not in to C myself, but here is some code that may help, its for the xbox. http://www.xbdev.net/non_xdk/openxdk/drawline/index.php
codemastersnake

Re:Line drawing

Post by codemastersnake »

Check out the attached file.... I created it for VGA graphics programming....
distantvoices
Member
Member
Posts: 1600
Joined: Wed Oct 18, 2006 11:59 am
Location: Vienna/Austria
Contact:

Re:Line drawing

Post by distantvoices »

@areo: pls refrain from source code requests. State what your problem is or what doesn't work. We are not your coding slaves.

Stay safe.
... the osdever formerly known as beyond infinity ...
BlueillusionOS iso image
Tora OS

Re:Line drawing

Post by Tora OS »

Here's some code i quickly wrote for a horizontal line. To make it do vertical lines reverse the axis in the code.

Code: Select all

//Draw Horizontal Line:
void h_line(int Yval, int start_Xval, int width, char color)
{
int END_Xval = start_Xval + width; /*set our ending pixel as WIDTH+STARTING PIXEL */
while (start_Xval <= END_XVal) /*while the current pixel is less than or equal to the ending X value */
{
pixel(color, Xval, start_Yval); //draw the pixel
start_Xval++; //increase our current X pixel by one.
}
return; //done
}

Cjmovie

Re:Line drawing

Post by Cjmovie »

Goto gamedev.net, look under articles->Programming->graphics

Then look for an article on Breseham Line algoritham (I think that's it...)
Rainer

Re:Line drawing

Post by Rainer »

if this doesnt help you, then I dont know what will:

http://en.wikipedia.org/wiki/Bresenham%27s_algorithm
NotTheCHEAT

Re:Line drawing

Post by NotTheCHEAT »

@areo: pls refrain from source code requests. State what your problem is or what doesn't work. We are not your coding slaves.
Was he specifically requesting source code, or was he asking if anyone knew how? There's nothing wrong with saying "Can someone please tell me how to draw a line using xxx pixel-drawing function?" What isn't OK would be to say "Give me some source code for drawing a line using xxx pixel-drawing function!!!"

Remember to be nice ;)

And yes, you want the Bresenham algorithm. Basically, since every line is the set of solutions to a linear equation, it figures out what those solutions are and plots them. I don't remember exactly what it does, but that's how it works. It's fast, easy to implement and understand, etc.
There is also a Bresenham circle algorithm, and others (I think).
mystran

Re:Line drawing

Post by mystran »

Bresenham circle is indeed another popular algorithm.

Anyway, the basic idea of drawing a line is looping over one coordinate, and solving the the line equation (or using linear interpolation) to get the other coordinate. The only complication is deciding which coordinate to loop so that you don't end up with holes.

But yeah Bresenham is a good algorithm.
Cjmovie

Re:Line drawing

Post by Cjmovie »

Yay, I was right ;D!

;)
Post Reply