Improved code readability
[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.Color;
13 import eu.svjatoslav.sixth.e3d.renderer.raster.ShapeCollection;
14 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.basic.line.LineAppearance;
15 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.basic.solidpolygon.SolidPolygon;
16 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite.Graph;
17 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite.wireframe.WireframeSphere;
18
19 import java.util.ArrayList;
20 import java.util.List;
21
22 public class GraphDemo {
23
24     private static final double WAVE_FREQUENCY = 50d;
25     private static final double WAVE_AMPLITUDE = 50d;
26     private static final Color SQUARE_PLATE_COLOR = new Color("88F7");
27     private static final double GRAPH_SCALE = 50d;
28
29     private static void makeSquarePlate(final ShapeCollection shapeCollection,
30                                         final double y, final double x, final double z) {
31         final Point3D p1 = new Point3D(x, y, z);
32         final Point3D p2 = new Point3D(x + 20, y, z);
33         final Point3D p3 = new Point3D(x, y, z + 20);
34         final Point3D p4 = new Point3D(x + 20, y, z + 20);
35         final SolidPolygon polygon1 = new SolidPolygon(p1, p2, p3, SQUARE_PLATE_COLOR);
36         final SolidPolygon polygon2 = new SolidPolygon(p4, p2, p3, SQUARE_PLATE_COLOR);
37         shapeCollection.addShape(polygon1);
38         shapeCollection.addShape(polygon2);
39     }
40
41     /**
42      * @param surfaceElevation surface total elevation
43      */
44     private static void addWobblySurface(final ShapeCollection shapeCollection,
45                                          final double surfaceElevation) {
46         for (double x = -500; x < 500; x += 20)
47             for (double z = -500; z < 500; z += 20) {
48
49                 // use Pythagorean theorem to compute distance from the center
50                 final double distanceFromCenter = Math.sqrt((x * x) + (z * z));
51
52                 double plateElevation = Math.sin(distanceFromCenter / WAVE_FREQUENCY) * WAVE_AMPLITUDE;
53
54                 makeSquarePlate(shapeCollection, plateElevation + surfaceElevation, x,
55                         z);
56             }
57     }
58
59     private static Graph getCosineGraph(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.cos(x);
63
64             final Point2D p = new Point2D(x, y);
65             data.add(p);
66         }
67
68         return new Graph(GRAPH_SCALE, data, "Cosine", location);
69     }
70
71     private static Graph getFormula1Graph(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(Math.tan(x));
75
76             final Point2D p = new Point2D(x, y);
77             data.add(p);
78         }
79
80         return new Graph(GRAPH_SCALE, data, "y = sin(tan(x))", location);
81     }
82
83     private static Graph getFormula2Graph(final Point3D location) {
84         final List<Point2D> data = new ArrayList<>();
85         for (double x = 0; x < 20; x += 0.25) {
86             final double y = (Math.pow((10 - x), 2) / 30) - 2;
87
88             final Point2D p = new Point2D(x, y);
89             data.add(p);
90         }
91
92         return new Graph(GRAPH_SCALE, data, "y = ( (10-x)^2 ) / 30", location);
93     }
94
95     private static Graph getFormula3Graph(final Point3D location) {
96         final List<Point2D> data = new ArrayList<>();
97         for (double x = 0; x < 20; x += 0.25) {
98             final double y = Math.sin(x / 2) + Math.sin(x / 1.26);
99
100             final Point2D p = new Point2D(x, y);
101             data.add(p);
102         }
103
104         return new Graph(GRAPH_SCALE, data, "y = sin(x/2) + sin(x/1.26)", location);
105     }
106
107     private static Graph getSineGraph(final Point3D location) {
108         final List<Point2D> data = new ArrayList<>();
109         for (double x = 0; x < 20; x += 0.25) {
110             final double y = Math.sin(x);
111
112             final Point2D p = new Point2D(x, y);
113             data.add(p);
114         }
115
116         return new Graph(GRAPH_SCALE, data, "Sine", location);
117     }
118
119     private static Graph getTangentGraph(final Point3D location) {
120         final List<Point2D> data = new ArrayList<>();
121         for (double x = 0; x < 20; x += 0.25) {
122             double y = Math.tan(x);
123
124             if (y > 2)
125                 y = 2;
126             if (y < -2)
127                 y = -2;
128
129             final Point2D p = new Point2D(x, y);
130             data.add(p);
131         }
132
133         return new Graph(GRAPH_SCALE, data, "Tangent", location);
134     }
135
136     public static void main(final String[] args) {
137
138         final ViewFrame viewFrame = new ViewFrame();
139         final ShapeCollection geometryCollection = viewFrame.getViewPanel()
140                 .getRootShapeCollection();
141
142         addMathFormulas(geometryCollection);
143         addSphere(geometryCollection);
144         addWobblySurface(geometryCollection, 200);
145         addWobblySurface(geometryCollection, -200);
146
147         setAvatarLocation(viewFrame);
148     }
149
150     private static void addSphere(ShapeCollection geometryCollection) {
151         // add sphere
152         geometryCollection.addShape(new WireframeSphere(new Point3D(0, 0, 0),
153                 100,
154                 new LineAppearance(
155                         4,
156                         new Color(255,0, 0, 30))
157         ));
158     }
159
160     private static void addMathFormulas(ShapeCollection geometryCollection) {
161         int z = 1000;
162         Point3D location = new Point3D(-600, -300, z);
163         geometryCollection.addShape(getSineGraph(location));
164
165         location = new Point3D(600, -300, z);
166         geometryCollection.addShape(getFormula1Graph(location));
167
168         location = new Point3D(-600, 0, z);
169         geometryCollection.addShape(getCosineGraph(location));
170
171         location = new Point3D(600, 0, z);
172         geometryCollection.addShape(getFormula2Graph(location));
173
174         location = new Point3D(-600, 300, z);
175         geometryCollection.addShape(getTangentGraph(location));
176
177         location = new Point3D(600, 300, z);
178         geometryCollection.addShape(getFormula3Graph(location));
179     }
180
181     private static void setAvatarLocation(ViewFrame viewFrame) {
182         viewFrame.getViewPanel().getAvatar()
183                 .setLocation(new Point3D(0, 0, -500));
184     }
185
186 }