Advertisement
Guest User

Untitled

a guest
Dec 4th, 2015
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. ///nearest_point_on_path_direction( x, y, path, pathX, pathY, absolute )
  2. //
  3. // Finds the point on a path closest to an analysis point. Echoes the arguments and functionality of draw_path().
  4. // This script adds some extra functionality insofar that the script also returns
  5. //
  6. // !!! Will not give accurate results with curved paths.
  7. //
  8. // argument0: x-coordinate of the analysis point.
  9. // argument1: y-coordinate of the analysis point.
  10. // argument2: Target path.
  11. // argument3: Relative x-position of the path in the room (See draw_path).
  12. // argument4: Relative y-position of the path in the room (See draw_path).
  13. // argument5: If the path coordinates are relative or absolute (See draw_path).
  14. //
  15. // return : The angle of the path at the point that has been found, "noone" otherwise.
  16. // {{global.nearest_point_on_path_x, global.nearest_point_on_path_y}} gives the position of the closest point on the path in absolute coordinates.
  17.  
  18. var xx, yy, path, pathX, pathY, absolute;
  19. var closestLine, closestDist, closestX, closestY;
  20. var closed, points, Ax, Ay, Bx, By, A, B;
  21. var i, iLoop, dx, dy, lenSqr, t, Cx, Cy, perpDist;
  22.  
  23. xx = argument0;
  24. yy = argument1;
  25. path = argument2;
  26. pathX = argument3;
  27. pathY = argument4;
  28. absolute = argument5;
  29.  
  30. if ( !absolute ) {
  31. xx -= pathX - path_get_point_x( path, 0 );
  32. yy -= pathY - path_get_point_y( path, 0 );
  33. }
  34.  
  35. closestLine = noone;
  36. closestDist = 999999;
  37. closestX = noone;
  38. closestY = noone;
  39.  
  40. points = path_get_number( path );
  41. closed = path_get_closed( path );
  42. Bx = path_get_point_x( path, 0 );
  43. By = path_get_point_y( path, 0 );
  44.  
  45. for( i = 1; i < points + closed; i++ ) {
  46. iLoop = i mod points;
  47.  
  48. Ax = Bx;
  49. Ay = By;
  50. Bx = path_get_point_x( path, iLoop );
  51. By = path_get_point_y( path, iLoop );
  52.  
  53. dx = Bx - Ax;
  54. dy = By - Ay;
  55. lenSqr = sqr( dx ) + sqr( dy );
  56. if ( lenSqr == 0 ) t = -1 else t = dot_product( xx - Ax, yy - Ay, dx, dy ) / lenSqr;
  57.  
  58. if ( t < 0 ) {
  59. Cx = Ax;
  60. Cy = Ay;
  61. } else if ( t > 1 ) {
  62. Cx = Bx;
  63. Cy = By;
  64. } else {
  65. Cx = Ax + t * dx;
  66. Cy = Ay + t * dy;
  67. }
  68.  
  69. var perpDist = point_distance( xx, yy, Cx, Cy );
  70.  
  71. if ( perpDist < closestDist ) {
  72. closestLine = i;
  73. closestDist = perpDist;
  74. closestX = Cx;
  75. closestY = Cy;
  76. }
  77.  
  78. }
  79.  
  80. if ( closestLine != noone ) {
  81.  
  82. if ( !absolute ) {
  83. closestX += pathX - path_get_point_x( path, 0 );
  84. closestY += pathY - path_get_point_y( path, 0 );
  85. }
  86.  
  87. global.nearest_point_on_path_x = closestX;
  88. global.nearest_point_on_path_y = closestY;
  89.  
  90. var A = closestLine - 1;
  91. var B = closestLine mod points;
  92. return point_direction( path_get_point_x( path, A ), path_get_point_y( path, A ), path_get_point_x( path, B ), path_get_point_y( path, B ) );
  93.  
  94. }
  95.  
  96. return noone;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement