C# 如何在完整多边形的两点之间添加点

C# 如何在完整多边形的两点之间添加点,c#,line,polygon,point,point-in-polygon,C#,Line,Polygon,Point,Point In Polygon,我有一份要点清单。我取第一个点和下一个点(第二个点),在第一个点和第二个点之间画一条线 我想在现有点之间添加另一个顶点 这是我的方法 当用户双击该行时,应在该行添加一个点。 找到多边形生成的所有形状点。 检查e.位置是否在点列表中 如果存在,则检查该点的下限和上限。 在下边界旁边插入e.位置 用更新的列表重新绘制线条 我正在计算直线的所有点。通过使用y=mx+c 我的问题是我能得到所有垂直点x=a,一些对角线点和水平点,但不是全部。我需要知道原因是什么以及如何修复它? 我正在创建一个由ICIma

我有一份要点清单。我取第一个点和下一个点(第二个点),在第一个点和第二个点之间画一条线

我想在现有点之间添加另一个顶点

这是我的方法

  • 当用户双击该行时,应在该行添加一个点。
  • 找到多边形生成的所有形状点。
  • 检查e.位置是否在点列表中
  • 如果存在,则检查该点的下限和上限。
  • 在下边界旁边插入e.位置
  • 用更新的列表重新绘制线条

    我正在计算直线的所有点。通过使用y=mx+c

    我的问题是我能得到所有垂直点x=a,一些对角线点和水平点,但不是全部。我需要知道原因是什么以及如何修复它?
    我正在创建一个由ICImaging视频抓取程序库提供的位图覆盖。所以我只能通过整数点在屏幕上绘制

       private void btnAddPoint_Click(object sender, EventArgs e)
        {
            //IsLineSelected = true;
            Debug.Print("Current Points in List ");
            foreach (System.Drawing.Point P in PathPoints)
            {
                Debug.Print(P.ToString());
            }
            for (int IndexI = 0; IndexI != PathPoints.Count - 1; IndexI++)
            {
                if (IndexI  == PathPoints.Count-1)
                {
                    break;
                }
                else
                {
                    Debug.Print("P1:" + PathPoints[IndexI].ToString() + "P2:"+ PathPoints[IndexI+1].ToString());
                    ShapeDirectory.AllLinePoints(PathPoints[IndexI], PathPoints[IndexI + 1]);
                }
    
            }
    
            foreach (System.Drawing.Point P in ShapeDirectory.LinePoints)
            {
                PathPoints3.Add(P);
            }
            drawLinePoints = true;
    
        }        
    
        public static void AllLinePoints(Point p1, Point p2)
        {
    
            double YDiff = p2.Y - p1.Y;
            double XDiff = p2.X - p1.X;
    
            double SlopM = Math.Round(Math.Abs(YDiff / XDiff));
    
            double YinterceptB = Math.Round(Math.Abs(p1.Y - (SlopM * p1.X)));
    
            Debug.Print("Slop: " + SlopM.ToString() + "Y Intercept: " + YinterceptB.ToString());
    
            if (SlopM == 0)
            {
    
            }
    
    
            if (Double.IsNegativeInfinity(SlopM) || Double.IsPositiveInfinity(SlopM))
            {
                int LowerBoundX = 0;
                int LowerBoundY = 0;
    
                int upperBoundX;
                int upperBoundY;
    
                double distanceBetwwenP1andp2 = GetDistanceBetween2points(p1, p2);
    
                if (p1.X == p2.X )
                {
                    LowerBoundX = p1.X;
                    upperBoundX = p2.X;                   
    
                }
    
                if (p1.Y <= p2.Y)
                {
                    LowerBoundY = p1.Y;
                    upperBoundY = p2.Y;
    
                }
                else
                {
                    LowerBoundY = p2.Y;
                    upperBoundY = p1.Y;
                }
    
                //Vertical 
    
                for (int YIndex = LowerBoundY; YIndex <= upperBoundY; YIndex++)
                {
    
                            TempLinePoint.X = LowerBoundX;
                            TempLinePoint.Y = YIndex;
    
                            //Debug.Print("Current Line Points X: " + XIndex.ToString() + "Y: " + YIndex.ToString());
                            LinePoints.Add(TempLinePoint);                            
                }
            }
            else 
            {
                int LowerBoundX;
                int LowerBoundY;
    
                int upperBoundX;
                int upperBoundY;
    
                double distanceBetwwenP1andp2 = GetDistanceBetween2points(p1, p2);
    
                if (p1.X <= p2.X && p1.Y <= p2.Y)
                {
                    LowerBoundX = p1.X;
                    upperBoundX = p2.X;
    
                    LowerBoundY = p1.Y;
                    upperBoundY = p2.Y;
    
                }
                else
                {
                    LowerBoundX = p2.X;
                    upperBoundX = p1.X;
    
                    LowerBoundY = p2.Y;
                    upperBoundY = p1.Y;
    
                }
    
                //if Vertical 
                for (int YIndex = LowerBoundY; YIndex <= upperBoundY; YIndex++)
                {
    
                    for (int XIndex = LowerBoundX; XIndex <= upperBoundX; XIndex++)
                    {
                        if (YIndex == (SlopM * XIndex) + YinterceptB)
                        {
                            TempLinePoint.X = XIndex;
                            TempLinePoint.Y = YIndex;
    
                            //Debug.Print("Current Line Points X: " + XIndex.ToString() + "Y: " + YIndex.ToString());
                            LinePoints.Add(TempLinePoint);
    
                        }
                    }
                }
    
            }
    
    private void btnAddPoint_单击(对象发送者,事件参数e)
    {
    //IsLineSelected=true;
    调试.打印(“列表中的当前点”);
    foreach(路径点中的System.Drawing.Point P)
    {
    Debug.Print(P.ToString());
    }
    for(int IndexI=0;IndexI!=PathPoints.Count-1;IndexI++)
    {
    if(IndexI==PathPoints.Count-1)
    {
    打破
    }
    其他的
    {
    Debug.Print(“P1:+PathPoints[IndexI].ToString()+”P2:+PathPoints[IndexI+1].ToString());
    所有线点(路径点[IndexI],路径点[IndexI+1]);
    }
    }
    foreach(System.Drawing.Point P在ShapeDirectory.LinePoints中)
    {
    路径点3.Add(P);
    }
    drawLinePoints=真;
    }        
    公共静态无效所有线点(点p1、点p2)
    {
    双YDiff=p2.Y-p1.Y;
    双XDiff=p2.X-p1.X;
    double slop=Math.Round(Math.Abs(YDiff/XDiff));
    double-YinterceptB=Math.Round(Math.Abs(p1.Y-(SlopM*p1.X));
    Debug.Print(“Slop:+Slop.ToString()+”Y截距:“+YinterceptB.ToString());
    如果(斜率=0)
    {
    }
    if(Double.IsNegativeInfinity(SlopM)| | Double.IsPositiveInfinity(SlopM))
    {
    int LowerBoundX=0;
    int LowerBoundY=0;
    int上限x;
    int上界;
    double DistanceBetween p1和p2=获取两点之间的距离(p1,p2);
    如果(p1.X==p2.X)
    {
    LowerBoundX=p1.X;
    上限X=p2.X;
    }
    如果(p1.Y
    public static double GetAngleBetweenPoints)(点p1,点p2)
    {           
    双xDiff=p2.X-p1.X;
    双yDiff=p1.Y-p2.Y;
    双角度=数学Atan2(yDiff,xDiff)*(180/数学PI);
    如果(角度<0.0)
    角度=角度+360.0;
    返回角;
    }
    公共静态GenericList点AddingByAngle(GenericList LinePoints2)
    {
    列表角度从中心点到形状点=新建列表();
    List Linepoints3=新列表(LinePoints2);
    List LinePoints=Linepoints3.Distinct().ToList();
    点中心点=新点();
    CenterPoint.X=PolygonCenterX;
    CenterPoint.Y=多克隆中心;
    对于(int index=0;index2)
    {
    添加(路径点[0]);
    IsPathComplete=true;
    IsPointSelected=false;
    起始点=路径点[0];
    EndingPoint=PathPoints[PathPoints.Count-1];
    }
    if(IsPathComplete&&NewPointAdd)
    {
    System.Drawing.Point Centerpoint=新的System.Drawing.Point();
    路径点。添加(例如位置);
    MinMaxFinder(路径点);
    Centerpoint.X=PolygonCenterX;
    Centerpoint.Y=多克隆中心;
    lblAngle.Text=Convert.ToString(Math.Round(ShapeDirectory.GetAngleBetweenPoints(Centerpoint,e.Location));
    路径点3.Clear();
    PathPoints3=ShapeDirectory.PointAddingByAngle(路径点);
    PathPoints.Clear();
    foreach(路径点S3中的System.Drawing.Point p)
    {
    路径点。添加(p);
    }
    PathPoints.Add(PathPoints3[0]);
    NewPointAddDraw=true;
    }
    }
    
        public static double GetAngleBetweenPoints(Point p1, Point p2)
        {           
            double xDiff = p2.X - p1.X;
            double yDiff = p1.Y - p2.Y;
    
            double Angle = Math.Atan2(yDiff, xDiff) * (180 / Math.PI);
    
            if (Angle < 0.0)
                Angle = Angle + 360.0;
    
            return Angle;
        }
    
     public static GenericList<Point> PointAddingByAngle(GenericList<Point> LinePoints2)
        {
    
            List<int> AnglefromCenterPointsofShapePoint = new List<int>();
            List<Point> Linepoints3 = new List<Point>(LinePoints2);
            List<Point> LinePoints = Linepoints3.Distinct().ToList();
    
            Point CenterPoint = new Point();
            CenterPoint.X = PolygonCenterX;
            CenterPoint.Y = PolygonCenterY;            
    
            for (int index = 0; index < LinePoints.Count; index++)
            {
                AnglefromCenterPointsofShapePoint.Add((int) Math.Round(GetAngleBetweenPoints(CenterPoint, LinePoints[index])));    
            }           
    
    
            AnglefromCenterPointsofShapePoint.Sort();
    
            int currentPointAngle = 0;
    
            for (int AngleListIndex = 0; AngleListIndex < AnglefromCenterPointsofShapePoint.Count; AngleListIndex++)
            {
                currentPointAngle = AnglefromCenterPointsofShapePoint[AngleListIndex];
    
                for (int index = 0; index < LinePoints.Count; index++)
                {
                    if ((Math.Round(GetAngleBetweenPoints(CenterPoint, LinePoints[index]))) == currentPointAngle)
                    {
                        SortedPoints.Add(LinePoints[index]);
                        break;
                    }
                }
            }
    
            SortedPoints.Add(SortedPoints[0]);
            return SortedPoints;
        }
    
    
       private void pictureBox_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (PathPoints.Count > 2)
            {
                PathPoints.Add(PathPoints[0]);
                IsPathComplete = true;
                IsPointSelected = false;
                StartingPoint = PathPoints[0];
                EndingPoint = PathPoints[PathPoints.Count - 1];
            }
    
            if (IsPathComplete && NewPointAdd)
            {
    
                System.Drawing.Point Centerpoint = new System.Drawing.Point();
    
                PathPoints.Add(e.Location);
    
                MinMaxFinder(PathPoints);
    
                Centerpoint.X = PolygonCenterX;
                Centerpoint.Y = PolygonCenterY;
    
    
                lblAngle.Text = Convert.ToString(Math.Round(ShapeDirectory.GetAngleBetweenPoints(Centerpoint, e.Location)));
    
                PathPoints3.Clear();
    
                PathPoints3 = ShapeDirectory.PointAddingByAngle(PathPoints);
    
                PathPoints.Clear();
    
                foreach (System.Drawing.Point p in PathPoints3)
                {
                    PathPoints.Add(p);
                }
    
                PathPoints.Add(PathPoints3[0]);
                NewPointAddDraw = true;
            }
        }