给定 2 点,我如何画一条与两点形成的线成直角的线? [英] Given 2 points how do I draw a line at a right angle to the line formed by the two points?

查看:14
本文介绍了给定 2 点,我如何画一条与两点形成的线成直角的线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

理想情况下,我想提供一系列点,并在每个点(从第二个点开始)以直角绘制一条线.

Idealy I want to supply a sequence of points and have a line drawn at a right angle at every point (starting at the second point).

每条线的方向会交替,所以如果我碰巧画了一条由 6 个点组成的曲线,那么将从第二个点开始为每个点绘制一条给定长度的线,即在曲线,有点像双腿交替的毛毛虫.

The direction of each line would alternate, so if I happened to draw a curve cosisting of 6 points, a line of a given lenth would be drawn for each point starting with the second point, i.e 5 additional lines on alternating sides of the curve, a bit like a caterpillar with alternating legs.

(我知道这些线不会完全与曲线成直角,而是与曲线上任意两点形成的线成直角).

(I understand that the lines won't be entirely at right angles to the curve but rather at right angle to the line formed by any two points on the curve).

推荐答案

这是向量数学的问题.您可以通过从 B 中减去 A 来计算两点 A 和 B 之间的定向向量.在 2D 中并且仅在 2D 中,可以通过反转 x 和 y 分量并取一个分量为负来获得与该向量成直角的向量.如果您否定新的 x 分量,您将左转,通过否定 y,您将右转.然后,您可以通过将每个分量除以向量的长度 (sqrt(xx + yy)) 来将定向向量减小到单位大小(= 长度 1).最后,您可以按所需的长度再次拉伸单位矢量,并获得所需的尺寸之一.如果将此向量添加到 A 或 B 中,您将获得要绘制线的点.

It's a question of vector mathematics. You can calculate the directing vector between two points A and B by subtracting A from B. In 2D and only in 2D the vector right angled to this vector can be obtained by reversing x and y component and taking one component negative. If you negate the new x component you'll make a left turn, by negating y you'll make a right turn. You can then reduce the directing vector to unit size (= of length 1) by dividing each component by the length of the vector (sqrt(xx + yy)). Finally you can stretch the unit vector again by your desired length and have one of the size you want. If you add this vector to either A or B you'll get a point to which you want to draw your line.

这里有一点数学帮助:

这些是用向量表示的点 A 和 B.

These are points A and B expressed as vector.

方向向量是通过简单的减法计算出来的.

The directing vector is calculated by a simple subtraction.

法向量是通过翻转定向向量给出的,即将分量反转,使一个分量为负.nl = 正常,向左翻转,nr = 正常,向右翻转

The normal vector is given by flipping the directing vector, that is to reverse the components and make one component negative. nl = normal, flipped to the left, nr = normal, flipped to the right

法线向量的单位向量由每个分量除以向量的长度得到.

The unit vector of the normal vector is given by dividing each component by the length of the vector.

计算向量的长度

如果你想从 B 向左画一条线(当来自 A 时),你计算点 P 以将线画到 as

If you want to draw a line from B to the left (when coming from A) you calculate the point P to draw the line to as

所以你想在迭代点时交替绘制一次向左绘制一次向右绘制.

So you want to alternate that one time you draw to the left and one time to the right when iterating over the points.

如果您的点位于画布之外,那么您的长度可能太大了.您当然可以通过计算向量 BP 和边界的交点来计算到 P 的向量将越过边界的点.

If you have points lying outside your canvas, then you length is probably too large. You can of course calculate the point at which the vector to P would cross the boundary by calculating the intersection point of the vector BP and the border.

这篇关于给定 2 点,我如何画一条与两点形成的线成直角的线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆