iosswiftdrawinguibezierpathcashapelayer

How to calculate points on a line which is perpendicular to another line in Swift?


I'm writing code in Swift and I want to draw CAShapeLayers with BezierPaths as it's presented in the image below.

How can I calculate C & D points if points A & B are known, distance between A & B is known, and length between points C & D is also known? Also, B is exactly mid-way between C & D.

enter image description here

After C and D points are calculated, a line between them would be drawn using UIBezierPath like this:

let path: UIBezierPath = UIBezierPath()
path.move(to: CGPoint(x: Cx, y: Cy))
path.addLine(to: CGPoint(x: Dx, y: Dy))
layer.path = path.cgPath

Thanks for your help and time.


Solution

  • Here's the math that I would use :)

    First you can calculate the angle in the Cartesian plane from A to B:

    theta_AB = atan2(By-Ay, Bx-Ax)
    

    The angle of the perpendicular line CD is just theta + pi/2

    theta_CD = theta_AB + pi/2
    

    Then assume that d is the known distance between C and D.

    Then you have:

    Cx = Bx + cos(theta_CD) * d/2
    Cy = By + sin(theta_CD) * d/2
    Dx = Bx - cos(theta_CD) * d/2
    Dy = By - sin(theta_CD) * d/2
    

    So putting it all together with (hopefully) correct Swift syntax, you'd have:

    let theta_AB = atan2(By-Ay, Bx-Ax);
    let theta_CD = theta_AB + Double.pi/2.0;
    let Cx = Bx + cos(theta_CD) * d/2;
    let Cy = By + sin(theta_CD) * d/2;
    let Dx = Bx - cos(theta_CD) * d/2;
    let Dy = By - sin(theta_CD) * d/2;
    let C : CGPoint = CGPoint(x: Cx, y: Cy);
    let D : CGPoint = CGPoint(x: Dx, y: Dy);
    

    Or something like that