简体   繁体   English

如何在两点之间得到一个点?

[英]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.我正在使用 UIBezierPath 用两点绘制线条(以多个角度),但我想绘制的线条比两点之间的距离短一点。

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.它可以在 1 或 2 个角度工作,但在其他角度会失败。 How can I get the correct point?我怎样才能得到正确的点? Thanks.谢谢。

=== Edited === === 编辑 ===

在此处输入图片说明

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获取两点之间的距离,然后减去 15,因为我希望它更短

    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:得到距离和角度的第 3 点:

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

It's a problem of wrong angle, function atan2 gives a correct angle value.这是角度错误的问题,函数atan2给出了正确的角度值。 Now the whole code work perfect.现在整个代码工作完美。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM