Understanding Cartesian Coordinates vs. Window Forms in C#
In this tutorial, we'll explore how to draw graphs in C# using the System.Drawing library.
By creating a Cartesian coordinate plane in C# Windows Forms, you can plot mathematical functions or custom data points.
Unlike charts, which focus on statistics, a C# graph drawing shows the relationship between values across the coordinate system.
The Cartesian Plane is the usual graph surface we are taught in school.
It consists of the x and y axes.
In C#, the Window's Form would represent something like a graph book;
and this graph book is plotted or drawn on using code.
By default, Windows Forms treats the top-left corner as '(0,0)', which means the Y-axis increases downward.
To make graphs behave like traditional math diagrams, you can invert the Y-axis.
Figure: Cartesian graph compared to C# Windows Form coordinate plane.
Drawing a Cartesian Plane in C# Windows Forms
To represent data clearly, we'll build a Cartesian coordinate system in C#.
The X-axis and Y-axis intersect at the origin, and we'll also add grid lines for better readability.
In C#, drawing is done using the System.Drawing library.
With C#, drawing is usually done in the paint method
of a window form or picture box.
Remember, there is a subtle difference between the way we use graphs and the C# windows form:
The y-axis of the C# windows form is measured from the top and increases as you move downwards.
In this tutorial, we've explored how to draw graphs in C# using the System.Drawing library.
By creating a Cartesian coordinate plane in C# Windows Forms, you can plot mathematical functions or custom data points.
Unlike charts, which focus on statistics, a C# graph drawing shows the relationship between values across the coordinate system.
Key Takeaways on Graphs and C# Windows Forms
Graphs in C# can be created using System.Drawing and the Paint event.
You can draw Cartesian axes in Windows Forms and invert the Y-axis for correct orientation.
By looping through values, you can plot functions in C# and visualize data clearly.
With these basics, you can expand to more advanced graph drawing in C#, including grid lines, labels, and multiple functions.
C# Graph Code for Form Class
using System; using System.Drawing; using System.Windows.Forms;
namespace Dymetric_CS
{
public partial classForm1 : Form
{ publicForm1()
{
InitializeComponent();
}
// Set a display text this.Text = "usingMaths.com";
// Set a background colour this.BackColor = System.Drawing.Color.Orange;
// Set an icon image String path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase);
path = newUri(path).LocalPath; try
{ this.Icon = newIcon(path + "\\usingMaths.ico");
} catch(System.IO.FileNotFoundException ex)
{ // Well, just go on and use default pic
}
}
}
}
Important:Get the logo image(.ico) used in the code here
and save it to the same folder(directory) as your code files.
For Visual Studio, this folder should be
C:\Users\user_name\Documents\Visual Studio 20**\Projects\Dymetric_VB\Dymetric_VB\bin\Debug.
C# Drawing Tutorial Reference
For a thorough explanation of drawing graphics in C#, please see the following links: