C#中的角度测量仪

我想制作一个工具,可以测量表格上两个用户定义的点之间的角度。 我目前没有代码可以执行此操作,因此任何代码都将受到赞赏。

谢谢

UPDATE

它需要是以度为单位,我的点是3个图片盒,每个图片盒的三个点都有不同的颜色供测量角度。

UPDATE

这是我当前的新代码:

namespace Angle_Measurer_Tool { public partial class Form1 : Form { public Form1() { InitializeComponent(); } int Dotter = 0; private void button1_Click(object sender, EventArgs e) { Dotter = 1; } public int Distance2D(int x1, int y1, int x2, int y2) { int result = 0; double part1 = Math.Pow((x2 - x1), 2); double part2 = Math.Pow((y2 - y1), 2); double underRadical = part1 + part2; result = (int)Math.Sqrt(underRadical); return result; } private void pictureBox1_MouseClick(object sender, MouseEventArgs e) { if (Dotter == 1) { dot1.Visible = true; dot1.Location = e.Location; Dotter = 2; } else if (Dotter == 2) { dot2.Visible = true; dot2.Location = e.Location; Dotter = 3; } else if (Dotter == 3) { dot3.Visible = true; dot3.Location = e.Location; Dotter = 4; } else if (Dotter == 4) { dot1.Visible = false; dot2.Visible = false; dot3.Visible = false; Dotter = 1; } anglesize.Text = Convert .ToInt32(Distance2D( dot1.Location, dot2.Location, dot3.Location)) .ToString(); } } } 

而我的问题是实际将角度的大小放在我所做的名为angularize的标签中。

要查找由三个点组成的角度,可以使用点积 。 假设你有三个点设置如下:

  dot1 / A / / / theta dot2-------dot3 B 

我假设你想找到点dot1dot2dot3创建的线之间的角度theta ,它们是你从用户那里收集的点。 然后,您可以定义两个向量AB

 A = dot1 - dot2 B = dot3 - dot2 

减去两个点只意味着您减去每个相应的组件。 所以在代码中可能看起来像这样:

 // I'll just use another point to represent a vector Point A = new Point(); AX = dot1.X - dot2.X; AY = dot1.Y - dot2.Y; Point B = new Point(); BX = dot3.X - dot2.X; BY = dot3.Y - dot2.Y; 

由点积定义的这两个向量之间的角度是:

  A * B theta = acos(-----------) ||A|| ||B|| 

其中||A||||B|| 是矢量AB的长度,它们是分量平方和的平方根(它只是距离公式)。

 double ALen = Math.Sqrt( Math.Pow(AX, 2) + Math.Pow(AY, 2) ); double BLen = Math.Sqrt( Math.Pow(BX, 2) + Math.Pow(BY, 2) ); 

点积A * B只是组件产品的总和,因此在代码中可能如下所示:

 double dotProduct = AX * BX + AY * BY; 

所以你可能有一个像这样定义的点积:

 double theta = (180/Math.PI) * Math.Acos(dotProduct / (ALen * BLen)); 

这将为您提供以度为单位的角度(请记住Math.Acos()以弧度为单位返回角度)。

与In silico的答案类似,您可以使用点积和叉积的组合来获得角度,而不仅仅是无向角度。

其中a和b分别是从想要计算角度到图像框角落的点开始的向量。

a * b = | a | | B | cos theta

axb = | a | | B | 罪恶theta

axb / a * b = tan theta

atan2(axb,a * b)= theta

嘿,这似乎是一个家庭作业问题,所以我不会直接回答,但你可以在这里找到一些东西:

http://msdn.microsoft.com/en-us/library/system.math.atan.aspx

要测量角度,您需要三个点或一个基本方向。

Math.Atan2(y,x)可用于测量与x轴的角度。

请注意,y是第一个参数,而不是第二个参数。 与此函数的其他版本不同,x = 0时是安全的

要将以弧度给出的结果转换为度数,您需要乘以(180 / Math.PI)

总是atan2(dy2, dx2) - atan2(dy1, dx1)适当地按摩。

首先,您需要测量点之间的距离:

  public int Distance2D(int x1, int y1, int x2, int y2) { int result = 0; double part1 = Math.Pow((x2 - x1), 2); double part2 = Math.Pow((y2 - y1), 2); double underRadical = part1 + part2; result = (int)Math.Sqrt(underRadical); return result; }