Добавлен MpW
Перевел я на lua код, и этот код почему-то больше не работает. хотел толщину отрезка задать
отрезок из точки (0,0) в точку (512,512). чекаем точку Xc,Tc равной 0,0. дебаг совсем ничего не показывает, обрыв потока
--Находит длину перпендикуляра от отрезка, заданного Xa, Ya, Xb, Yb к точке, заданной Xc, Yc. Полезно при реализации заклинаний типа "Огненная стена", во избежание последовательных пиков юнитов по прямой.
function Perpendicular(Xa,Ya,Xb,Yb,Xc,Yc)
return ((Xa - Xc)^2 + (Ya - Yc)^2)^0.5 * math.sin(math.atan2(Yc-Ya,Xc-Xa) - math.atan2(Yb-Ya,Xb-Xa))
end
Я проверил, код работает правильно. Но в моем коде перпендикуляр вообще ничего не выдает.
код
function plotLineWidth(file,size,imagetype,x0,y0,x1,y1,wd)
x0 = CoordGride(size,x0)
y0 = CoordGride(size,y0)
x1 = CoordGride(size,x1)
y1 = CoordGride(size,y1)
local step = size/2
local Xa,Ya,Xb,Yb = x0,y0,x1,y1
local dx,dy = math.abs(x1-x0),math.abs(y1-y0)
local sx,sy
if x0 < x1 then
sx = step
else
sx = -step
end
if y0 < y1 then
sy = step
else
sy = -step
end
local err,e2,x2,y2,ed = dx-dy; -- error value e_xy
if dx+dy == 0 then
ed = 1
else
ed = (dx^2+dy^2)^0.5
end
local sum = 0
wd = (wd+1)/2
while true do --x0<x1 and y0<y1 do
print('Xa,Ya,Xb,Yb,x0,y0: ',Xa,Ya,Xb,Yb,x0,y0)
print('перпендикуляр: ',Perpendicular(Xa,Ya,Xb,Yb,x0,y0))
--if Perpendicular(Xa,Ya,Xb,Yb,x0,y0) <= wd then
putpixel(file,size,imagetype,x0,y0,math.max(0,255*(math.abs(err-dx+dy)/ed-wd+1))) -- pixel loop
--end
sum = sum + 1
e2 = err; x2 = x0;
if (2*e2 >= -dx) then --x step
y2=y0
e2=e2+dy
print('начало e2: ',e2)
while true do
if not(e2 < ed*wd and (y1 ~= y2 or dx > dy))then
break
end
print('ay')
y2=y2+sy
--if Perpendicular(Xa,Ya,Xb,Yb,x0,y2) <= wd then
putpixel(file,size,imagetype,x0,y2,math.max(0,255*(math.abs(e2)/ed-wd+1 )))
--end
sum = sum + 1
e2=e2+dx
end
if (x0 == x1)then
break;
end
e2 = err; err =err-dy; x0 = x0+sx;
end
if (2*e2 <= dy)then --y step
e2 = dx-e2
while true do
if not(e2 < ed*wd and (x1 ~= x2 or dx < dy)) then
break
end
print('bx')
x2 = x2+sx
--if Perpendicular(Xa,Ya,Xb,Yb,x2,y0) <= wd then
putpixel(file,size,imagetype,x2,y0,math.max(0,255*(math.abs(e2)/ed-wd+1 )))
--end
sum = sum + 1
e2 = e2+dy
end
if (y0 == y1)then
break;
end
err = err+dx; y0 = y0+sy;
end
end
print('sum',sum)
end
`
ОЖИДАНИЕ РЕКЛАМЫ...
Комментарии пока отсутcтвуют.
Чтобы оставить комментарий, пожалуйста, войдите на сайт.