Anh minh ơi. Làm như anh nói mới là đúng. Nhưng mà tài liệu thầy cho + bài thầy giảng đều ko nói đến cái đó. Liệu làm như thế có đc ko ?
Nếu ai thích thì đọc qua:
{m <= 1.0} for x_step = 1, dy = m = yi+1 - yi -> yi+1 = yi + m
{m > 1} for y_step = 1 m = 1/dx => dx = 1/m => xi+1 = xi + 1/m
If, instead, we draw from x2 , y2 to x1, y1 then:
a.) dx = -1 yi+1 = yi -m or
b.) dy = -1 xi+1 = xi - 1/m
For a line with slopeslope < 0.0 and drawing from x1, y1 to x2, y2, i.e., left to right then:
if |m| < 1 then
let dx = 1 and yi+1 = yi + m
else {|m| ³ 1}
let dy = -1 and xi+1 = xi -1/m
if draw from x2, y2 to x1, y1 (right to left) then:
if |m| < 1 then let dx = -1 yi+1 = yi -m
else {|m| ³ 1} dy = 1 xi+1 = xi + 1/m
Complete DDA Algorithm
procedure DDA( x1, y1, x2, y2: integer);
var
dx, dy, steps: integer;
x_inc, y_inc, x, y: real;
begin
dx := x2 - x1; dy := y2 - y1;
if abs(dx) > abs(dy) then
steps := abs(dx); {steps is larger of dx, dy}
else
steps := abs(dy);
x_inc := dx/steps; y_inc := dy/steps;
{either x_inc or y_inc = 1.0, the other is the slope}
x:=x1; y:=y1;
set_pixel(round(x), round(y));
for i := 1 to steps do
begin
x := x + x_inc;
y := y + y_inc;
set_pixel(round(x), round(y));
end;
end; {DDA}