Click here to Skip to main content
15,884,628 members
Articles / Programming Languages / C#
Tip/Trick

Extension methods for finding centers of a rectangle

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
13 Jun 2012CPOL 17K   98   5
It's very easy to work out the center of a rectangle, but it's messy as inline code. Hence these simple extension methods to provide the five centers.

Introduction

I wanted to add a thin, grey line down the middle of a splitter control split handle (the bar down the middle that separates the two panels and lets the user change the ratio displayed). So, I needed the center of the Splitter rectangle:

C#
private void splitContainer_Paint(object sender, PaintEventArgs e)
    {
    SplitContainer sc = sender as SplitContainer;
    if (sc != null)
        {
        Graphics g = e.Graphics;
        if (sc.Orientation == Orientation.Vertical)
            {
            g.DrawLine(Pens.LightGray,
                       (sc.SplitterRectangle.Left + sc.SplitterRectangle.Right) / 2,
                       sc.SplitterRectangle.Top,
                       (sc.SplitterRectangle.Left + sc.SplitterRectangle.Right) / 2,
                       sc.SplitterRectangle.Bottom);
            }
        else
            {
            g.DrawLine(Pens.LightGray,
                       sc.SplitterRectangle.Left,
                       (sc.SplitterRectangle.Top + sc.SplitterRectangle.Bottom) / 2,
                       sc.SplitterRectangle.Right,
                       (sc.SplitterRectangle.Top + sc.SplitterRectangle.Bottom) / 2);
            }
        }
    }
But that is messy, and difficult to read.

Background 

A much more readable version would be:

C#
private void splitContainer_Paint(object sender, PaintEventArgs e)
    {
    SplitContainer sc = sender as SplitContainer;
    if (sc != null)
        {
        Graphics g = e.Graphics;
        if (sc.Orientation == Orientation.Vertical)
            {
            g.DrawLine(Pens.LightGray, sc.SplitterRectangle.CenterTop(), sc.SplitterRectangle.CenterBottom());
            }
        else
            {
            g.DrawLine(Pens.LightGray, sc.SplitterRectangle.CenterLeft(), sc.SplitterRectangle.CenterRight());
            }
        }
    }
All I had to do was add some static methods to my StaticMethods utility controls class:
C#
using System.Drawing;

namespace UtilityControls
    {
    /// <summary>
    /// Contains simple, tested static methods for generic use.
    /// </summary>
    public static class StaticMethods
        {
        /// <summary>
        /// Returns the center point of the rectangle
        /// </summary>
        /// <param name="r"></param>
        /// <returns>Center point of the rectangle</returns>
        public static Point Center(this Rectangle r)
            {
            return new Point((r.Left + r.Right) / 2, (r.Top + r.Bottom) / 2);
            }
        /// <summary>
        /// Returns the center right point of the rectangle
        /// i.e. the right hand edge, centered vertically.
        /// </summary>
        /// <param name="r"></param>
        /// <returns>Center right point of the rectangle</returns>
        public static Point CenterRight(this Rectangle r)
            {
            return new Point(r.Right, (r.Top + r.Bottom) / 2);
            }
        /// <summary>
        /// Returns the center left point of the rectangle
        /// i.e. the left hand edge, centered vertically.
        /// </summary>
        /// <param name="r"></param>
        /// <returns>Center left point of the rectangle</returns>
        public static Point CenterLeft(this Rectangle r)
            {
            return new Point(r.Left, (r.Top + r.Bottom) / 2);
            }
        /// <summary>
        /// Returns the center bottom point of the rectangle
        /// i.e. the bottom edge, centered horizontally.
        /// </summary>
        /// <param name="r"></param>
        /// <returns>Center bottom point of the rectangle</returns>
        public static Point CenterBottom(this Rectangle r)
            {
            return new Point((r.Left + r.Right) / 2, r.Bottom);
            }
        /// <summary>
        /// Returns the center top point of the rectangle
        /// i.e. the topedge, centered horizontally.
        /// </summary>
        /// <param name="r"></param>
        /// <returns>Center top point of the rectangle</returns>
        public static Point CenterTop(this Rectangle r)
            {
            return new Point((r.Left + r.Right) / 2, r.Top);
            }
        }
    }

Using the code

Just add the file and use the new Rectangle.Center, Rectangle.CenterLeft and so forth methods. 

History

Keep a running update of any changes or improvements you've
made here.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
CEO
Wales Wales
Born at an early age, he grew older. At the same time, his hair grew longer, and was tied up behind his head.
Has problems spelling the word "the".
Invented the portable cat-flap.
Currently, has not died yet. Or has he?

Comments and Discussions

 
GeneralMy 5! Pin
Maciej Los18-Oct-14 5:53
mveMaciej Los18-Oct-14 5:53 
GeneralRe: My 5! Pin
OriginalGriff18-Oct-14 6:02
mveOriginalGriff18-Oct-14 6:02 
GeneralMy vote of 5 Pin
johannesnestler13-Jun-12 4:00
johannesnestler13-Jun-12 4:00 
GeneralRe: My vote of 5 Pin
OriginalGriff13-Jun-12 4:46
mveOriginalGriff13-Jun-12 4:46 
GeneralRe: My vote of 5 Pin
johannesnestler13-Jun-12 7:58
johannesnestler13-Jun-12 7:58 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.