determine distance of origin to intersection of j-th side of polygon bb and ray [u,v,0] from origin call dist = dist_poly_ray(bb,j,u,v) bb Nx2 matrix of coordinates of polygon j number of selected side [j,j+1] u,v direction [u,v] of ray from origin wf 7/2020
0001 function dist = dist_poly_ray(bb,j,u,v) 0002 % determine distance of origin to 0003 % intersection of j-th side of polygon bb and ray [u,v,0] from origin 0004 % 0005 % call 0006 % dist = dist_poly_ray(bb,j,u,v) 0007 % bb Nx2 matrix of coordinates of polygon 0008 % j number of selected side [j,j+1] 0009 % u,v direction [u,v] of ray from origin 0010 % wf 7/2020 0011 0012 %% coordinates of j-th side of polygon 0013 a=bb(1,j); 0014 b=bb(2,j); 0015 c=bb(1,j+1); 0016 d=bb(2,j+1); 0017 %% intersection point of diagonal with j-th side of boundary 0018 % denomiator 0019 den = -u*c+u*a+v*b-v*d; 0020 % coordinates 0021 x1 = -(v*(a*d-b*c))/den; 0022 x2 = (u*(a*d-b*c))/den; 0023 %% distance 0024 dist = norm([x1,x2]); 0025 0026 end 0027