Using bresehams line algo to iterate trhu array
Hi.
Im using line algorithm to loop trhu array. Please look few threads down for code.
[edited by - Craazer on February 13, 2003 2:19:15 PM]
I understand exactly why you might want to use Bresenham''s algorithm to iterate through an array. Its a perfect way to sort through a group of objects that are batched into the cells of a grid for the purpose of front-to-back or back-to-front rendering, e.g. when using a quadtree.
I''m confused by what you want to do with the Line() method, though. Do you mean to do something like the following?
Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.
I''m confused by what you want to do with the Line() method, though. Do you mean to do something like the following?
int x = starting x;for(int y=0; y < height;y++){ Line(x, y); array[x + y *width];}withvoid Line(int &x, int y){ // increment or decrement x, if necessary, according // to Bresenham''s algorithm. The value of x returned // to the calling function is the correct x for the // sent value of y}
Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.
Graham Rhodes Moderator, Math & Physics forum @ gamedev.net
Ok heres the codes unmodified version so it wont compile like that but u should see what im doing.
At this point the lines x and y arent needed so dont mind about them, what I need to have is lines coords and arrays up/down coords.
And litle add Im doing this becose bitmap/surface rotation.
[edited by - Craazer on January 31, 2003 4:43:16 AM]
// this is horizontal y (y1)for(y2 = 0; y2 < spr->height; ++y2){y1 = y2;rx1 = ((y1-hheight) * sinang + (x1-hwidth) * cosang);ry1 = ((y1-hheight) * cosang - (x1-hwidth) * sinang);rx1 += hwidth;ry1 += hheight;rx2 = ((y2-hheight) * sinang + (x2-hwidth) * cosang);ry2 = ((y2-hheight) * cosang - (x2-hwidth) * sinang);rx2 += hwidth;ry2 += hheight; int x = rx1; int y = ry1; int dx = rx2 - rx1; int dy = ry2 - ry1; int error=0; int x_inc,y_inc; int tx,ty=0; // x if(dx >= 0){ x_inc = 1; } // right else{ x_inc = -1; dx=-dx; } // left // y if(dy >= 0){ y_inc = 1; } // up else{ y_inc = -1; dy=-dy; } // down // dx,dy * 2 int dx2 = dx << 1; int dy2 = dy << 1; if(dx > dy) { error = cy2 - dx; for(index=0;index <= dx;index++) { if(error >= 0) { error-=dx2; y+=y_inc; } error+=dy2; x+=x_inc; // NOTE I used index as X's place // y1 is the loops y so its horizontal // if the width is 50 index isnt allways back_buffer[((spr->x+index) + (spr->y+y1) * back_lpitch)] = 0; } } // end if <= 1 else { error = dx2 - dy; for(index=0;index <= dy; index++) { if(error >= 0) { error-=dy2; x+=x_inc; } error+=dx2; y+=y_inc; // same thing here back_buffer[((spr->x+index) + (spr->y+y1) * back_lpitch)] = 0; } }} // end y2
At this point the lines x and y arent needed so dont mind about them, what I need to have is lines coords and arrays up/down coords.
And litle add Im doing this becose bitmap/surface rotation.
[edited by - Craazer on January 31, 2003 4:43:16 AM]
Please give me some time. I apologize for not replying quickly. I am, as we say in the business, "overcommitted." Too many things to do every day. Its 2AM here now on a Sunday morning, and I''m composing music for our game (I do more than math/physics!) I might not have time to look at this until next week.
Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.
Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.
Graham Rhodes Moderator, Math & Physics forum @ gamedev.net
quote:
Original post by grhodes_at_work
Please give me some time. I apologize for not replying quickly. I am, as we say in the business, "overcommitted." Too many things to do every day. Its 2AM here now on a Sunday morning, and I''m composing music for our game (I do more than math/physics!) I might not have time to look at this until next week.
Graham Rhodes
Senior Scientist
Applied Research Associates, Inc.
Allright thanks a lot man! I have been doin this rotation techniques research for half a year! Now im so close. And I hope u find the solution, im trying to figure this one out my self too, but im counting on u!

This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement