Monthly Archives: February 2015

Plotting a sine graph in text mode

The purpose of this post is to sinedemonstrate plotting of graphs in text mode using printable characters. I would be taking a simple case in which the x-axis is aligned vertically, like shown in the adjoining figure, because cursor cannot move up in the text mode. However, the same can be aligned horizontally by using a double dimension array to first store the plot and then display it using nested loops, but I am going to keep that for some other post.

I would be giving two implementation one in which the asterisk would represent the graph (unshaded sine graph) and the other in which the space between asterisk and the axis would be filled with the character ‘=’ to give it a shaded appearance, like shown in the figure above.

The function to determine the sine of an angle in radians in Java is Math.sine( ). Math.sine() returns a value between -1 and +1 (both inclusive) depending on the argument. In order to get a nice plot we will leave five columns of space from both sides. Let us assume that we want to plot from column 5 to column 80 only. So, the problem is essentially to scale the value obtained from Math.sin() between 5 and 80. This can be achieved easily using the following formula.

y prime = c1+{(y-ymin)/(ymax-ymin)}*(c2-c1)
where
y’ is the scaled value
c1 and c2 are the starting and ending columns
y is equal to sine(x)
ymax is the maximum possible value of sine(x) i.e. +1
ymin is the minimum possible value of sine(x) i.e. -1

The program to implement the above logic of plotting a sine graph in text mode is as follows:

class Graph {
	public static void generateSine() {
		int scaledValue, fromColumn = 5, toColumn = 80, minima = -1, maxima = 1, 
		midColumn;
		midColumn = (fromColumn + toColumn) / 2;
		for (float x = 0; x <= (float) (2 * Math.PI); x += 0.20) {
			scaledValue = (int) (fromColumn + (Math.sin(x) - minima)
					/ (maxima - minima) * (toColumn - fromColumn));
			for (int leadingSpace =1;leadingSpace<=fromColumn;leadingSpace++) {
				System.out.print(" ");
			}
			for (int col = fromColumn; col <= toColumn; col++) {
				if (col == scaledValue)
					System.out.print("*");
				else
					if (col == midColumn)
						System.out.print("|");
					else
						System.out.print(" ");
			}
			System.out.println();
		}
	}
	public static void generateShadedSine() {
		int scaledValue, fromColumn = 5, toColumn = 80, minima = -1, 
		maxima = 1, midColumn;
		midColumn = (fromColumn + toColumn) / 2;
		for (float x = 0; x <= (float) (2 * Math.PI); x += 0.20) {
			scaledValue = (int) (fromColumn + (Math.sin(x) - minima)
					/ (maxima - minima) * (toColumn - fromColumn));
			for (int leadingSpace = 1; leadingSpace <= fromColumn; leadingSpace++) {
				System.out.print(" ");
			}
			for (int col = fromColumn; col <= toColumn; col++) { 				if (col == scaledValue) 					System.out.print("*"); 				else 					if (col == midColumn) 						System.out.print("|"); 					else 						if(scaledValue>midColumn && col>midColumn && col<scaledValue)
						System.out.print("=");
						else if(scaledValuescaledValue) System.out.print("=");
						else System.out.print(" ");
			}
			System.out.println();
		}
	}
	public static void main(String args[]){
		generateSine();
		generateShadedSine();
	}

}

The output of the above program would display one unshaded sine wave and one shaded sine wave as follows:

                                          *                                      
                                          |      *                               
                                          |              *                       
                                          |                    *                 
                                          |                          *           
                                          |                               *      
                                          |                                  *   
                                          |                                    * 
                                          |                                    * 
                                          |                                    * 
                                          |                                 *    
                                          |                             *        
                                          |                        *             
                                          |                  *                   
                                          |            *                         
                                          |    *                                 
                                        * |                                      
                                *         |                                      
                         *                |                                      
                   *                      |                                      
              *                           |                                      
         *                                |                                      
      *                                   |                                      
     *                                    |                                      
     *                                    |                                      
      *                                   |                                      
         *                                |                                      
             *                            |                                      
                  *                       |                                      
                         *                |                                      
                                *         |                                      
                                       *  |                                      
                                          *                                      
                                          |======*                               
                                          |==============*                       
                                          |====================*                 
                                          |==========================*           
                                          |===============================*      
                                          |==================================*   
                                          |====================================* 
                                          |====================================* 
                                          |====================================* 
                                          |=================================*    
                                          |=============================*        
                                          |========================*             
                                          |==================*                   
                                          |============*                         
                                          |====*                                 
                                        *=|                                      
                                *=========|                                      
                         *================|                                      
                   *======================|                                      
              *===========================|                                      
         *================================|                                      
      *===================================|                                      
     *====================================|                                      
     *====================================|                                      
      *===================================|                                      
         *================================|                                      
             *============================|                                      
                  *=======================|                                      
                         *================|                                      
                                *=========|                                      
                                       *==|