plonp

instance_nth_nearest

Apr 6th, 2024
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /// @func   instance_nth_nearest(x, y, obj, n)
  2. ///
  3. /// @desc   Returns the nth nearest object instance to a given point.
  4. ///         If none is found, noone is returned.
  5. ///
  6. /// @param  {real}      x           x-coordinate of point
  7. /// @param  {real}      y           y-coordinate of point
  8. /// @param  {object}    obj         object index (or all)
  9. /// @param  {real}      n           proximity
  10. ///
  11. /// @return {instance}  object instance found (or noone)
  12. ///
  13. /// GMLscripts.com/license
  14.  
  15. function instance_nth_nearest(x, y, obj, n)
  16. {
  17.     n = clamp(n, 1, instance_number(obj));
  18.     var list = ds_priority_create();
  19.     var nearest = noone;
  20.     with (obj) ds_priority_add(list, id, distance_to_point(x, y));
  21.     repeat (n) nearest = ds_priority_delete_min(list);
  22.     ds_priority_destroy(list);
  23.     return nearest;
  24. }
  25.  
Add Comment
Please, Sign In to add comment