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

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

问题描述

Idealy我想提供一系列的点,并在每个点以直角绘制一条线(从第二点开始)。

每条线的方向会交替出现,所以如果我碰巧绘制了一个由6个点组成的曲线,第二点,即在曲线交替两侧增加5条线,有点像交替腿的毛毛虫。 (我知道这些线条不会完全与曲线成直角,而是与曲线上任意两点形成的直线成直角。)(b)。 nofollow noreferrer>矢量数学。您可以通过从A中减去B来计算A和B点之间的引导矢量。在2D中,只有在2D中,通过反转x和y分量并将一个分量取为负值,可以获得与此矢量成直角的向量。如果你否定新的x分量,你会左转,通过否定y你会右转。然后,可以通过将每个分量除以向量的长度(sqrt(x x + y ))将导向向量缩小为单位大小(=长度为1)。最后,您可以根据需要的长度再次拉伸单位矢量,并获得您想要的大小之一。如果您将此矢量添加到A或B,您将得到一个要绘制线条的点。



以下是一些数学帮助:





<这些点A和B表示为矢量。





引导向量是通过一个简单的减法来计算的。 //i.stack.imgur.com/1Md6p.pngalt =>



法向量是通过翻转引导向量给出的,也就是逆向组件并使一个组件成为负面。 nl =正常,翻到左边,nr =正常,翻到右边


$ b

b



计算一个向量的长度

如果你想画一条从B到左边的线(来自A),你计算点P来画线为





所以你想在左侧画一次,在迭代时重复一次。



如果你有点你的画布外,那么你的长度可能太大了。通过计算向量BP和边界的交点,您当然可以计算出P向量将穿过边界的点。

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).

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).

解决方案

It's a question of vector mathematics. You can calculate the directing vector between two points A and B by subtracting B from A. 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.

Here's a little math help:

These are points A and B expressed as vector.

The directing vector is calculated by a simple subtraction.

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.

Calculates the length of a vector

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.

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天全站免登陆