Title: Graph a curve in polar coordinates in C#
In Cartesian coordinates, a point is specified by X and Y values. In polar coordinates, a point is specified by r and θ where:
- r is the point's radial distance from the origin
- θ is the angle that gives the direction between the positive X axis and the point
You can use the following equations to convert from polar coordinates to Cartesian coordinates.
- X = r × Cos(θ)
- Y = r × Sin(θ)
This program graphs the equation r = 2 * Sin(5 θ).
The following Form_Load event handler does everything except actually draw the curve.
// Draw the graph.
private void Form1_Load(object sender, EventArgs e)
{
// Make the Bitmap and associated Graphics object.
Bitmap bm = new Bitmap(300, 300);
using (Graphics gr = Graphics.FromImage(bm))
{
gr.Clear(Color.White);
// Set up a transformation to map the region
// -2.1 <= X <= 2.1, -2.1 <= Y <= 2.1 onto the Bitmap.
RectangleF rect = new RectangleF(-2.1f, -2.1f, 4.2f, 4.2f);
PointF[] pts =
{
new PointF(0, bm.Height),
new PointF(bm.Width, bm.Height),
new PointF(0, 0),
};
gr.Transform = new Matrix(rect, pts);
gr.SmoothingMode = SmoothingMode.AntiAlias;
using (Pen thin_pen = new Pen(Color.Blue, 0))
{
// Draw the X and Y axes.
thin_pen.Color = Color.Blue;
gr.DrawLine(thin_pen, -2.1f, 0, 2.1f, 0);
const float big_tick = 0.1f;
const float small_tick = 0.05f;
for (float x = (int)-2.1f; x <= 2.1f; x += 1)
gr.DrawLine(thin_pen, x, -small_tick, x, small_tick);
for (float x = (int)-2.1f + 0.5f; x <= 2.1f; x += 1)
gr.DrawLine(thin_pen, x, -big_tick, x, big_tick);
gr.DrawLine(thin_pen, 0, -2.1f, 0, 2.1f);
for (float y = (int)-2.1f; y <= 2.1f; y += 1)
gr.DrawLine(thin_pen, -small_tick, y, small_tick, y);
for (float y = (int)-2.1f + 0.5f; y <= 2.1f; y += 1)
gr.DrawLine(thin_pen, -big_tick, y, big_tick, y);
// Draw the graph.
DrawGraph(gr);
}
}
// Display the result and size the form to fit.
picGraph.Image = bm;
this.ClientSize = new Size(
picGraph.Width + 2 * picGraph.Left,
picGraph.Height + 2 * picGraph.Top);
}
This code makes a Bitmap and an associated Graphics object. It clears the Bitmap and creates a transformation to center the region -2.1 ≤ {x, y} ≤ 2.1 on the Bitmap. It then creates a thin pen (otherwise the transformation makes the pen extremely thick) and draws the X and Y axes.
The code then calls the DrawGraph method described shortly to draw the graph on the Bitmap.
Next, the event handler sets the picGraph PictureBox control's Image property to the Bitmap. That control's SizeMode property is set to AutoSize so it resizes itself to fit the image. The event handler finishes by resizing the form so its client area is big enough to show the PictureBox.
The following code shows the DrawGraph method that draws the graph.
// Draw the graph on a Bitmap.
private void DrawGraph(Graphics gr)
{
// Generate the points.
double t = 0;
const double dt = Math.PI / 100.0;
const double two_pi = Math.PI;
List<PointF> points = new List<PointF>();
while (t <= two_pi)
{
double r = 2 * Math.Sin(5 * t);
float x = (float)(r * Math.Cos(t));
float y = (float)(r * Math.Sin(t));
points.Add(new PointF(x, y));
t += dt;
}
// Draw the curve.
using (Pen thin_pen = new Pen(Color.Red, 0))
{
gr.DrawPolygon(thin_pen, points.ToArray());
}
}
This code makes the variable t range from 0 to 2π. For each value of t, the method calculates r = 2 * Sin(5 t). It then converts the polar coordinates into Cartesian coordinates and adds the point to the points list.
After it has generated all of the points, the method draws them.
Download the example to experiment with it and to see additional details.
|