Graph a square curve in C#

[square curve]

This example shows how to graph a curve that draws a square. The basic idea isn’t as bizarre as you might think. You probably know that x2 + y2 = 1 is the equation for a circle. So what is the equation for a square?

If you use the similar equation xN + yN = 1 where N is an increasingly large even integer, the equation approximates a square. For example, the picture at the top of this post shows the graph of the squarish curve x10 + y10 = 1.

By making N big enough, you can make the curve approximate a square to within any given tolerance. If you set N = 100, you can’t really see a difference between the curve and a square.

You can’t really plot the equation xN + yN = 1 directly, so the program solves it for x and actually graphs the following equation.

[square curve]

If the power N is an even integer, then this equation has two real solutions: a positive root and a negative root.

When you enter a positive, even, integer power in the text box and click Graph, the program makes a Bitmap and draws axes on it. That part is relatively straightforward so it isn’t shown here. Download the example to see how it works.

The following code shows how the program draws the curve.

// Draw the curve.
private void DrawGraph(Graphics gr, Pen pen, float wxmin)
    // Get the power.
    int power = int.Parse(txtPower.Text);
    float root = 1f / power;

    // Even integer power. -1 <= x, y <= 1
    List points1 = new List();
    List points2 = new List();
    for (float x = -1f; x <= 1f; x += 0.001f)
        float y = -(float)Math.Pow(1 - Math.Pow(x, power), root);
        points1.Add(new PointF(x, y));
        points2.Add(new PointF(x, -y));

    // Combine the lists.

    // Draw the curve.
    gr.DrawPolygon(pen, points1.ToArray());

This code draws the graph of the equation shown above. It loops through values for x between -1 and 1 and uses the previous equation to find the corresponding y values.

The points that use positive y values move across the top of the square curve. The code saves them in the points1 array.

Similarly, the points that use negative y values move across the bottom of the square curve. The code saves them in the points2 array.

To easily draw the final curve, the program reverses the order of the points in the points2 array and adds those points to the end of the points1 array. The combined points now walk clockwise around the square curve.

The method finishes by drawing the points as a polygon.

Download the example to see additional details and to experiment with the program.

Note that the code only works with even, positive, integer powers.

Download Example   Follow me on Twitter   RSS feed   Donate

About RodStephens

Rod Stephens is a software consultant and author who has written more than 30 books and 250 magazine articles covering C#, Visual Basic, Visual Basic for Applications, Delphi, and Java.
This entry was posted in algorithms, drawing, mathematics and tagged , , , , , , , , , , , . Bookmark the permalink.

6 Responses to Graph a square curve in C#

  1. Annon says:

    Download 404

  2. PBdirector says:

    So that means if we divide 2 integer number we will get a floating point value. If we want the result as Integer we have to type cast it.

    • RodStephens says:

      No, it’s the other way around. If you divide two integers, C# assumes that you want the result to be an integer and it performs integer division. If you want the result to be floating point, then you need to cast at least one of the numbers into a float before you perform the division.

      More generally, C# performs operations in the data types of the most detailed variable. If one of the numbers is a float and the others are integers, it uses floats. If one of the numbers is a double and the others are integers, it uses doubles.

  3. Dimpal Mistry says:

    Can I have some c# solution with guidance to problem – say I have a graph and some square inside it. How to calculate total squares inside graph with efficient algorithsm

    • RodStephens says:

      If you mean that you want to calculate the area under the graph, then look at this example:

      Calculate the area of a polygon in C#

      Either use the technique to add up the areas under the graph or use the graph’s points and the X axis to define a polygon and then calculate its area.

      If you mean that you have some randomly and explicitly positioned squares, that’s different. If you want to know whether a square is completely below the graph, you could see if any of the segments that make up the graph cut any of the square’s edges.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.