jdleung
jdleung

Reputation: 1098

How to get a point between two points?

I'm using UIBezierPath to draw lines(in multiple angles) with two points, but I want to draw lines a little shorter than the distance between the two points.

I tried the following codes to find a point between the two points:

let x3 = x2 + 0.9 * (x1 - x2);
let y3 = y2 + 0.9 * (y1 - y2);

It works in 1 or 2 angles but fails in others. How can I get the correct point? Thanks.

=== Edited ===

enter image description here

By now I got some idea from the search, but I still cannot get it works

  1. Get the distance between the two points, and then minus 15, since I want it shorter

    let distance = sqrt(pow((p2.x - p1.x), 2) + pow((p2.y - p1.y), 2)) - 15
    
  2. Get the line angle:

    let angle = (p2.y - p1.y) / (p2.x - p1.x)
    
  3. Get point 3 with distance and angle:

    let x = p1.x + (distance * cos(angle))
    let y = p1.y - (distance * sin(angle))
    

Upvotes: 0

Views: 1719

Answers (1)

jdleung
jdleung

Reputation: 1098

It's a problem of wrong angle, function atan2 gives a correct angle value. Now the whole code work perfect.

let angle = atan2((p2.y - p1.y), (p2.x - p1.x))

Upvotes: 2

Related Questions