计算一个垂直距离的对角线偏移 [英] calculate a perpendicular offset from a diagonal line

查看:320
本文介绍了计算一个垂直距离的对角线偏移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个音乐显示程序,并需要借助两个音符之间的污点。 在嫌弃是一条曲线连接两个音符 - 仅仅是明确的。

I am writing a music display program and need to draw a 'slur' between two notes. A slur is a curved line linking two notes - just to be clear.

我知道音符的位置,并计算其中,曲线的起点和终点应该是 - 起点的 A 和终点的 B

I know the note positions and calculate where the start and end points of the curve should be - Start point A and End point B.

我现在需要得到补偿的 C ,给予二次曲线中所需的距离,以供使用。 这是我的,非常有限的数学公式的知识和理解的原因。

I now need to obtain the offset C, given the distance required, for use within a quadratic curve. This is where my, very, limited knowledge and understanding of maths formulae comes in.

我确实在这里对着所以我的答案,但提出的解决方案要么不工作,或者我太正确仅限于code它们。

I have indeed looked here in SO for my answer, but the solutions proposed either do not work or I am too limited to code them correctly.

有人可以帮助我的计算,在非数学形式

Can someone help me with the calculation, in a NON mathematical form ?

推荐答案

由于线段AB,你可以找到中间点,说的 M ,使用著名的中点公式 (A + B)/ 2 。现在,从 B 计算矢量为 A

Given the line segment AB, you can find the midpoint, say M, using the famous midpoint formula (A + B)/2. Now calculate the vector from B to A:

P =< p.x,p.y> = A - B

p = <p.x, p.y> = AB

现在 90°旋转逆时针得到垂直向量

Now rotate it by 90° counter-clockwise to get the perpendicular vector

N =&LT; n.x的,N.Y&GT; =&LT; - p.y,p.x&GT;

n = <n.x, n.y> = < ‒ p.y, p.x >

正常化它

N =&LT; n.x的,N.Y&GT; /‖n‖其中,‖n‖=√(n.x²+n.y²)是欧几里德规范或长度

n = <n.x, n.y> / ‖n‖ where ‖n‖ = √(n.x² + n.y²) is the Euclidean Norm or length

C = L(T)= <强> M + T的 N

C = L(t) = M + t n

使用这个等式(行参数形式),你可以找到任何数量以及您需要的垂直线的点。 T 是从获得的 M 点的 C 的距离。当 T = 0 ,你得到的 M 返回,当 T = 1 ,你会得到点1个单位距离的 M 以及 N 等。这也适用于对 T 负值,其中所获得的积分将在AB即对纸币的另一侧。由于 T 可以是一个十进制数,你可以用它通过改变其值来获得所获得的点所需的距离和方向玩的 M

Using this equation (parametric form of a line) you can find any number of points along the perpendicular line that you require. t is the distance of the obtained point C from M. When t = 0, you get M back, when t = 1, you get a point 1 unit away from M along n and so on. This also works for negative values of t, where the points obtained will be on the opposite side of AB i.e. towards the note. Since t can be a decimal number, you can play with it by changing its values to get the desired distance and direction of the obtained point from M.

code,因为你说你不感兴趣的数学术语;)

Code, since you said you're not interested in the math jargon ;)

vec2d calculate_perp_point(vec2d A, vec2d B, float distance)
{
   vec2d M = (A + B) / 2;
   vec2d p = A - B;
   vec2d n = (-p.y, p.x);
   int norm_length = sqrt((n.x * n.x) + (n.y * n.y));
   n.x /= norm_length;
   n.y /= norm_length;
   return (M + (distance * n));
}

这只是伪code,因为我不知道您使用的是为您的项目矢量数学库。

This is just pseudo code, since I'm not sure of the vector math library you are using for your project.

粗体变量上面是2维向量;大写字母表示点和小写的是没有位置矢量

这篇关于计算一个垂直距离的对角线偏移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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