Plotting functions » Histórico » Versión 4
Federico Vera, 2018-07-09 19:32
1 | 1 | Federico Vera | # Plotting functions |
---|---|---|---|
2 | There are several ways of plotting mathematical functions with `jdrawinglib`, but the easiest way is to use `Calculable` and `GFormula`. |
||
3 | |||
4 | All the code examples assume you are using the [[Examples#usage-template|usage template]]. |
||
5 | |||
6 | 4 | Federico Vera | ## Import the classes |
7 | 1 | Federico Vera | ```java |
8 | import com.dkt.graphics.exceptions.DomainException; |
||
9 | import com.dkt.graphics.extras.GFormula; |
||
10 | import com.dkt.graphics.extras.formula.Calculable; |
||
11 | ``` |
||
12 | 4 | Federico Vera | ## Create a new `Calculable` object and implement `f(x)` |
13 | 1 | Federico Vera | In our case we are implementing a simple `sin(x)`, since all the real numbers are a part of the domain of `sin(x)` we won't bother with the `DomainException`. |
14 | ```java |
||
15 | Calculable calculable = new Calculable() { |
||
16 | @Override |
||
17 | public double f(double x) throws DomainException { |
||
18 | return Math.sin(x); |
||
19 | } |
||
20 | }; |
||
21 | ``` |
||
22 | Since the image of `sin(x)` is the interval `[-1, 1]` and the domain of a period is `[0, 2π]` the height will be just two pixels and the width ~6 pixels, so in order to make it look nice, we'll need something like: |
||
23 | ```java |
||
24 | calculable.setScaleX(40); |
||
25 | calculable.setScaleY(60); |
||
26 | ``` |
||
27 | |||
28 | 4 | Federico Vera | ## Create a `GFormula` and calculate it |
29 | 1 | Federico Vera | `Calculable` objects are just a container for `f(x)` and it's scales, but the real calculation is done by `GFormula`. This class provides some methods to calculate paths, areas and points. |
30 | ```java |
||
31 | GFormula formula = new GFormula(calculable); |
||
32 | // Paints should be set before calling the calculate methods |
||
33 | // I use -π to π so the function is centered |
||
34 | formula.calculatePath(-Math.PI, Math.PI, 0.01); |
||
35 | canvas.add(formula); |
||
36 | ``` |
||
37 | |||
38 | 4 | Federico Vera | ## Beautify it |
39 | 1 | Federico Vera | This isn't actually necessary, but it does give a nice effect. First we'll create a `GAxis` object and add it to the canvas, since the axis should really be behind the graph, this could be achieved by adding the axis first, or simply adding the axis to the background of the canvas with |
40 | `canvas.addFixed(GraphicE)`. |
||
41 | ```java |
||
42 | GAxis axis = new GAxis(-300, 300, -300, 300); |
||
43 | axis.drawLinesH(true); |
||
44 | axis.drawLinesV(true); |
||
45 | canvas.addFixed(axis); |
||
46 | ``` |
||
47 | 3 | Federico Vera | And the final result should be something like this (the final code is [here `->` attachment:Plotting.java): |
48 | 1 | Federico Vera | |
49 | ![Screenshot](sin.png) |
||
50 | |||
51 | 4 | Federico Vera | ## Beautify it some more |
52 | 1 | Federico Vera | In the following code we'll add some color to the graph, and paint an area changing the stroke used to show the area. in order to do this we must set the `Paint` before calculating the path. So the code will end up: |
53 | ```java |
||
54 | GFormula formula = new GFormula(calculable); |
||
55 | formula.setPaint(Color.ORANGE); // <-- |
||
56 | formula.calculatePath(-Math.PI, Math.PI, 0.01); |
||
57 | ``` |
||
58 | Now we need to calculate the area, lets say we want to color the area in the interval `(1, 2)` to that end we need a new `GFormula` object, since the idea is making it look nice, lets put a transparent color on the area, and a dashed `Stroke`. The code will be something like: |
||
59 | |||
60 | ```java |
||
61 | import com.dkt.graphics.utils.Utils; |
||
62 | import java.awt.BasicStroke; |
||
63 | import java.awt.Color; |
||
64 | import java.awt.Stroke; |
||
65 | ``` |
||
66 | ```java |
||
67 | // This variables aren't really necessary, but the code |
||
68 | // needs to be short for the wiki. |
||
69 | final int cap = BasicStroke.CAP_ROUND; |
||
70 | final int join = BasicStroke.JOIN_ROUND; |
||
71 | final float[] dash = new float[] {5, 5}; |
||
72 | Stroke stroke = new BasicStroke(1, cap, join, 0, dash, 0); |
||
73 | // This creates a new color with 32 as the alpha value |
||
74 | Color background = Utils.getColorWithAlpha(Color.GREEN, 32); |
||
75 | GFormula area = new GFormula(calculable); |
||
76 | area.setAreaPaint(background); |
||
77 | area.setPaint(Color.GREEN); |
||
78 | area.setStroke(stroke); |
||
79 | // Calculate the area in the interval |
||
80 | area.calculateArea(1, 2, 0.01); |
||
81 | ``` |
||
82 | 3 | Federico Vera | So the final code (attachment:Plotting2.java) will result in the following output: |
83 | 2 | Federico Vera | |
84 | ![Screenshot](sin2.png) |