Bressenham line algorithm | ||
---|---|---|
Prev | Created by Bayou. | Next |
The algorithm seeks the fasted way to an endpoint. Because of the chosen restpoint (the middle) the left side is considered negative, in opposite of the right side. The calculation takes advantage of integer devision, as explained on the second page. It needs two motors for X and Y positioning. This algorithm is proven in Matlab and further on programmed in C.
The algorithm calculates whether, and how much, Y steps must be taken when a X step is taken. This is done through factor F, which is 0.5 or -0.5 depending on the Y position. The system has boundaries which prevent the motor rotating too much left or right. See the table below for a variable overview.
Variable | Description |
---|---|
MAX_X | Positive max X position from zero to outer right |
MAX_Y | Positive max Y position from zero to outer top. |
CUR_X | Current X position |
CUR_Y | Current Y position |
x | Incoming X position and will be formed to a useful value |
y | Incoming Y position and will be formed to a useful value |
xi | A temporary backup of the incoming X position |
yi | A temporary backup of the incoming Y position |
nx | The actual X step taken, negative or positive, in the appropriate direction |
m | The amount of Y steps |
At the beginning, the correct X and Y values will be calculated. Whenever x is higher or lower than MAX_X, x will become MAX_X or -MAX_X. This function returns X and Y coordinates in each step and takes integers. See the comments in the code below for further details.
function [coordinates] = jump(x, y, CUR_X, CUR_Y)
coordinates = [];
MAX_X = 50;
MAX_Y = 50;
% setting x to correct value
if x > MAX_X
x = MAX_X;
elseif x < 0-MAX_X
x = 0-MAX_X;
end
xi = x;
x = x - CUR_X;
CUR_X = xi;
% setting y to correct value
if y > MAX_Y
y = MAX_Y;
elseif y < 0
y = 0;
end
yi = y;
y = y - CUR_Y;
CUR_Y = yi;
nx = 1;
if x > 0
nx = -1;
end
while x ~= 0
m = floor((y / x) );
if m ~= 0
if CUR_X < 0
y = y + m;
else
y = y - m;
end
end
x = x + nx;
coordinates = [coordinates; x, y];
end
plot(coordinates(1:end, 1), coordinates(1:end, end));
end
Please see the following page for the implementation of this code.
Prev | Home | Next |
Index | Up | The implementation |