简体   繁体   English

C#中的角度测量器

[英]Angle Measurer in C#

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. 它必须以度为单位,我的点是3个图片框,在三个要测量角度的点上每个都有不同的颜色。

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. 我的问题是实际上将角度的大小放在我制作的标签中的那条线称为anglesize。

To find the angle formed by three points, you can use the dot product . 要找到由三个点形成的角度,可以使用点积 Say you have the three points set up like this: 假设您设置了以下三点:

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

I assume you want to find the angle theta between the lines created by points dot1 , dot2 and dot3 , where they're points that you've collected from the user. 我假设您想找到由点dot1dot2dot3创建的线之间的角度theta ,它们是您从用户那里收集的点。 Then, you can define two vectors A and B : 然后,您可以定义两个向量AB

A = dot1 - dot2
B = dot3 - dot2

Subtraction of two points simply means that you subtract each corresponding component. 两点相减仅意味着您减去每个对应的分量。 So it might look like this in code: 因此,在代码中可能看起来像这样:

// 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;

The angle between these two vectors as defined by the dot product is: 点积定义的两个向量之间的夹角为:

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

Where ||A|| ||A|| and ||B|| ||B|| are the lengths of the vectors A and B respectively, which is the square root of the sum of the squares of the components (which is simply the distance formula). 分别是向量AB的长度,它是分量平方和的平方根(即距离公式)。

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

The dot product A * B is simply the sum of the products of the components, so it might look like this in code: 点积A * B只是组件乘积的总和,因此在代码中可能看起来像这样:

double dotProduct = A.X * B.X + A.Y * B.Y;

So you may perhaps have a dot product defined like this: 因此,您可能定义了这样的点积:

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

This gives you the angle in degrees (remember that Math.Acos() returns the angle in radians). 这为您提供了以度为单位的角度(请记住Math.Acos()返回以弧度为单位的角度)。

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. 与In silico的答案类似,您可以使用点积和叉积的组合来获取角度,而不仅仅是不定向角度。

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 = |a| a * b = | a | |b| | b | cos theta cos theta

axb = |a| axb = | a | |b| | b | sin theta 罪孽

axb / a*b = tan theta axb / a * b = tan theta

atan2(axb, a*b) = theta atan2(axb,a * b)= theta

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. Math.Atan2(y,x)可用于测量与x轴的角度。

Note that y is the first param and not the second. 请注意,y是第一个参数,而不是第二个。 Unlike other versions of this function it is safe with x=0 与该函数的其他版本不同,x = 0时很安全

To transform the result which is given in radians to degrees you need to multiply with (180/Math.PI) 要将以弧度表示的结果转换为度数,您需要乘以(180 / Math.PI)

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 http://msdn.microsoft.com/en-us/library/system.math.atan.aspx

总是有适当按摩过的atan2(dy2, dx2) - atan2(dy1, dx1)

First you need to measure the distance between your points: 首先,您需要测量点之间的距离:

    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;
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM