Removed ViewListener interface. Renamed View to ViewPanel.
[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 getCosinusGraph(final Point3D location)
26             throws IOException {
27         final List<Point2D> data = new ArrayList<>();
28         for (double x = 0; x < 20; x += 0.25) {
29             final double y = Math.cos(x);
30
31             final Point2D p = new Point2D(x, y);
32             data.add(p);
33         }
34
35         return new Graph(scale, data, "Cosinus", location);
36     }
37
38     private static Graph getFormula1Graph(final Point3D location)
39             throws IOException {
40         final List<Point2D> data = new ArrayList<>();
41         for (double x = 0; x < 20; x += 0.25) {
42             final double y = Math.sin(Math.tan(x));
43
44             final Point2D p = new Point2D(x, y);
45             data.add(p);
46         }
47
48         return new Graph(scale, data, "y = sin(tan(x))", location);
49     }
50
51     private static Graph getFormula2Graph(final Point3D location)
52             throws IOException {
53         final List<Point2D> data = new ArrayList<>();
54         for (double x = 0; x < 20; x += 0.25) {
55             final double y = (Math.pow((10 - x), 2) / 30) - 2;
56
57             final Point2D p = new Point2D(x, y);
58             data.add(p);
59         }
60
61         return new Graph(scale, data, "y = ( (10-x)^2 ) / 30", location);
62     }
63
64     private static Graph getFormula3Graph(final Point3D location)
65             throws IOException {
66         final List<Point2D> data = new ArrayList<>();
67         for (double x = 0; x < 20; x += 0.25) {
68             final double y = Math.sin(x / 2) + Math.sin(x / 1.26);
69
70             final Point2D p = new Point2D(x, y);
71             data.add(p);
72         }
73
74         return new Graph(scale, data, "y = sin(x/2) + sin(x/1.26)", location);
75     }
76
77     private static Graph getSinusGraph(final Point3D location)
78             throws IOException {
79         final List<Point2D> data = new ArrayList<>();
80         for (double x = 0; x < 20; x += 0.25) {
81             final double y = Math.sin(x);
82
83             final Point2D p = new Point2D(x, y);
84             data.add(p);
85         }
86
87         return new Graph(scale, data, "Sinus", location);
88     }
89
90     private static Graph getTangentGraph(final Point3D location)
91             throws IOException {
92         final List<Point2D> data = new ArrayList<>();
93         for (double x = 0; x < 20; x += 0.25) {
94             double y = Math.tan(x);
95
96             if (y > 2)
97                 y = 2;
98             if (y < -2)
99                 y = -2;
100
101             final Point2D p = new Point2D(x, y);
102             data.add(p);
103         }
104
105         return new Graph(scale, data, "Tangent", location);
106     }
107
108     public static void main(final String[] args) throws IOException {
109
110         final ViewFrame viewFrame = new ViewFrame();
111
112         final ShapeCollection geometryCollection = viewFrame.getViewPanel()
113                 .getContext().getRootShapeCollection();
114
115         Point3D location = new Point3D(-600, -300, 0);
116         geometryCollection.addShape(getSinusGraph(location));
117
118         location = new Point3D(600, -300, 0);
119         geometryCollection.addShape(getFormula1Graph(location));
120
121         location = new Point3D(-600, 0, 0);
122         geometryCollection.addShape(getCosinusGraph(location));
123
124         location = new Point3D(600, 0, 0);
125         geometryCollection.addShape(getFormula2Graph(location));
126
127         location = new Point3D(-600, 300, 0);
128         geometryCollection.addShape(getTangentGraph(location));
129
130         location = new Point3D(600, 300, 0);
131         geometryCollection.addShape(getFormula3Graph(location));
132
133         viewFrame.getViewPanel().getContext().getAvatar()
134                 .setLocation(new Point3D(0, 0, -500));
135
136     }
137 }