如何在另外两条平行线之间画一条直线垂直线?
How to draw a straight perpendicular line between 2 other parallel lines?
enter image description here
我需要使用手指的位置绘制第一条线。
稍后我需要使用手指的位置绘制第二条平行线。
我已经做到了。
主要任务是在这些平行线之间绘制第三条垂直线。
如何画第三条线?
如果您有 2 条平行线并希望在它们之间画垂直线,您将需要 1 个额外的点。假设这个点在第一行的中间(称之为C
)。
还假设我们有以下内容:
L1 // Represents the first line
L2 // Represents the second line
L1.start // Represents the line start point CGPoint
L1.end // Represents the line end point CGPoint
现在您希望绘制一条与第一条线 L1
垂直的线,为此您需要获得它的 normal
,这在 2D 中非常简单。首先通过减去给定线的起点和终点来获得线方向direction = CGPoint(x: L1.end.x-L1.start.x, y: L1.end.y-L1.start.y)
。现在要获得法线,我们只需要反转坐标并将它们除以方向长度:
let length = sqrt(direction.x*direction.x + direction.y*direction.y)
let normal = CGPoint(x: -direction.y/length, y: direction.x/length) // Yes, normal is (-D.y, D.x)
所以说起点是C
现在我们只需要在另一条线上找到终点C + normal*distanceBetweenLines
。所以我们需要最好通过点积接收到的两条线之间的距离...
首先,我们需要两条线中任意一对点之间的向量(第一条线上的一个点和第二条线上的另一个点)。所以让我们
let between = CGPoint(x: L1.start.x-L2.start.x, y: L1.start.y-L2.start.y)
现在我们需要用点积将这条线投影到法线以获得投影的长度,即两条线之间的长度
let distanceBetweenLines = between.x*normal.x + between.y*normal.y
.
所以现在我们有了所有的点,可以在两条给定的线之间画出垂直线,假设两条线是平行的:
L3.start = C
L3.end = CGPoint(x: C.x + normal.x*distanceBetweenLines, y: C.y + normal.y*distanceBetweenLines)
enter image description here 我需要使用手指的位置绘制第一条线。 稍后我需要使用手指的位置绘制第二条平行线。 我已经做到了。 主要任务是在这些平行线之间绘制第三条垂直线。 如何画第三条线?
如果您有 2 条平行线并希望在它们之间画垂直线,您将需要 1 个额外的点。假设这个点在第一行的中间(称之为C
)。
还假设我们有以下内容:
L1 // Represents the first line
L2 // Represents the second line
L1.start // Represents the line start point CGPoint
L1.end // Represents the line end point CGPoint
现在您希望绘制一条与第一条线 L1
垂直的线,为此您需要获得它的 normal
,这在 2D 中非常简单。首先通过减去给定线的起点和终点来获得线方向direction = CGPoint(x: L1.end.x-L1.start.x, y: L1.end.y-L1.start.y)
。现在要获得法线,我们只需要反转坐标并将它们除以方向长度:
let length = sqrt(direction.x*direction.x + direction.y*direction.y)
let normal = CGPoint(x: -direction.y/length, y: direction.x/length) // Yes, normal is (-D.y, D.x)
所以说起点是C
现在我们只需要在另一条线上找到终点C + normal*distanceBetweenLines
。所以我们需要最好通过点积接收到的两条线之间的距离...
首先,我们需要两条线中任意一对点之间的向量(第一条线上的一个点和第二条线上的另一个点)。所以让我们
let between = CGPoint(x: L1.start.x-L2.start.x, y: L1.start.y-L2.start.y)
现在我们需要用点积将这条线投影到法线以获得投影的长度,即两条线之间的长度
let distanceBetweenLines = between.x*normal.x + between.y*normal.y
.
所以现在我们有了所有的点,可以在两条给定的线之间画出垂直线,假设两条线是平行的:
L3.start = C
L3.end = CGPoint(x: C.x + normal.x*distanceBetweenLines, y: C.y + normal.y*distanceBetweenLines)