00001
00002
#include "bresenham.h"
00003
#include "main.h"
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 void cIncremental_Bresenham::Reset(
cPoint2D & start,
cPoint2D & goal )
00014 {
00015
A = start;
00016
B = goal;
00017
00018
00019
sdx = goal.X - start.X;
00020
sdy = goal.Y - start.Y;
00021
dx = goal.X - start.X;
00022
dy = goal.Y - start.Y;
00023
00024
Px = start.X;
00025
Py = start.Y;
00026
00027
if(
dx<0)
00028 {
00029
x_inc = -1;
00030
dx=-
dx;
00031 }
00032
else
00033
x_inc=1;
00034
00035
if(
dy<0)
00036 {
00037
y_inc = -1;
00038
dy=-
dy;
00039 }
00040
else
00041
y_inc = 1;
00042
00043
00044
dx2=
dx*2;
00045
dy2=
dy*2;
00046
00047
if(dx>dy)
00048 {
00049
00050
error=
dy2-dx;
00051 }
00052
else
00053
error=
dx2-dy;
00054 }
00055
00056
00057
00058 bool cIncremental_Bresenham::Step(
int &length ,
cPoint2D & position )
00059 {
00060
bool arrived =
false;
00061
int lx = 0;
00062
int ly = 0;
00063
00064
while( lx*lx+ly*ly < length)
00065 {
00066
if(
dx>
dy)
00067 {
00068
00069
if(
error>=0)
00070 {
00071
error-=
dx2;
00072
00073
Py+=
y_inc;
00074 ly+=y_inc;
00075
00076 }
00077
00078
error+=
dy2;
00079
00080
Px+=
x_inc;
00081 lx+=x_inc;
00082 }
00083
else
00084 {
00085
00086
if(
error>=0)
00087 {
00088
error-=
dy2;
00089
00090
Px+=
x_inc;
00091 lx+=x_inc;
00092 }
00093
00094
error+=
dx2;
00095
00096
Py+=
y_inc;
00097 ly+=y_inc;
00098 }
00099 }
00100
00101
if(
sdx >=0 &&
sdy>= 0 &&
Px >=
B.
X &&
Py >=
B.
Y )
00102 arrived=
true;
00103
if(
sdx >=0 && sdy<0 && Px >=
B.
X &&
Py <=
B.
Y )
00104 arrived=
true;
00105
if( sdx <0 && sdy>= 0 && Px <=
B.
X &&
Py >=
B.
Y )
00106 arrived=
true;
00107
if(
sdx <0 && sdy< 0 && Px <=
B.
X &&
Py <=
B.
Y )
00108 arrived=
true;
00109
00110 position.
X = Px;
00111 position.
Y =
Py;
00112
return arrived;
00113 }
00114
00115
00116
00117 bool cIncremental_Bresenham::Step(
cPoint2D & position )
00118 {
00119
bool arrived =
false;
00120
int lx = 0;
00121
int ly = 0;
00122
00123
00124
if(
dx>
dy)
00125 {
00126
00127
if(
error>=0)
00128 {
00129
error-=
dx2;
00130
00131
Py+=
y_inc;
00132 ly+=y_inc;
00133
00134 }
00135
00136
error+=
dy2;
00137
00138
Px+=
x_inc;
00139 lx+=x_inc;
00140 }
00141
else
00142 {
00143
00144
if(
error>=0)
00145 {
00146
error-=
dy2;
00147
00148
Px+=
x_inc;
00149 lx+=x_inc;
00150 }
00151
00152
error+=
dx2;
00153
00154
Py+=
y_inc;
00155 ly+=y_inc;
00156 }
00157
00158
00159
00160
00161
00162
00163
if(
sdx >=0 &&
sdy>= 0 &&
Px >=
B.
X &&
Py >=
B.
Y )
00164 arrived=
true;
00165
if(
sdx >=0 && sdy<0 && Px >=
B.
X &&
Py <=
B.
Y )
00166 arrived=
true;
00167
if( sdx <0 && sdy>= 0 && Px <=
B.
X &&
Py >=
B.
Y )
00168 arrived=
true;
00169
if(
sdx <0 && sdy< 0 && Px <=
B.
X &&
Py <=
B.
Y )
00170 arrived=
true;
00171
00172 position.
X = Px;
00173 position.
Y =
Py;
00174
return arrived;
00175 }
00176