Optimized frame repainting. Fixed mouse click processing.
[sixth-3d-demos.git] / src / main / java / eu / svjatoslav / sixth / e3d / examples / GraphDemo.java
1 /*
2  * Sixth 3D engine demos. Copyright ©2012-2018, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of version 3 of the GNU Lesser General Public License
6  * or later as published by the Free Software Foundation.
7  */
8
9 package eu.svjatoslav.sixth.e3d.examples;
10
11 import eu.svjatoslav.sixth.e3d.geometry.Point2D;
12 import eu.svjatoslav.sixth.e3d.geometry.Point3D;
13 import eu.svjatoslav.sixth.e3d.gui.ViewFrame;
14 import eu.svjatoslav.sixth.e3d.renderer.raster.ShapeCollection;
15 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite.Graph;
16
17 import java.io.IOException;
18 import java.util.ArrayList;
19 import java.util.List;
20
21 public class GraphDemo {
22
23     private static final double scale = 50d;
24
25     private static Graph getCosineGraph(final Point3D location) {
26         final List<Point2D> data = new ArrayList<>();
27         for (double x = 0; x < 20; x += 0.25) {
28             final double y = Math.cos(x);
29
30             final Point2D p = new Point2D(x, y);
31             data.add(p);
32         }
33
34         return new Graph(scale, data, "Cosine", location);
35     }
36
37     private static Graph getFormula1Graph(final Point3D location) {
38         final List<Point2D> data = new ArrayList<>();
39         for (double x = 0; x < 20; x += 0.25) {
40             final double y = Math.sin(Math.tan(x));
41
42             final Point2D p = new Point2D(x, y);
43             data.add(p);
44         }
45
46         return new Graph(scale, data, "y = sin(tan(x))", location);
47     }
48
49     private static Graph getFormula2Graph(final Point3D location) {
50         final List<Point2D> data = new ArrayList<>();
51         for (double x = 0; x < 20; x += 0.25) {
52             final double y = (Math.pow((10 - x), 2) / 30) - 2;
53
54             final Point2D p = new Point2D(x, y);
55             data.add(p);
56         }
57
58         return new Graph(scale, data, "y = ( (10-x)^2 ) / 30", location);
59     }
60
61     private static Graph getFormula3Graph(final Point3D location) {
62         final List<Point2D> data = new ArrayList<>();
63         for (double x = 0; x < 20; x += 0.25) {
64             final double y = Math.sin(x / 2) + Math.sin(x / 1.26);
65
66             final Point2D p = new Point2D(x, y);
67             data.add(p);
68         }
69
70         return new Graph(scale, data, "y = sin(x/2) + sin(x/1.26)", location);
71     }
72
73     private static Graph getSineGraph(final Point3D location) {
74         final List<Point2D> data = new ArrayList<>();
75         for (double x = 0; x < 20; x += 0.25) {
76             final double y = Math.sin(x);
77
78             final Point2D p = new Point2D(x, y);
79             data.add(p);
80         }
81
82         return new Graph(scale, data, "Sine", location);
83     }
84
85     private static Graph getTangentGraph(final Point3D location) {
86         final List<Point2D> data = new ArrayList<>();
87         for (double x = 0; x < 20; x += 0.25) {
88             double y = Math.tan(x);
89
90             if (y > 2)
91                 y = 2;
92             if (y < -2)
93                 y = -2;
94
95             final Point2D p = new Point2D(x, y);
96             data.add(p);
97         }
98
99         return new Graph(scale, data, "Tangent", location);
100     }
101
102     public static void main(final String[] args) throws IOException {
103
104         final ViewFrame viewFrame = new ViewFrame();
105
106         final ShapeCollection geometryCollection = viewFrame.getViewPanel()
107                 .getRootShapeCollection();
108
109         Point3D location = new Point3D(-600, -300, 0);
110         geometryCollection.addShape(getSineGraph(location));
111
112         location = new Point3D(600, -300, 0);
113         geometryCollection.addShape(getFormula1Graph(location));
114
115         location = new Point3D(-600, 0, 0);
116         geometryCollection.addShape(getCosineGraph(location));
117
118         location = new Point3D(600, 0, 0);
119         geometryCollection.addShape(getFormula2Graph(location));
120
121         location = new Point3D(-600, 300, 0);
122         geometryCollection.addShape(getTangentGraph(location));
123
124         location = new Point3D(600, 300, 0);
125         geometryCollection.addShape(getFormula3Graph(location));
126
127         viewFrame.getViewPanel().getAvatar()
128                 .setLocation(new Point3D(0, 0, -500));
129
130     }
131 }