Project

General

Profile

Plotting parametric equations » Parametric.java

Federico Vera, 2018-07-08 04:58

 
1
import com.dkt.graphics.canvas.Canvas;
2
import com.dkt.graphics.canvas.CanvasFrame;
3
import com.dkt.graphics.extras.formula.ParametricCalculable;
4
import com.dkt.graphics.extras.formula.ParametricCalculableTimer;
5
import com.dkt.graphics.utils.Utils;
6
import java.awt.Color;
7

    
8
public class Parametric {
9
    public static void main(String[] args) {
10
        //////////////////////////////////////////////////////////
11
        //The name that appears in the window
12
        String title = "Parametric example";
13
        //We need to create a new frame
14
        CanvasFrame frame = new CanvasFrame(title);
15
        //And make it visible
16
        frame.setVisible(true);
17
        //Set a size in pixels (whichever you want)
18
        frame.setSize(550/*width*/, 550/*height*/);
19
        //Extract a reference to the canvas from the frame
20
        final Canvas canvas = frame.getCanvas();
21
        //Tell the canvas to center the origin of coordinates,
22
        //which by default is located in the upper left corner
23
        canvas.setCenterOrigin(true);
24
        //Tell the canvas to invert the Y axis, this way we will
25
        //consider all positive Y increments from the origin of
26
        //coordinates to the title bar
27
        canvas.setInvertYAxis(true);
28
        //////////////////////////////////////////////////////////
29
        canvas.setAutoRepaint(true);
30
        canvas.setRepaintDelay(35);
31

    
32
        final double a = 1;
33
        final double b = 60;
34
        final double c = 1;
35
        final double d = 1;
36
        final double e = 60;
37
        final double i = 1;
38
        final double j = 2;
39

    
40
        ParametricCalculable eq = new ParametricCalculable() {
41
            @Override
42
            public double x(double t) {
43
                return i * Math.cos(a * t) - Math.cos(b * t) * Math.sin(c * t);
44
            }
45

    
46
            @Override
47
            public double y(double t) {
48
                return j * Math.sin(d * t) - Math.sin(e * t);
49
            }
50
        };
51

    
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
        ParametricCalculableTimer timer = new ParametricCalculableTimer(eq);
59
        timer.setPaint(Utils.getColorWithAlpha(Color.BLUE, 64));
60
        canvas.add(timer);
61

    
62
        timer.start();
63
    }
64
}
(1-1/4) Go to top