Search code examples
swiftuiimageviewuibezierpath

Swift Draw a line from center of one circle to the edge of another


I am trying to draw a UIBezierPath in swift where i can get it to go from the center of a circle to another circles center but what i really want is for the line to stop at the edge of the end circle but at the point where the line would have intersected the circles edge if the line was continuating to the center of the circle and not stopping at the edge.

I need somehow to calculate the intersection point between the line and the circle. how would i be able to do that when the circles are UIImageViews with width and height of 30 (so radius 15) and i now the centers coordinates of the two circles.

UIBezierPath (Line) going from center of one circle to edge of the other circle.


Solution

  • I have found a solution.

    From the the set of x and y (center of circles) from now called (x, y) and (x2, y2) a vector can be made that is (x2 - x, y2 - y) where x2 - x i will call a and y2 - y i will call b then the distance of the vector can be calculates as sqrt(a^2 + b^2) where the result of i will call dist.

    From this a Unit vector can be made with is (a/dist, b/dist) now all i need to do to get my new coordinates for the arrow is (x,y) as starting coordinates and (x2 + ra/dist, y2 + rb/dist) as the end coordinates where r is the radius of the circle at (x2, y2).