MAIN

EN

Function graph in console

Geometric figures • Text image
06.02.2023

Let’s write an algorithm to output a graph of a function or a system of equations to the console in the form of text. We will use Java tools. For calculations, we will use the Math class, and to bypass the range of coordinates, we will use two nested for loops. We draw in the console a graph of a circle and graphs of a rhombus and a square inscribed in it.

Graph of a function with filling: Drawing heart in console.

Equations of functions #

We will check each point (x,y) from the output range of coordinates for belonging to function graphs and output in accordance with this. We define the parameters of the functions and the output range in advance before the start of bypass.

Rhombus.

Square.

Circle.

Parameters.

r — circle radius;

(a,b) — figure center;

c — half a side of a square.

Mathematical operations #

To perform basic mathematical operations, Java uses the FdLibm library — Freely Distributable Math Library. Most of the methods are implemented at the platform level to increase performance. We will refer to them through the Math class.

For floating point calculations and rounding of results, we will use the methods of the Math class.

abs(a) — absolute value of the argument a;

pow(a,b) — raising the argument a to the power of the argument b;

sqrt(a) — square root of the argument a;

ceil(a) — rounding up the argument a;

floor(a) — rounding down the argument a.

Algorithm description #

We take a range of coordinates on the plane in such a way that the displayed figure completely fits in the printing area. We bypass the selected range with two nested for loops: first along the y axis and then along the x axis. We check each point for belonging to the graphs of functions and output. For clarity, we also output the coordinate axes, the origin of coordinates and the center of the figure.

// radius, figure center, offset
int r=12, a=5, b=-1, gap=2;
// boundaries of the text image
int xMin=a-r-gap, xMax=a+r+gap;
int yMin=b-r-gap, yMax=b+r+gap;
// half a side of an inscribed square
double c = Math.ceil(r/Math.sqrt(2));
// output the title
System.out.println("Radius: "+r+"; center: 0("+a+","+b+").");
// output to the console line by line from left to right from top to bottom
for (int y = yMax; y >= yMin; y--) {
    for (int x = xMin; x <= xMax; x++) {
        // check each point for belonging to the graphs and output
        if (Math.abs(x-a) + Math.abs(y-b) == r)
            System.out.print("z "); // rhombus
        else if (Math.abs(x-a) == c && Math.abs(y-b) <= c
                || Math.abs(y-b) == c && Math.abs(x-a) <= c)
            System.out.print("* "); // square
        else if (Math.floor(Math.sqrt(Math.pow(x-a,2)+Math.pow(y-b,2))) == r)
            System.out.print("o "); // circle
        else if (y == b && x == a)
            System.out.print("0 "); // figure center
        else if (y == 0 && x == 0)
            System.out.print("+-"); // origin of coordinates
        else if (y == 0) // abscissa axis (x)
            System.out.print(x == xMax ? ">x" : "--");
        else if (x == 0) // ordinate axis (y)
            System.out.print(y == yMax ? "↑y" : "¦ ");
        else // empty place
            System.out.print("  ");
    } // transition to a new line
    System.out.println();
}

Text image in console.

Radius: 12; center: 0(5,-1).
                  ↑y                                      
                  ¦                                       
                  ¦ o o o o z o o o o                     
                o o       z   z       o o                 
            o o   ¦     z       z         o o             
          * * * * * * z * * * * * z * * * * * *           
        o *       ¦ z               z         * o         
        o *       z                   z       * o         
      o   *     z ¦                     z     *   o       
      o   *   z   ¦                       z   *   o       
    o     * z     ¦                         z *     o     
    o     z       ¦                           z     o     
    o   z *       ¦                           * z   o     
----o z --* ------+---------------------------* --z o -->x
    z     *       ¦         0                 *     z     
    o z   *       ¦                           *   z o     
    o   z *       ¦                           * z   o     
    o     z       ¦                           z     o     
    o     * z     ¦                         z *     o     
      o   *   z   ¦                       z   *   o       
      o   *     z ¦                     z     *   o       
        o *       z                   z       * o         
        o *       ¦ z               z         * o         
          * * * * * * z * * * * * z * * * * * *           
            o o   ¦     z       z         o o             
                o o       z   z       o o                 
                  ¦ o o o o z o o o o                     
                  ¦                                       
                  ¦                                       

© Golovin G.G., Code with comments, translation from Russian, 2023

Upward