Xiaolin Wu's line algorithm: Difference between revisions
Content added Content deleted
(→{{header|J}}: use isidraw in j802) |
|||
Line 591: | Line 591: | ||
const |
const |
||
FPS=1000 div 60; |
FPS = 1000 div 60; |
||
SCALE=6; |
SCALE = 6; |
||
var |
var |
||
Line 600: | Line 600: | ||
origin: TSDL_Point; |
origin: TSDL_Point; |
||
event: TSDL_Event; |
event: TSDL_Event; |
||
line_alpha: byte=255; |
line_alpha: byte = 255; |
||
procedure SDL_RenderDrawWuLine(renderer: PSDL_Renderer; x1, y1, x2, y2: longint); |
procedure SDL_RenderDrawWuLine(renderer: PSDL_Renderer; x1, y1, x2, y2: longint); |
||
Line 613: | Line 613: | ||
tmp: longint; |
tmp: longint; |
||
begin |
begin |
||
tmp:=a; |
tmp := a; |
||
a:=b; |
a := b; |
||
b:=tmp; |
b := tmp; |
||
end; |
end; |
||
begin |
begin |
||
if a=0 then |
if a = 0 then |
||
exit; |
exit; |
||
SDL_GetRenderDrawColor(renderer, @r, @g, @b, @a); |
SDL_GetRenderDrawColor(renderer, @r, @g, @b, @a); |
||
if abs(y2-y1)>abs(x2-x1) then |
if abs(y2 - y1) > abs(x2 - x1) then |
||
begin |
begin |
||
swap(x1, y1); |
swap(x1, y1); |
||
swap(x2, y2); |
swap(x2, y2); |
||
px:=@y; |
px := @y; |
||
py:=@x; |
py := @x; |
||
end |
end |
||
else |
else |
||
begin |
begin |
||
px:=@x; |
px := @x; |
||
py:=@y; |
py := @y; |
||
end; |
end; |
||
if x1>x2 then |
if x1 > x2 then |
||
begin |
begin |
||
swap(x1, x2); |
swap(x1, x2); |
||
swap(y1, y2); |
swap(y1, y2); |
||
end; |
end; |
||
x:=x2-x1; |
x := x2 - x1; |
||
if x=0 then |
if x = 0 then |
||
x:=1; |
x := 1; |
||
gradient:=(y2-y1)/x; |
gradient := (y2 - y1) / x; |
||
iy:=y1; |
iy := y1; |
||
for x:=x1 to x2 do |
for x := x1 to x2 do |
||
begin |
begin |
||
a_new:=round(a*frac(iy)); |
a_new := round(a * frac(iy)); |
||
y:=floor(iy); |
y := floor(iy); |
||
SDL_SetRenderDrawColor(renderer, r, g, b, a-a_new); |
SDL_SetRenderDrawColor(renderer, r, g, b, a-a_new); |
||
SDL_RenderDrawPoint(renderer, px^, py^); |
SDL_RenderDrawPoint(renderer, px^, py^); |
||
Line 653: | Line 653: | ||
SDL_SetRenderDrawColor(renderer, r, g, b, a_new); |
SDL_SetRenderDrawColor(renderer, r, g, b, a_new); |
||
SDL_RenderDrawPoint(renderer, px^, py^); |
SDL_RenderDrawPoint(renderer, px^, py^); |
||
iy:=iy+gradient; |
iy := iy + gradient; |
||
end; |
end; |
||
SDL_SetRenderDrawColor(renderer, r, g, b, a); |
SDL_SetRenderDrawColor(renderer, r, g, b, a); |
||
Line 660: | Line 660: | ||
begin |
begin |
||
SDL_Init(SDL_INIT_VIDEO); |
SDL_Init(SDL_INIT_VIDEO); |
||
win:=SDL_CreateWindow('Xiaolin Wu''s line algorithm', SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, |
win := SDL_CreateWindow('Xiaolin Wu''s line algorithm', SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, |
||
640, 480, SDL_WINDOW_RESIZABLE); |
640, 480, SDL_WINDOW_RESIZABLE); |
||
ren:=SDL_CreateRenderer(win, -1, 0); |
ren := SDL_CreateRenderer(win, -1, 0); |
||
if ren=NIL then |
if ren = NIL then |
||
begin |
begin |
||
writeln(SDL_GetError); |
writeln(SDL_GetError); |
||
Line 672: | Line 672: | ||
SDL_SetCursor(SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_CROSSHAIR)); |
SDL_SetCursor(SDL_CreateSystemCursor(SDL_SYSTEM_CURSOR_CROSSHAIR)); |
||
mouse_x:=0; |
mouse_x := 0; |
||
mouse_y:=0; |
mouse_y := 0; |
||
origin.x:=0; |
origin.x := 0; |
||
origin.y:=0; |
origin.y := 0; |
||
repeat |
repeat |
||
while SDL_PollEvent(@event)=1 do |
while SDL_PollEvent(@event) = 1 do |
||
case event.type_ of |
case event.type_ of |
||
SDL_KEYDOWN: |
SDL_KEYDOWN: |
||
if event.key.keysym.sym=SDLK_ESCAPE then |
if event.key.keysym.sym = SDLK_ESCAPE then |
||
halt; |
halt; |
||
SDL_MOUSEBUTTONDOWN: |
SDL_MOUSEBUTTONDOWN: |
||
begin |
begin |
||
origin.x:=mouse_x; |
origin.x := mouse_x; |
||
origin.y:=mouse_y; |
origin.y := mouse_y; |
||
end; |
end; |
||
SDL_MOUSEMOTION: |
SDL_MOUSEMOTION: |
||
with event.motion do |
with event.motion do |
||
begin |
begin |
||
mouse_x:=x div SCALE; |
mouse_x := x div SCALE; |
||
mouse_y:=y div SCALE; |
mouse_y := y div SCALE; |
||
end; |
end; |
||
SDL_MOUSEWHEEL: |
SDL_MOUSEWHEEL: |
||
line_alpha:=EnsureRange(line_alpha+event.wheel.y*20, 0, 255); |
line_alpha := EnsureRange(line_alpha + event.wheel.y * 20, 0, 255); |
||
SDL_QUITEV: |
SDL_QUITEV: |
||
halt; |
halt; |