How to get Length of Graphics.DrawLines (Points) in c#

Posted on 2008-11-19
Last Modified: 2012-05-05

oGraphics = e.Gragpics
 Point[] pntLines = new Point{(323,449), (324, 449), (325, 448), (327,444), (327,443) }
            oGraphics.DrawLines(oPen, pntLines);
   oGraphics.DrawString("TestLine", Font,  Brushes.Red,  MiddleLineofPntLines?????)

How can get exactly middle of Point of pntLines.


Question by:narlasridhar
    LVL 13

    Expert Comment

    BTW I think you have used the System.Windows.Shapes.Polygon class accordingly with the Gragpics.DrawPolygon method to draw this shape. With this Polygon class you'll get some helpful properties like Height or Width...
    Anyway, as far as I know this is not a C# problem but a mathematical issue. I think you have to calculate it by yourself. I suppose that you can calculte the average X coordinates and the average of Y coordinates, don't you ?
    LVL 11

    Accepted Solution

    You're asking how to compute the centroid.

    This sounds like a homework problem, so i can't directly answer the "how"... but I can provide some tips...

    The center of a polygon is the mean of all the X,Y coordinates...

    Check the wikipedia regarding CENTROID

    But, to compute the mean, do the following

    For all the X in the list, sum the values...
    For all the Y in the list, sum the values...
    Divide by the number of point pairs in the list...



    Author Comment

    hi igordos,

    Thanks verymuch helping this solution.   Still i am looking for a Trigonometry function to do this in .net
    The way i did....

    Assuming strait line from minimum point to maximum point.  and calculating the  middle point by following formula.

       (x1 - X-max point, x2  - X-Mini Point, y1 - Y-MaxPoint, y2 - Y-Miniumum Point ) in the array...
    so MiddlePoint = ((x1-x2) / 2 + x1, (y1-y2) / 2)
    -------------------------E x a m p l e ---------------------------------------------------------------------------
                Point MinPoint = arrPoints[0];
                Point MaxPoint = arrPoints[arrPoints.Length - 1];
                Point MiddlePoint = new Point((int)(MaxPoint.X - MinPoint.X) / 2 + MinPoint.X,
                        (int)(MaxPoint.Y - MinPoint.Y) / 2 + MinPoint.Y);

                Point mMaxPoint = new Point(0, 0), mMinPoint = new Point(0, 0), mMidPoint = new Point(0, 0);
                int a = 0;
                for (int i = 0; i < arrPoints.Length; i++)
                    if (arrPoints[i].X < MiddlePoint.X && i != arrPoints.Length - 2)
                        if (a == 0)
                            mMinPoint = arrPoints[i - 1];
                            mMaxPoint = arrPoints[i - 1];
                            mMidPoint = new Point((int)(mMaxPoint.X - mMinPoint.X/2 +  mMinPoint.X) ,
                                   (int)(mMaxPoint.Y - mMinPoint.Y)/2 + mMinPoint.Y);
                Graphics g = pictureBox1.CreateGraphics();
                g.DrawLines(new Pen(Brushes.Red), arrPoints);
                g.DrawString("Test", this.Font, new SolidBrush(Color.Tomato), mMidPoint);

    LVL 11

    Expert Comment

    I have no idea why you're closing it.

    Can you supply some details?

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    In one of my recent projects, I was working with IP cameras, I need to take some pictures from the camera and do some processing on it. The first step, was to retrieve the image from camera into Image object. So that it can be displayed or …
    Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now