Chr*_*con 0 c# measurement angle winforms
我想制作一个工具,可以测量表格上两个用户定义的点之间的角度.我目前没有代码可以执行此操作,因此任何代码都将受到赞赏.
谢谢
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();
}
}
}
Run Code Online (Sandbox Code Playgroud)
而我的问题是实际将角度的大小放在我所做的名为angularize的标签中.
In *_*ico 16
要查找由三个点组成的角度,可以使用点积.假设你有三个点设置如下:
dot1
/
A /
/
/ theta
dot2-------dot3
B
Run Code Online (Sandbox Code Playgroud)
我假设您想要找到theta由点创建的线之间的角度dot1,dot2以及dot3它们是您从用户收集的点.然后,您可以定义两个向量A和B:
A = dot1 - dot2
B = dot3 - dot2
Run Code Online (Sandbox Code Playgroud)
减去两个点只意味着您减去每个相应的组件.所以在代码中可能看起来像这样:
// I'll just use another point to represent a vector
Point A = new Point();
A.X = dot1.X - dot2.X;
A.Y = dot1.Y - dot2.Y;
Point B = new Point();
B.X = dot3.X - dot2.X;
B.Y = dot3.Y - dot2.Y;
Run Code Online (Sandbox Code Playgroud)
由点积定义的这两个向量之间的角度是:
A * B
theta = acos(-----------)
||A|| ||B||
Run Code Online (Sandbox Code Playgroud)
其中||A||和||B||是矢量的长度A和B分别是组件的平方和的平方根(它只是距离公式).
double ALen = Math.Sqrt( Math.Pow(A.X, 2) + Math.Pow(A.Y, 2) );
double BLen = Math.Sqrt( Math.Pow(B.X, 2) + Math.Pow(B.Y, 2) );
Run Code Online (Sandbox Code Playgroud)
点积A * B只是组件产品的总和,因此在代码中可能如下所示:
double dotProduct = A.X * B.X + A.Y * B.Y;
Run Code Online (Sandbox Code Playgroud)
所以你可能有一个像这样定义的点积:
double theta = (180/Math.PI) * Math.Acos(dotProduct / (ALen * BLen));
Run Code Online (Sandbox Code Playgroud)
这将为您提供以度Math.Acos()为单位的角度(请记住以弧度为单位返回角度).