C# 中的角度测量器
我想制作一个可以测量表单上两个用户定义点之间角度的工具。我目前没有代码可以执行此操作,因此任何代码将不胜感激。
谢谢
更新
它需要以度为单位,我的点是3个图片框,每个图片框的三个点上都有不同的颜色来测量角度。
更新
这是我当前的新代码:
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();
}
}
}
我的问题是将角度大小实际放入我制作的称为anglesize的标签中。
I want to make a tool that can measure angles between two user defined spots on a form. I have no code to do this at the moment, so any code would be appreciated.
Thanks
UPDATE
It needs to be in Degrees and my points are 3 pictureboxes, each with different colours on each of the three points for the angle to be measured.
UPDATE
This is my new current code:
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();
}
}
}
and my problem is the line of actually putting the size of the angle in the label I have made called anglesize.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
要求三点形成的角度,您可以使用点积。假设您设置了如下三个点:
我假设您想要找到点
dot1
、dot2
创建的线之间的角度theta
和dot3
,它们是您从用户那里收集的积分。然后,您可以定义两个向量A
和B
:两个点相减仅仅意味着减去每个相应的分量。因此,它在代码中可能如下所示:
由点积定义的这两个向量之间的角度为:
其中
||A||
和||B||
是分别是向量A
和B
的长度,它是分量平方和的平方根(这只是距离公式)。点积
A * B
只是组件的乘积之和,因此它在代码中可能如下所示:因此您可能有一个如下定义的点积:
这为您提供了角度以度为单位(请记住,Math.Acos() 返回以弧度为单位的角度)。
To find the angle formed by three points, you can use the dot product. Say you have the three points set up like this:
I assume you want to find the angle
theta
between the lines created by pointsdot1
,dot2
anddot3
, where they're points that you've collected from the user. Then, you can define two vectorsA
andB
:Subtraction of two points simply means that you subtract each corresponding component. So it might look like this in code:
The angle between these two vectors as defined by the dot product is:
Where
||A||
and||B||
are the lengths of the vectorsA
andB
respectively, which is the square root of the sum of the squares of the components (which is simply the distance formula).The dot product
A * B
is simply the sum of the products of the components, so it might look like this in code:So you may perhaps have a dot product defined like this:
This gives you the angle in degrees (remember that
Math.Acos()
returns the angle in radians).与 In silico 的答案类似,您可以使用点积和叉积的组合来获取角度,而不仅仅是无向角。
其中 a 和 b 分别是从要计算角度的点到图片框角点的向量。
a*b = |a| |b| cos θ
axb = |a| |b| sin theta
axb / a*b = tan theta
atan2(axb, a*b) = theta
similar to In silico's answer, you can use a combination of a dot product and cross product to get the angle, and not just the undirected angle.
where a and b are vectors run from the point you want to calculate the angle from to the corners of your picture boxes, respectively.
a*b = |a| |b| cos theta
axb = |a| |b| sin theta
axb / a*b = tan theta
atan2(axb, a*b) = theta
嘿,这似乎是一个家庭作业问题,所以我不会直接回答,但你可以在这里找到一些东西:
http://msdn.microsoft.com/en-us/library/system.math.atan.aspx
Hey this seems like a homework question so I won't answer directly but you can find something here:
http://msdn.microsoft.com/en-us/library/system.math.atan.aspx
要测量角度,您需要三个点或一个基本方向。
可以使用 Math.Atan2(y, x)测量与 x 轴的角度。
请注意,y 是第一个参数,而不是第二个参数。与此函数的其他版本不同,x=0 是安全的。
要将以弧度给出的结果转换为度数,您需要乘以 (180/Math.PI)
To measure an angle you need three points or a base direction.
Math.Atan2(y, x) can be used to measure the angle to the x-axis.
Note that y is the first param and not the second. Unlike other versions of this function it is safe with x=0
To transform the result which is given in radians to degrees you need to multiply with (180/Math.PI)
总有
atan2(dy2, dx2) - atan2(dy1, dx1)
得到适当的按摩。There's always
atan2(dy2, dx2) - atan2(dy1, dx1)
suitably massaged.首先,您需要测量点之间的距离:
First you need to measure the distance between your points: