The above video goes away if you are a member and logged in, so log in now!

 Would you like to get all the newest Gaming News fromQJ.NET in your email each day? Want to learn more about the team who brings you the QJ news?

## Bresenham's Algorithms

This is a discussion on Bresenham's Algorithms within the Developer's Dungeon forums, part of the PSP Development Forum category; Hey - im pretty much replicating graphics library, but for TIFF SDK. Im in need of a line drawing function, ...

 Tweet
1. ## Bresenham's Algorithms

Hey - im pretty much replicating graphics library, but for TIFF SDK. Im in need of a line drawing function, so im using Bresenham's Line Drawing algorithm. Right now, it is:
Code:
```void DrawLineOnScreen(int x0, int y0, int x1, int y1, unsigned long color) {
int dy = y1 - y0;
int dx = x1 - x0;
int stepx, stepy;

if (dy < 0) { dy = -dy;  stepy = -1; } else { stepy = 1; }
if (dx < 0) { dx = -dx;  stepx = -1; } else { stepx = 1; }
dy <<= 1;                                                  // dy is now 2*dy
dx <<= 1;                                                  // dx is now 2*dx

if (dx > dy) {
int fraction = dy - (dx >> 1);                         // same as 2*dy - dx
while (x0 != x1) {
if (fraction >= 0) {
y0 += stepy;
fraction -= dx;                                // same as fraction -= 2*dx
}
x0 += stepx;
fraction += dy;                                    // same as fraction -= 2*dy
}
} else {
int fraction = dx - (dy >> 1);
while (y0 != y1) {
if (fraction >= 0) {
x0 += stepx;
fraction -= dy;
}
y0 += stepy;
fraction += dx;
}
}
}```
Right now, this crashes when executed (nothing shows either).

Anything wrong you can see here? Oh and DrawQuadOnScreen's parameters are:

int x, int y, int width, int height, unsigned long color

so, a width and height of 1 represent a pixel.

Can anyone help? Ive tried other line drawing algorithms as well, but still a crash.

2. Haven't really looked closely at the code, but it looks suspiciously like you're using integral types for an algorithm that requires accumulation of fractional error terms.

Are you using fixed point arithmetic, or just misunderstanding the algorithm?

3. Code:
```void line(int x1, int y1, int x2, int y2) {
int dx, dy, x, y, e, ix, iy, ty, dc, c;
u8 *p;

if ((x1==x2) && (y1==y2)) return;

p = (u8*)pixels + x1 + (y1<<8);
dx = x2 - x1;
dy = y2 - y1;
ix = 0;
iy = 0;
ty = 0;
c = 63 << 8;
e = 0;

if (dx < 0) {
dx = -dx;
ix = -1;
} else if (dx > 0) {
ix = 1;
}

if (dy < 0) {
dy = -dy;
iy = -256;
ty = -1;
} else if (dy > 0) {
iy = 256;
ty = 1;
}

if (dx < dy) {
dc = c/dy;
for (y=y1; y!=y2; y+=ty) {
*p = c >> 8;
c -= dc;
p += iy;
e += dx;
if (e > dy) {
e -= dy;
p += ix;
}
}
} else {
dc = c/dx;
for (x=x1; x!=x2; x+=ix) {
*p = c >> 8;
c -= dc;
p += ix;
e += dy;
if (e > dx) {
e -= dx;
p += iy;
}
}
}
}```
This is a routine im using in a small demo for the psp.
This routine plots into a 256x256 indexed color bitmap, and it starts at color index 63, ends at color index 0. Im sure you can tweak this to work with different width buffer and colors

link to demo with src: http://www.box.net/public/xe49gi2ds6

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

All times are GMT -8. The time now is 06:26 AM.

Use of this Web site constitutes acceptance of the TERMS & CONDITIONS and PRIVACY POLICY