知行合一

享受过程 漠视结果

导航

根据点到已知坐标的两点的距离 获取该点坐标

    public class TowerCraneLocate
    {
        
public static Point[] GetCoordinate(int L1, int L2)
        {
            
if (L1 < L2)
            {
                
return null;
            }
            
else if (L1 == L2)
            {
                Point[] points 
= new Point[2];

                points[
0].X = L2;
                points[
0].Y = 0;
                points[
1].X = -L2;
                points[
1].Y = 0;

                
return points;
            }
            
else if (L2 == 0)
            {
                Point[] points 
= new Point[2];

                points[
0].X = 0;
                points[
0].Y = L1;
                points[
1].X = 0;
                points[
1].Y = -L1;

                
return points;
            }
            
else
            {
                Point[] points 
= new Point[4];

                
int L3 = (int)Math.Round(Math.Sqrt(L1 * L1 - L2 * L2), 0);
                points[
0].X = L2;
                points[
0].Y = L3;
                points[
1].X = -L2;
                points[
1].Y = L3;
                points[
2].X = L2;
                points[
2].Y = -L3;
                points[
3].X = -L2;
                points[
3].Y = -L3;

                
return points;
            }
        }

        
public static Point[] GetCoordinate(Point pointA, int lA, Point pointB, int lB)
        {
            
int dX = pointA.X - pointB.X;
            
int dY = pointA.Y - pointB.Y;
            
int lAB = (int)Math.Round(Math.Sqrt(dX * dX + dY * dY), 0);

            
//如果两点重合
            if ((pointA.X == pointB.X) && (pointA.Y == pointB.Y))
            {
                
return null;
            }

            
//如果距离错误
            if ((lA + lB) < lAB)
            {
                
return null;
            }

            
//如果点在直线上
            if ((lA + lB) == lAB || Math.Abs(lA - lB) == lAB)
            {
                Point[] points 
= new Point[1];
                
int ex = lA * dX / lAB;
                
int ey = lA * dY / lAB;

                
if ((lB - lA) == lAB)
                {
                    points[
0].X = pointA.X + ex;
                    points[
0].Y = pointA.Y + ey;
                }
                
else
                {
                    points[
0].X = pointA.X - ex;
                    points[
0].Y = pointA.Y - ey;
                }
                
return points;
            }
            
else
            {
                pointB.X 
-= pointA.X;
                pointB.Y 
-= pointA.Y;

                
double cita1;
                
if (pointB.X == 0)
                {
                    
if (pointB.Y > 0)
                        cita1 
= 90;
                    
else
                        cita1 
= 270;
                }
                
else if (pointB.Y == 0)
                {
                    
if (pointB.X > 0)
                        cita1 
= 0;
                    
else
                        cita1 
= 180;
                }
                
else
                {
                    cita1 
= Math.Atan(dY / (dX + 0.00001)) * 180 / Math.PI;
                    
if (pointB.X < 0)
                        cita1 
+= 180;
                }

                
double cita2 = Math.Acos((lAB * lAB + lA * lA - lB * lB) / (2 * lAB * lA * 0.99999)) * 180 / Math.PI;

                
double x1 = Math.Cos((cita1 + cita2) * Math.PI / 180* lA;
                
double y1 = Math.Sin((cita1 + cita2) * Math.PI / 180* lA;
                
double x2 = Math.Cos((cita1 - cita2) * Math.PI / 180* lA;
                
double y2 = Math.Sin((cita1 - cita2) * Math.PI / 180* lA;
                
                Point[] points 
= new Point[2];
                points[
0].X = (int)Math.Round(x1,0+ pointA.X;
                points[
0].Y = (int)Math.Round(y1,0+ pointA.Y;
                points[
1].X = (int)Math.Round(x2,0+ pointA.X;
                points[
1].Y = (int)Math.Round(y2,0+ pointA.Y;
                
return points;
            }
        }
    }

posted on 2011-02-25 08:59  项羽  阅读(747)  评论(0编辑  收藏  举报