zoukankan      html  css  js  c++  java
  • 根据点到已知坐标的两点的距离 获取该点坐标

        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;
                }
            }
        }
  • 相关阅读:
    蓝牙遥控小车设计(二)——车体搭建和利用串口遥控小车
    WIN7下使用sublime text3替代arduino IDE(安装方法和所遇到的问题)
    在使用Arduino中遇到的问题(无法使用中文注释、程序无法下载)
    python 任务调度模块sched
    使用__all__限制模块可被导入对象
    python判断任务是CPU密集型还是IO密集型
    使用__slots__限制实例的属性
    使用装饰器获取被调用函数的执行的时间
    python上下文管理器
    http协议以及http1.0和http1.1的区别
  • 原文地址:https://www.cnblogs.com/bloodofhero/p/1964414.html
Copyright © 2011-2022 走看看