locked
How do i find the middle distance between two points ? RRS feed

  • Question

  • I have this method:

    private static List<Point> ExtendPoints(Point pt1, Point pt2)
            {
                List<Point> ExtendedPoints = new List<Point>();
                decimal i = (Math.Max(pt1.X, pt2.X) - Math.Min(pt1.X, pt2.X)) / 2;
    
                return ExtendedPoints;
            }


    And i call it like this:

    ExtendPoints(clouds[0], clouds[1]);


    The problem is that clouds ia List<PointF>

    And i mgetting 2 errors:

    Error 2 Argument 1: cannot convert from 'System.Drawing.PointF' to 'System.Drawing.Point'

    Error 3 Argument 2: cannot convert from 'System.Drawing.PointF' to 'System.Drawing.Point'

    What i want to do is to find the middle coordinate x,y between the two points the method get.

    Then to return a List that will contain 3 points coordinates: pt1 and pt3 and pt2 in the end.

    The List must be in this format: pt1 pt3 then pt2.

    Where pt3 is the point in the middle between pt1 and pt2. Thats why the List it return should be: pt1 pt3 pt2

    Saturday, December 28, 2013 7:30 PM

Answers

  • private static List<PointF> ExtendPoints(Point pt1, Point pt2)
    {
         float x = (Math.Max(pt1.X, pt2.X) - Math.Min(pt1.X, pt2.X)) / 2 + Math.Min(pt1.X, pt2.X);
         float y = (Math.Max(pt1.Y, pt2.Y) - Math.Min(pt1.Y, pt2.Y)) / 2 + Math.Min(pt1.Y, pt2.Y);
         var pt3 = new PointF(x,y);
         var extendedPoints = new List<PointF>{pt1, pt3, pt2};
         return extendedPoints;
    }
    
    Formula was not correct. And you can use int instead float and Poin instead PointF.
    • Marked as answer by Chocolade1972 Saturday, December 28, 2013 8:52 PM
    Saturday, December 28, 2013 8:25 PM

All replies

  • And im also not sure if this calculation to find the middle point coordinates is good:

    decimal i = (Math.Max(pt1.X, pt2.X) - Math.Min(pt1.X, pt2.X)) / 2;

    Im getting decimal i need to get a Point . Variable Point not decimal.

    Saturday, December 28, 2013 7:31 PM
  • private static List<PointF> ExtendPoints(Point pt1, Point pt2)
    {
         float x = (Math.Max(pt1.X, pt2.X) - Math.Min(pt1.X, pt2.X)) / 2 + Math.Min(pt1.X, pt2.X);
         float y = (Math.Max(pt1.Y, pt2.Y) - Math.Min(pt1.Y, pt2.Y)) / 2 + Math.Min(pt1.Y, pt2.Y);
         var pt3 = new PointF(x,y);
         var extendedPoints = new List<PointF>{pt1, pt3, pt2};
         return extendedPoints;
    }
    
    Formula was not correct. And you can use int instead float and Poin instead PointF.
    • Marked as answer by Chocolade1972 Saturday, December 28, 2013 8:52 PM
    Saturday, December 28, 2013 8:25 PM
  • Try this:

    private static List<PointF> ExtendPoints( PointF pt1, PointF pt2 )
    {
        return new List<PointF>( 3 )
            {
                pt1,
                new PointF((pt1.X + pt2.X) / 2, (pt1.Y + pt2.Y) / 2),
                pt2
            };
    }
    

    • Marked as answer by Chocolade1972 Saturday, December 28, 2013 8:52 PM
    • Unmarked as answer by Chocolade1972 Saturday, December 28, 2013 8:52 PM
    Saturday, December 28, 2013 8:26 PM
  • Its working now:

    private static List<PointF> ExtendPoints(PointF pt1, PointF pt2)

    Changed it to PointF pt1 and PointF pt2 it was Point pt1 and Point pt2


    Saturday, December 28, 2013 8:51 PM