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

Posted on 2008-11-19
Medium Priority
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
  • 2
LVL 13

Expert Comment

ID: 22993855
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

jgordos earned 500 total points
ID: 22995923
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

ID: 23003102
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

ID: 23150976
I have no idea why you're closing it.

Can you supply some details?

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

579 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