There may be some smart way to do this faster and easier, but you can always hack it like this:
You know the distance formula: s = sqrt ((x2-x1) ^ 2 + (y2-y1) ^ 2). To apply this, you must find the x and y coordinates of the points where the line intersects the edges of each mesh cell. You can do this by connecting the x and y coordinates of the cell borders to the line equation and deciding for x or y respectively.
(x0, y0) (x0 + 1, y0 + 1). y (x0), y (x0 + 1), x (y0) x (y0 + 1). x y . , . , , , , , .
, , , .
, x = 2/3 * y. , , (1,0) (2,1).
x = 1, y = 2/3. 2/3 y - 0 1 - (1,2/3) , . , .
x = 2, y = 4/3. 4/3 y. , .
y = 0 x = 0. 0 x, .
y = 1, x = 3/2. 3/2 x, (3/2,1) .
, , , (1,2/3) (3/2,1). , , sqrt ((1-3/2) ^ 2 + (2/3-1) ^ 2) = sqrt (1/4 +1/9) = SQRT (13/36). .
, - : ( , , )
function y(x)
return mx+b
function x(y)
return (y-b)/m
function segLength(cellx, celly)
n=0
y1=y(cellx)
if y1>=celly and y1<=celly+1
pointx[n]=cellx
pointy[n]=y1
n=n+1
y2=y(cellx+1)
if y2>=celly and y2<=celly+1
pointx[n]=cellx+1
pointy[n]=y2
n=n+1
x1=x(celly)
if x1>=cellx and x1<=cellx+1
pointx[n]=x1
pointy[n]=celly
n=n+1
x2=x(celly+1)
if x2>=cellx and x2<=cellx+1
pointx[n]=x2
pointy[n]=celly+1
n=n+1
if n==0
return "Error: line does not intersect this cell"
else if n==2
return sqrt((pointx[0]-pointx[1])^2+(pointy[0]-pointy[1])^2)
else
return "Error: Impossible condition"
, , , .