Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function scrBresenhamModified(x1, y1, x2, y2, grid){
- // Источник : http://eugen.dedu.free.fr/projects/bresenham/
- var ystep, xstep;
- var error;
- var errorprev;
- var _y = y1, _x = x1;
- var ddy, ddx;
- var dx = x2 - x1;
- var dy = y2 - y1;
- var arr = [];
- var width = ds_grid_width(grid);
- var height = ds_grid_height(grid);
- array_push(arr, [x1, y1]);
- if (dy < 0){
- ystep = -1;
- dy = -dy;
- }
- else {
- ystep = 1;
- }
- if (dx < 0){
- xstep = -1;
- dx = -dx;
- }
- else {
- xstep = 1;
- }
- ddy = 2 * dy; // Для точности - используем двойные значения
- ddx = 2 * dx;
- if (ddx >= ddy){
- errorprev = dx;
- error = dx; // Стартуем в центре
- for (i=0 ; i < dx ; i++){ // Не используя первую точку, т.к. уже включена
- if _y >= 0 and _y < height
- and _x >= 0 and _x < width
- and ds_grid_get(grid, _x, _y) > 0 {
- _x += xstep;
- error += ddy;
- if (error > ddx){
- _y += ystep;
- error -= ddx;
- //
- if (error + errorprev < ddx)
- and _x >= 0 and _x < width
- and _y-ystep >= 0 and _y - ystep < height
- and ds_grid_get(grid, _x, _y-ystep) > 0
- { // bottom square also
- array_push(arr, [_x, _y-ystep]);
- }
- else if (error + errorprev > ddx)
- and _y >= 0 and _y < height
- and _x-xstep >= 0 and _x - xstep < width
- and ds_grid_get(grid, _x-xstep, _y) > 0
- { // left square also
- array_push(arr, [_x-xstep, _y]);
- }
- else{ // corner: bottom and left squares also
- if _y-ystep >= 0 and _y - ystep < height
- and _y >= 0 and _y < height
- and ds_grid_get(grid, _x-xstep, _y) > 0
- {
- array_push(arr, [_x-xstep, _y]);
- }
- if _y-ystep >= 0 and _y - ystep < height
- and _x >= 0 and _x < width
- and ds_grid_get(grid, _x, _y-ystep) > 0
- {
- array_push(arr, [_x, _y-ystep]);
- }
- }
- }
- if _y >= 0 and _y < height
- and _x >= 0 and _x < width
- and ds_grid_get(grid, _x, _y) > 0
- {
- array_push(arr, [_x, _y])
- }
- errorprev = error;
- }
- }
- }
- else{ // Тоже самое
- errorprev = dy;
- error = dy;
- for (i=0 ; i < dy ; i++){
- if _y >= 0 and _y < height
- and _x >= 0 and _x < width
- and ds_grid_get(grid, _x, _y) > 0 {
- _y += ystep;
- error += ddx;
- if (error > ddy){
- _x += xstep;
- error -= ddy;
- if (error + errorprev < ddy)
- and _x-xstep >= 0 and _x - xstep < width
- and _y >= 0 and _y < height
- and ds_grid_get(grid, _x-xstep, _y) > 0
- {
- array_push(arr, [_x-xstep, _y]);
- }
- else if (error + errorprev > ddy)
- and _y-ystep >= 0 and _y - ystep < height
- and _x >= 0 and _x < width
- and ds_grid_get(grid, _x, _y-ystep) > 0
- {
- array_push(arr, [_x, _y-ystep]);
- }
- else{
- if _x-xstep >= 0 and _x - xstep < width
- and _y >= 0 and _y < height
- and ds_grid_get(grid, _x-xstep, _y) > 0
- {
- array_push(arr, [_x-xstep, _y]);
- }
- if _x >= 0 and _x < width
- and _y-ystep >= 0 and _y - ystep < height
- and ds_grid_get(grid, _x, _y-ystep) > 0
- {
- array_push(arr, [_x, _y-ystep]);
- }
- }
- }
- if _y >= 0 and _y < height
- and _x >= 0 and _x < width
- and ds_grid_get(grid, _x, _y) > 0
- {
- array_push(arr, [_x, _y])
- }
- errorprev = error;
- }
- }
- }
- return arr
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement