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