Understanding Points Using Drawing2D in Microsoft C#

Amos Chepchieng
Mar 1, 2018 · 2 min read

You are wondering what Drawing2D is in C#, basically it is namespace under System.Drawing.Drawing2D that provides advanced two-dimensional and vector graphics functionality.

So why Points? Points represent an integer x- and y-coordinates that defines a point in a two-dimensional plane. It assists in creating polygons like Triangles, quadrilaterals, pentagons, and hexagons.

Let’s say we want to create a triangle using, first we declare our points like;

Point p0 = Point.Empty;
Point p1 = Point.Empty;
Point p2 = Point.Empty;

First thing you need to have is a rectangle to draw the points in, create a rectangle like;

//set the location of your triangle withing your ClientRectangle otherwise leave at 0 so it can draw at far left of you form
int x,y = 0
//width and height = 100
Rectangle rectangle = new Rectangle(x, y, 100, 100);

Then we initialize points with

p0 = new Point(rectangle.Left + (rectangle.Width/2), rectangle.Top);
p1 = new Point(rectangle.Left, rectangle.Bottom);
p2 = new Point(rectangle.Right, rectangle.Bottom);

Create Graphics to paint the points

Graphics graphics = this.CreateGraphics();
//use paint events if your want.
//make sure you include Smoothening Mode in your graphics for better looks like
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;SolidBrush solidbrush = new SolidBrush(Color.Black);
Pen pen = new Pen(solidbrush);

Paint the points like:

graphics.DrawPolygon(pen, new Point[] { p0, p1, p2 });

If you would wish to fill the you polygon use;

graphics.FillPolygon(solidbrush, new Point[] { p0, p1, p2 });

Result will be like

Image for post
Image for post

Here is the simplicity.

Image for post
Image for post

What if you just want to draw a V shape? This is how you hack it.

Change the location of your points to;

p0 = new Point(rectangle.Left + (rectangle.Width / 2), rectangle.Bottom);
p1 = new Point(rectangle.Left, rectangle.Top);
p2 = new Point(rectangle.Right, rectangle.Top);

So that p0 is on the bottom side then draw your polygon like;

graphics.DrawPolygon(pen, new Point[] { p0, p2 , p0, p1});


Image for post
Image for post

NB: Make sure you dispose your GDI Objects since they are unmanaged


Source : https://github.com/k33ptoo/KeepToo_Points

Thank you for your time.

Check out my Youtube Channel : https://www.youtube.com/KeepToo

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store