Angle Measurer in C#

我只是一个虾纸丫 提交于 2019-11-29 12:58:55

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. Then, you can define two vectors A and B:

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|| and ||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).

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:

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

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

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

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)

There's always atan2(dy2, dx2) - atan2(dy1, dx1) suitably massaged.

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;
  }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!