Jump to content
• Advertisement

#### Archived

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

# simple line algorithm

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

If you intended to correct an error in the post then please contact us.

## Recommended Posts

This is not even optimized or anything, im just tryin to have a sloped line and all i get is a horizontal line, and i dont know why!?! Well heres the code...
  #include #include #define MODE13H 0x13 #define TEXTMODE 0x00 unsigned char far *video_buffer=(unsigned char far *)0xA0000000L; void video_mode(int mode) { union REGS inregs, outregs; inregs.h.ah=0; inregs.h.al=mode; int86(0x10,&inregs, &outregs); } void plot_pixel(int x, int y, int color) { int offset; offset= ((y<<8)+(y<<6))+x; video_buffer[offset]=(unsigned char)color; } void slope_line(int x1, int y1, int x2, int y2, int color) { int x_length, y_length, temp, x, y, i; float x_step, y_step;//coordinates for pixel-plotting x_length = ABS((x2 - x1)); y_length = ABS((y2 -y1)); //Absolute Values! if(x_length > y_length) { if(x1 > x2) //exchanging coordinates?? { //if x-coord are exchanged, exchange y-coord temp = x1; x1 = x2; x2 = temp; temp = y1; y1 = y2; y2 =temp; }//end if x1 y_length else { if(y1 > y2) //exchanging coordinates?? { temp = y1; y1 = y2; y2 = temp; temp = x1; x1 = x2; x2 = temp; }//end if y1 > y2 x_step = ((x2 - x1)/(y2 - y1)); x=x1; //x is used for pixel-plotting for(y= y1, i=0; y <= y2, i<=y_length; ++y, ++i) { x +=(float)x_step*i; plot_pixel(x,y,(unsigned char)color); }//end for y <= y2 }//end else } int main() { video_mode(MODE13H); slope_line(0,150,200,130,15); while(!kbhit()) ; video_mode(TEXTMODE); return 0; } 
Edited by - PredeX on February 8, 2002 8:21:03 AM Edited by - PredeX on February 8, 2002 8:21:48 AM

#### Share this post

##### Share on other sites
Advertisement
Stop that now and try to implement the well-known line algorithm of Bresenham...

#### Share this post

##### Share on other sites
I didn''t read it completely (quick scan), but I found at least 3 problems so far.. which could be the cause of your problem...

y_step=(float)((y2 - y1)/(x2 - x1));
y = (float)y1;

x_step = ((x2 - x1)/(y2 - y1));
x = x1;

change these lines to:
y_step=(y2 - y1)/(float)(x2 - x1);
y = (float)y1;

x_step = (x2 - x1)/(float)(y2 - y1);
x = (float)x1;

Also, make x, y float instead of ints.
Then change these lines:
y +=(float)y_step*i;
x +=(float)x_step*i;

To this:
y += y_step;
x += x_step;

and you don''t have to deal with ''i'' anymore.

Billy

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
Rutin
25
2. 2
JoeJ
20
3. 3
4. 4
5. 5
• Advertisement

• 9
• 9
• 46
• 41
• 23
• ### Forum Statistics

• Total Topics
631750
• Total Posts
3002051
×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!