## Introduction

We all are familiar with 2D arrays in C#, every element is accessed by 2 points.

In the same way 2D graphics has elements much like shapes, where every point has 2 coordinates, X & Y. So if you want to draw a line you'll need 2 vertices, and each vertex will have 2 coordinates on the graph. e.g. point1 = 30,10 point2 = 220,10.

This will draw a straight line.

2D graphics in WPF

WPF provides various shapes & drawings to generate 2D graphics.

We will learn shapes such as Line, Ellipse, Rectangle, Path, Polyline & Polygon.

Without further ado let's learn them one by one.

## Line

In order to draw a line in WPF we can use Line element.

It has properties such as X1,Y1 & X2,Y2. Use its X1 and Y1 properties to set its start point & X2 and Y2 properties to set its endpoint of the line.

Now you have set the points right, but we have to make the line visible as well, for that, we need to set its Stroke and StrokeThickness properties.
1. <!-- 1. Line -->
2. <Line x:Name="TwoDLine"
3.   X1="30" Y1="10"
4.   X2="220" Y2="10"
5.   Stroke="DarkGray"
6.   StrokeThickness="2"/>
Output

## Ellipse

To draw an ellipse, we must use the Ellipse element and specify its Width and Height. If you want its interior to be painted then you can use Fill property to specify the Brush. For setting up its border we can use its Stroke property to specify the Brush. The StrokeThickness property specifies the thickness of the ellipse's border.
1. <!-- 2. Ellipse -->
2. <Ellipse x:Name="TwoDEllipse"
3. Width = "75"
4. Height = "45"
5. Stroke="DarkGray"
6. Margin="10 0 0 0"
7. StrokeThickness="1">
8.     <Ellipse.Fill>
10.             <GradientStop Offset = "0" Color = "AliceBlue"/>
11.             <GradientStop Offset = "1" Color = "LightBlue"/>
12.             <GradientStop Offset = "2" Color = "DarkBlue"/>
14.     </Ellipse.Fill>
15. </Ellipse>

## Rectangle

To draw a rectangle, we have a Rectangle element and same as ellipse we can specify its Width and Height.

If you want its interior to be painted then you can use Fill property to specify the Brush. For setting up its border we can use its Stroke property to specify the Brush. The StrokeThickness property specifies the thickness of the rectangle's border.
1. <!-- 3. Rectangle -->
2.  <Rectangle x:Name="TwoDRectangle"
3.  Width="75"
4.  Height="45"
5.  Margin = "10 0 0 0"
6.  Fill="LightBlue"
7.  Stroke="DarkGray"
8.  StrokeThickness="1"/>
Output

We can also have a rectangle with rounded corners, specify RadiusX and RadiusY properties.
1. <!-- 3. Rectangle with Rounded corners -->
2.  <Rectangle x:Name="TwoDRectangle"
3.  Width="75"
4.  Height="45"
5.  Margin = "10 0 0 0"
6.  Fill="LightBlue"
7.  Stroke="DarkGray"
8.  StrokeThickness="1"
Output

## Path

It defines path from Point-A to Point-B.

WPF provides two classes: StreamGeometry and PathFigureCollection.

The following example uses attribute syntax to create a StreamGeometry.

Path with StreamGeometry,
1. <!-- 4. Path -->
2. <Path x:Name="TwoDPath"
3.   Margin = "10 0 0 0"
4.   Height = "45"
5.   Width="75"
6.   Fill = "LightBlue"
7.   Stroke="DarkGray"
8.   StrokeThickness="1"
9.   Stretch = "Fill">
10.     <Path.Data>
11.         <PathGeometry x:Name="PathGeoMetry">
12.             <PathFigure StartPoint = "50,0" IsClosed = "True">
13.                 <LineSegment Point = "100,50"/>
14.                 <LineSegment Point = "50,100"/>
15.                 <LineSegment Point = "0,50"/>
16.             </PathFigure>
17.         </PathGeometry>
18.     </Path.Data>
19. </Path>
Output

Path with PathFigureCollection,
1. <!-- 5. Path with PathFigureCollection-->
2. <Path  x:Name="TwoDPath2"
3.       Margin = "10"
4.       Height = "45"
5.       Width="75"
6.       HorizontalAlignment="Left"
7.       Stroke="DarkGray"
8.       StrokeThickness="1"
9.       Data = "M 50,150 A 120,60 50 1 0 100, 5"
10.       Stretch = "Fill"  />
Output

## Polyline

To draw a Polyline, We have a Polyline element.  Use its Points property to specify each point.

For setting up its border we can use its Stroke property to specify the Brush. The StrokeThickness property specifies the thickness of the rectangle's border.
1. <!-- 5. Polyline -->
2. <Polyline x:Name="TwoDPolyline"
3.     Points="20,50 40,100 60,50 80,100 100,50"
4.     Stroke="DarkGray"
5.     StrokeThickness="2" />
Output

## Polygon

We can draw a closed shape with a polygon.

To draw a closed shape graph also known as a connected graph, we have to use Polygon and we can use its Points property to specify its points. A line is automatically drawn as soon as you start updating each point. Then we can use Fill property to color the shape, a Stroke property for border & StorkeThickness for the size of the border.
1. <!-- 6. Polygon -->
2. <Polygon x:Name="TwoDPolygon"
3.       Stroke="DarkGray"
4.       StrokeThickness="1"
5.       Fill="LightBlue"
6.       Points="5,5 30,5 70,50 100,80 90,120 5,80"
7.       Margin = "10" />
Output

Beautiful, it looks awesome.

## Summary

In this article, we learned some of the basic shapes widely used in WPF such as Line, Ellipse, Rectangle, Path, Polyline & Closed shape like Polygon.

They are easy to use once you know where to place a point on a screen.

You can play around with them & make it more beautiful.