home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 10 / 10.iso / l / l455 / 10.ddi / CONTROL.DI$ / PERPXY.M < prev    next >
Encoding:
Text File  |  1993-03-11  |  986 b   |  40 lines

  1. function [x4,y4] = perpxy(x1,y1,x2,y2,x3,y3)
  2. % PERPXY Finds a point [xp,yp] which is the nearest point from
  3. %    [x3,y3] on the straight line formed between [x1,x2] and
  4. %    [x2,y2].
  5. %
  6. %    [xp,yp]=perpxy(x1,y1,x2,y2,x3,y3)
  7. %
  8. %     Returns points [x4,y4] which are perpendicular to the 
  9. %    the straight line formed between the points
  10. %    points [x1,y1] and [x2,y2], starting from the points
  11. %    [x3,y3]: 
  12. %
  13. %                      .[x1,y1]
  14. %                          
  15. %
  16. %       
  17. %               .[x4,y4]
  18. %           .[x3,y3]
  19. %                 
  20. %
  21. %                .[x2,y2]
  22. %
  23. %     i.e. (x3-x4)*(x1-x2) + (y3-y4)*(y1-y2) = 0
  24. %            (x1-x4)*(y4-y2) - (x4-x2)*(y1-y4) = 0
  25.  
  26. %    Copyright (c) 1986-93 by the MathWorks, Inc.
  27.  
  28. % Cater  for the case when the line has infinite gradient:
  29. xeq=abs(x1-x2)<1e-10;   
  30.  
  31. x12=(x1-x2)+eps*(xeq);
  32. y12=(y1-y2);
  33.  
  34. x4=(x12.*(-x3.*x12-y3.*y12) + y12.*(x1.*y2-x2.*y1))./(-x12.*x12 - y12.*y12);
  35.  
  36. y4=(x1.*y2 + x4.*y12 - x2.*y1)./(x12);
  37.  
  38. y4=y4.*(~xeq)+y3.*(xeq);
  39.  
  40.