I've been having the hardest time trying to get this code to work. I tried to take the pseudo-code from wikipedia and turn it into C++ code I can use for a project. bufPixel() draws a pixel on (buffer,x,y with attributes). When it draws the line, it appears to only draw straight down each time, as in, no matter what line, it will always draw a slope of 1 or -1. Thanks in advance.

Code:
```void bufLine(CHAR_INFO* buffer,short x0,short y0,short x1,short y1,WORD attributes) {         CHAR_INFO letter;         letter.Char.AsciiChar = ' ';         letter.Attributes = attributes;            bool steep = abs(y1 - y0) > abs(x1 - x0);          SHORT a;      if (steep)          {                 a = x0; x0 = y0; y0 = a;                 a = x1; x1 = y1; y1 = a;          }      if (x0 > x1)          {                 a = x0; x0 = x1; x1 = a;                 a = y0; y0 = y1; y1 = a;          }           int deltax = x1 - x0;         int deltay = abs(y1 - y0);         float error = 0;         float deltaerr = deltay / deltax;         int ystep;         if (y0 < y1)                 ystep = 1;         else                 ystep = -1;           int y = y0;         for (int x = x0;x < x1;x++)         {                 if (steep)                         bufPixel(buffer,y,x,attributes);                 else                         bufPixel(buffer,x,y,attributes);                 error = error + deltaerr;                 if (error >= 0.5)                 {                         y += ystep;                         error = error - 1;                 }         } }```

Code:
`float deltaerr = deltay / deltax;`

An integer divided by an integer performs integer division, whereas here you need to use floating point division:

Code:
`float deltaerr = (float)deltay / (float)deltax;`

However, the appeal of Bresenham's line algorithm is that it works fine purely with integers. Read the Wikipedia article a little further and you'll find the standard implementation.
Ah, thank you. I saw the other implementation further down, but I wanted to get this version working first Also, now I know that I have to do that to get it to divide correctly.
_player1537 wrote:
Ah, thank you. I saw the other implementation further down, but I wanted to get this version working first Also, now I know that I have to do that to get it to divide correctly.
So did that fix everything for you, or is it still buggy?
It fixed it
_player1537 wrote:
It fixed it
Awesome! How fast is the code in lines or pixels per second? Have you tried benchmarking it?
Nope, no idea how to either :/
Draw a million lines, time how long it takes
The non-optimised version won't draw lines correctly owing to limitations in the representation of floating-point numbers - the integer version does not have this problem.

As a test, you can write a program that adds 0.1 - a value that cannot be exactly represented by binary floating-point numbers - to a variable and display the results.

Code:
```std::cout.precision(20); for (float f = 0.f; f < 10.f; f += 0.1f) {     std::cout << f << std::endl; }```

This is why you should never store currency in floating point variables. (Fixed-point representations are generally used, such as storing the hundredths of the main currency as an integer - storing cents instead of dollars, for example, and there's usually a decimal type specifically for this type of usage).

Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

»
» All times are UTC - 5 Hours

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum