Home > front end >  Calculating if CGPoint is on a line segment?
Calculating if CGPoint is on a line segment?

Time:11-26

I'm looking for a way to calculate if a CGPoint is on a line segment. Are there any built-in methods in Swift or any of Apple's libraries?

I don't mind converting to a different type as long as it is part of Apple's code (e.g. similar to how enter image description here


Technical notes:

  1. Depending on your needs, you can adopt different strategies for the handling of the endpoints:
  • a point past the endpoint can be considered outside the segment;

  • a point past the endpoint horizontally by more than the tolerance can be considered outside the segment;

  • a point past the endpoint obliquely by more than the tolerance can be considered outside the segment.

These options correspond to acceptance domains with respective shapes being tight rectangle, larger rectangle and rounded rectangle.

  1. This method preserves scale, making the tolerance an absolute distance criterion.
  • Related