Get started with plotter


Get started with plotter

The “Plotter” class offers us options for the graphical representation of mathematical functions.

This enables the career changer to implement the corresponding applications early on without special knowledge. Therefore, getting started should be as simple and intuitive as possible.

In the following, we will first see the possibilities of the Plotter class using a few examples.
The Plotter class is essentially designed to draw curves. Originally, these classes were created by Stephan Euler from the University applied Sciences Mittelhessen (THM).

The classes directly belonging to plotters can be found in the "plotter" package.

So that we can access the required classes from the "plotter" package, we have to import them. How to import packages or classes can be found >> HERE <<.

Now an example:

import plotter.Graphic;

import plotter.Plotter;

public class Demo1 {
      
       public static void main(String[] args) {
             Graphic graphic = new Graphic("Demo_1");
             Plotter plotter = graphic.getPlotter();
            
             for(double x = -Math.PI; x <= Math.PI; x += 0.001) {
                    plotter.add(Math.sin(x));
             }
             graphic.repaint();
       }
}


First we define an object of the "Graphic" class. This class creates a main window and embedded in it a "plotter" object and a status line.

In addition, the "plotter" can be implemented in your own applications if necessary. However, the "plotter.jar" class must be implemented in your project.

We get the implicitly generated plotter via the "getter" method.
In the "for" loop we pass the values of the sine function to the plotter using the "add ()" method.

The entered values are therefore stored internally in pairs in the form of a table of values.
After going through the "for" loop we solve an update by calling the "repaint ()" method.

In the first example we see how to get a graphic representation with a plotter with minimal effort. The Plotter class provides a whole range of methods for this.

In our example, the numbering indicates the x range from 0 to 628. Why it 628? We plot from -pi to + pi which means that the period of the sine curve is 2pi. 2pi = 6.28. Since we enumerate in steps of 0.01, the "for" loop is run a total of 629 times.

It would be nicer to use the actual range from -pi to + pi. All we have to do is enter the associated x values when entering the values. In this case there is the version "add (double x, double y) of the method with two parameters.



Now we add the following extensions to the previous code:

1. The definition range is increased by a factor of 1.1 to -1.1pi to + 1.1pi.
2. The x and y axes are drawn.
3. A step size of 0.25 is selected for the y-axis.
4. On the x-axis we mark the points x = -pi; x = -pi / 2; x = 0; x = pi / 2; x = pi and pass the values as a field.
5. Two decimal places should be displayed for the labels on the x-axis. "Plotter" offers us this possibility with the "printf ()" method.


Second example:
import plotter.Graphic;
import plotter.Plotter;

public class Demo2 {
      
       public static void main(String[] args) {
             Graphic graphic = new Graphic("Demo_2");
             Plotter plotter = graphic.getPlotter();
            
             plotter.setXrange(-(Math.PI) * 1.1, (Math.PI) * 1.1);
             plotter.setXLine(0);
             plotter.setYLine(0);
             plotter.setAutoYgrid(0.25);
             double[] xgrid = {-Math.PI, -1, 0, 1, Math.PI};
             plotter.setXLabelFormat("%.2f");
             plotter.setXGrid(xgrid);
            
             for(double x = -Math.PI; x <= Math.PI; x += 0.001) {
                    plotter.add(x, Math.sin(x));
             }
             graphic.repaint();
       }

}


You can download both examples as a file >>HERE<<

Kommentare

Kommentar veröffentlichen

Beliebte Posts aus diesem Blog

How can I transform a .jar file to a .bat file?

Raspberry Pi als echten Web- und Mailserver einsetzen (German)

Umrechnung von Grad Celsius nach Grad Fahrenheit