Merged sphere demo and graph demo. Set default FPS to 60.
[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.io.IOException;
20 import java.util.ArrayList;
21 import java.util.List;
22
23 public class GraphDemo {
24
25     private static final double WAVE_FREQUENCY = 50d;
26     private static final double WAVE_AMPLITUDE = 50d;
27     private static final Color SQUARE_PLATE_COLOR = new Color("88F7");
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 final double scale = 50d;
60
61     private static Graph getCosineGraph(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.cos(x);
65
66             final Point2D p = new Point2D(x, y);
67             data.add(p);
68         }
69
70         return new Graph(scale, data, "Cosine", location);
71     }
72
73     private static Graph getFormula1Graph(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(Math.tan(x));
77
78             final Point2D p = new Point2D(x, y);
79             data.add(p);
80         }
81
82         return new Graph(scale, data, "y = sin(tan(x))", location);
83     }
84
85     private static Graph getFormula2Graph(final Point3D location) {
86         final List<Point2D> data = new ArrayList<>();
87         for (double x = 0; x < 20; x += 0.25) {
88             final double y = (Math.pow((10 - x), 2) / 30) - 2;
89
90             final Point2D p = new Point2D(x, y);
91             data.add(p);
92         }
93
94         return new Graph(scale, data, "y = ( (10-x)^2 ) / 30", location);
95     }
96
97     private static Graph getFormula3Graph(final Point3D location) {
98         final List<Point2D> data = new ArrayList<>();
99         for (double x = 0; x < 20; x += 0.25) {
100             final double y = Math.sin(x / 2) + Math.sin(x / 1.26);
101
102             final Point2D p = new Point2D(x, y);
103             data.add(p);
104         }
105
106         return new Graph(scale, data, "y = sin(x/2) + sin(x/1.26)", location);
107     }
108
109     private static Graph getSineGraph(final Point3D location) {
110         final List<Point2D> data = new ArrayList<>();
111         for (double x = 0; x < 20; x += 0.25) {
112             final double y = Math.sin(x);
113
114             final Point2D p = new Point2D(x, y);
115             data.add(p);
116         }
117
118         return new Graph(scale, data, "Sine", location);
119     }
120
121     private static Graph getTangentGraph(final Point3D location) {
122         final List<Point2D> data = new ArrayList<>();
123         for (double x = 0; x < 20; x += 0.25) {
124             double y = Math.tan(x);
125
126             if (y > 2)
127                 y = 2;
128             if (y < -2)
129                 y = -2;
130
131             final Point2D p = new Point2D(x, y);
132             data.add(p);
133         }
134
135         return new Graph(scale, data, "Tangent", location);
136     }
137
138     public static void main(final String[] args) throws IOException {
139
140         final ViewFrame viewFrame = new ViewFrame();
141         final ShapeCollection geometryCollection = viewFrame.getViewPanel()
142                 .getRootShapeCollection();
143
144         addMathFormulas(geometryCollection);
145         addSphere(geometryCollection);
146         addWobblySurface(geometryCollection, 200);
147         addWobblySurface(geometryCollection, -200);
148
149         setAvatarLocation(viewFrame);
150     }
151
152     private static void addSphere(ShapeCollection geometryCollection) {
153         // add sphere
154         geometryCollection.addShape(new WireframeSphere(new Point3D(0, 0, 0),
155                 100,
156                 new LineAppearance(
157                         4,
158                         new Color(255,0, 0, 30))
159         ));
160     }
161
162     private static void addMathFormulas(ShapeCollection geometryCollection) {
163         int z = 1000;
164         Point3D location = new Point3D(-600, -300, z);
165         geometryCollection.addShape(getSineGraph(location));
166
167         location = new Point3D(600, -300, z);
168         geometryCollection.addShape(getFormula1Graph(location));
169
170         location = new Point3D(-600, 0, z);
171         geometryCollection.addShape(getCosineGraph(location));
172
173         location = new Point3D(600, 0, z);
174         geometryCollection.addShape(getFormula2Graph(location));
175
176         location = new Point3D(-600, 300, z);
177         geometryCollection.addShape(getTangentGraph(location));
178
179         location = new Point3D(600, 300, z);
180         geometryCollection.addShape(getFormula3Graph(location));
181     }
182
183     private static void setAvatarLocation(ViewFrame viewFrame) {
184         viewFrame.getViewPanel().getAvatar()
185                 .setLocation(new Point3D(0, 0, -500));
186     }
187
188 }