PageRenderTime 76ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/scilab-master-1333395999/modules/graphics/macros/datatips/orthProj.sci

#
Unknown | 49 lines | 45 code | 4 blank | 0 comment | 0 complexity | eefdfe2bfdab54d027e44c72db1fb4fb MD5 | raw file
Possible License(s): LGPL-3.0, BSD-3-Clause
  1. // Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
  2. // Copyright (C) 2010 - INRIA - Serge Steer <serge.steer@inria.fr>
  3. //
  4. // This file must be used under the terms of the CeCILL.
  5. // This source file is licensed as described in the file COPYING, which
  6. // you should have received as part of this distribution. The terms
  7. // are also available at;
  8. // http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
  9. function [d,ptp,ind,c]=orthProj(data,pt)
  10. // computes minimum distance from a point to a polyline
  11. //d minimum distance of the point to the nearest polyline data point
  12. //ptp projected point coordiantes
  13. //ind projection lies on segment [ind ind+1]
  14. //c orthogonal projection coefficient
  15. if argn(2)<>2 then
  16. error(msprintf(_("%s: Wrong number of input argument(s): %d expected.\n"),"orthProj",2))
  17. end
  18. d = [];ptp = [];ind = [],c = []
  19. [n,m] = size(data)
  20. pt = matrix(pt,1,-1) //make pt a row vector
  21. if n<2 then return,end
  22. //the orthogonal projection coefficient of the vector y on the vector x;
  23. //is given by <x,y>/||x||^2
  24. //shift origin to (0,0) for each segment defined by data
  25. X = (data(2:$,:)-data(1:$-1,:))
  26. //apply similar origin transformation to the given point
  27. Y = ones(n-1,1)*pt-data(1:$-1,:) ;
  28. //compute the orthogonal projection coefficients relative to each segments
  29. //first remove zero length segements;
  30. L = sum(X.*X,2); //segment lengths
  31. nz = find(L>0)
  32. X = X(nz,:); Y = Y(nz,:);
  33. P = sum(X.*Y,2)./L(nz);
  34. //the projected point lies in the segment nz(i) if 0 <= P(i)<1
  35. i_in = find(P >= 0 & P<1); //find segments the projected point falls in
  36. if i_in<>[] then
  37. //find the segment that realizes the min distance
  38. [d,k] = min(sum((X(i_in,:).*(P(i_in)*ones(1,m))-Y(i_in,:)).^2,2))
  39. d = sqrt(d) //the mini distance between the given point and the curve
  40. i_in = i_in(k) //index of the first bound of the segment in data
  41. c = P(i_in) // the orthogonal projection coefficient
  42. ind = nz(i_in) //make i_in relative to the initial data
  43. ptp = data(ind,:)+(data(ind+1,:)-data(ind,:))*c //the projected point
  44. end
  45. endfunction