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