Proyecto

General

Perfil

Plotting parametric equations » Histórico » Versión 3

Federico Vera, 2018-07-09 19:31

1 1 Federico Vera
# Plotting parametric equations
2
Let's plot some parametric equations, with a small twist. This is basically the
3
core of [ParametricFun], and it was to that end that this class was created. For
4
this example we'll use some easier equation.
5
6
In order to obtain some parametric equations, let's checkout [Wikipedia].
7
8
For this example we'll be plotting:
9
10
![image](paramx.png)
11
12
![image](paramy.png)
13
14 3 Federico Vera
## Import the classes
15 1 Federico Vera
```java
16
import com.dkt.graphics.extras.formula.ParametricCalculable;
17
import com.dkt.graphics.extras.formula.ParametricCalculableTimer;
18
```
19 3 Federico Vera
## Declare variables
20 1 Federico Vera
Since this equations have a lot of parameters, we'll declare some auxiliary variables to clarify the code a bit.
21
```java
22
// This values will change the output as explained
23
// in the Wikipedia article, so change them at will
24
final double a = 1;
25
final double b = 60;
26
final double c = 1;
27
final double d = 1;
28
final double e = 60;
29
final double i = 1;
30
final double j = 2;
31
```
32
33 3 Federico Vera
## Create a new `ParametricCalculable`
34 1 Federico Vera
We will create a new `ParametricCalculable` object, and implement `x(t)` and `y(t)` according to the specified equation, in this case:
35
```java
36
ParametricCalculable eq = new ParametricCalculable() {
37
    @Override
38
    public double x(double t) {
39
        return i * Math.cos(a*t) - Math.cos(b*t) * Math.sin(c*t);
40
    }
41
42
    @Override
43
    public double y(double t) {
44
        return j * Math.sin(d*t) - Math.sin(e*t);
45
    }
46
};
47
```
48
49 3 Federico Vera
## Edit the drawing and calculation parameters
50 1 Federico Vera
As before we need to scale the equation, and set the interval and step:
51
```java
52
eq.setScaleX(80);
53
eq.setScaleY(80);
54
eq.startPoint(0);
55
eq.endPoint(2 * Math.PI);
56
eq.step(0.000001);
57
```
58
59 3 Federico Vera
## Create a `ParametricCalculableTimer`
60 1 Federico Vera
Since this is a timer, we'll use the auto repaint feature of the canvas.
61
```java
62
canvas.setAutoRepaint(true);
63
canvas.setRepaintDelay(25);
64
```
65
And finally create a new timer and **run it**!
66
67
```java
68
ParametricCalculableTimer timer = new ParametricCalculableTimer(eq);
69
canvas.add(timer);
70
timer.start();
71
```
72
73 3 Federico Vera
## Misc
74 1 Federico Vera
This class has some interesting features that I invite you to play with:
75
* `ParametricCalculableTimer#setNumberOfThreads(...)`
76
* `ParametricCalculableTimer#setDrawPen(...)`
77
* `ParametricCalculableTimer#setDrawAsPath(...)`
78
* `ParametricCalculableTimer#pause()`
79
* `ParametricCalculableTimer#resume()`
80
81
There are some situations where you'll need to take some action when the timer starts, pauses, resumes or ends; to that end there's an interface that you can use `AbstractTimer.Action` and then register it in your timer with something like:
82
```java
83
ParametricCalculableTimer#setActions(new AbstractTimer.Action() {
84
    @Override
85
    public void start() {
86
    }
87
    @Override
88
    public void pause() {
89
    }
90
    @Override
91
    public void resume() {
92
    }
93
    @Override
94
    public void stop() {
95
    }
96
});
97
```
98
This comes in handy when dealing with the auto repaint feature, since we can use it to show an "animation" of the equation being calculated, but we need to cancel it once the calculation has finished. On the other hand, if we only care about the end result, it's much faster to calculate without drawing, and draw all together once all the threads are done.
99
100
The end result should look something like this:
101
102
![image](test.gif)
103
104
The final code can be downloaded from here `->` attachment:Parametric.java
105
106
[Wikipedia]: http://en.wikipedia.org/wiki/Parametric_equation#Some_sophisticated_functions
107
[ParametricFun]: https://redmine.riddler.com.ar/attachments/193/ParametricFun.jar
Volver al inicio