Does anybody know a faster line-drawing algorithm? This is Bresenham's, C++.
void draw_line(SDL_Surface *screen, int x1, int y1, int x2, int y2, Uint8 color)
{
/* Draw a line from (x1, y1) to (x2, y2) */
int dy = y2 - y1;
int dx = x2 - x1;
int stepx;
int stepy;
if (dy < 0)
{
dy = -dy;
stepy = -1;
}
else
{
stepy = 1;
}
There are times where there is a difference. << works, but if you use >> on negative numbers: from that and dy /= 2; For instance, if 2's complement is being used, and if dy = -5;
So if 2's compliment is used, then -5 >> 1 == -3. The rounding went down to negative infinity. But -5 / 2 = -2, because rounding is supposed to go towards zero.
Name:
Anonymous2011-12-27 16:35
>>5
Uh... Yeah? I know how binary arithmetic works. dy can never be negative during the right shift in >>1, because of the (similarly OPTIMIZED) abs in the beginning of the procedure.
is there any reason not to use x <<= 1? Sure *2 is optimized in pretty much every compiler available if optimizations are enable but why not to optimize it on every compiler?
Name:
Anonymous2011-12-27 19:23
>>15
It's just silly. You will get it when you grow out of your ``I'm so low-level I shit xors'' phase.
>>17
It is straightforward when it is straightforward, i.e. when you mean a left shift of 1 bit.
Name:
Anonymous2011-12-27 19:50
>>17
Why stop there? Why not just rewrite the whole thing in METICULOUSLY HAND-OPTIMIZED ASSEMBLY to make sure that no chance to optimize is missed? Have you actually looked at the output produced by, say, gcc with -O3 from >>1? If, after that, you still think that replacing *2 by <<1 is an "optimization", we are talking about entirely different concepts.
Name:
Anonymous2011-12-27 19:55
Go dig up some Dr. Dobb's Journals from the early 90s. IIRC a semi-common way to optimize Bresenham's was to split it up into 8 different cases depending on slope.
>>19
>Why stop there?
Because <<1 is only 1 additional character and it won't take additional work/work to do it. Also if someone is suggesting a line drawing algorithm with no multiplication, it better not have multiplication in it.
>>21
If someone is doing multiplication, it better only have addition in it.
Name:
Anonymous2011-12-27 20:17
There's a slightly faster one that produces slightly uglier lines. It involves precomputing the length of each 'run' so it doesn't have to be done inside the loop.
just sayin, sometimes you have to use shitty compilers. Also, some times you might know that the input tot he function will always be positive, but the compiler might have to solve some hard shit to prove it. Or maybe the values comes from an input file and you trust that the input will always be positive. Just sayin.