Search code examples
c#linepolygonpointpoint-in-polygon

How to add a point in somewhere between two points of a complete polygon


I have a list of points. I take first one and very next point (second point) and draw a line between first and second point.

I want to add another vertex in between existing points.

This is my method.

  • when user double clicks on line a point should be added there.
  • find all points of shape generated by polygon.
  • check if e.Location is in the list of points.
  • if exists then check that point's lowerbound and upperbound.
  • insert that e.Location next to lowerbound.
  • Re-draw the lines with updated list.

    I'm calculating all points of lines. by using y = mx + c.

    My problem is I'm able to get all vertical points x = a, some diagonal points and Horizontal points but not all. I need to know what is the cause and how can I fix it ?
    I'm creating a bitmap overlay provided by ICImaging video grabber library.So I'm limited to pass only integer points to draw on screen.

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

    Edit

    I have changed my method.

  • Calculate center point.
  • Add New MouseDoubleClicked e.Location to PathPoint List.
  • Find the angle of every vertex of polygon.
  • Sort list in ascending order.
  • Insert the first PointAt End of list to complete Polygon.
  • Code is in the answer.


    Solution

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