79027b09259e402d4f1f8ff91153d60ff0feb7ec
[sixth-3d-demos.git] / src / main / java / eu / svjatoslav / sixth / e3d / examples / GraphDemo.java
1 /*
2  * Sixth - System for data storage, computation, exploration and interaction.
3  * Copyright ©2012-2016, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
4  * 
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of version 3 of the GNU Lesser General Public License
7  * or later as published by the Free Software Foundation.
8  */
9
10 package eu.svjatoslav.sixth.e3d.examples;
11
12 import eu.svjatoslav.sixth.e3d.geometry.Point2D;
13 import eu.svjatoslav.sixth.e3d.geometry.Point3D;
14 import eu.svjatoslav.sixth.e3d.gui.ViewFrame;
15 import eu.svjatoslav.sixth.e3d.renderer.raster.ShapeCollection;
16 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite.Graph;
17
18 import java.io.IOException;
19 import java.util.ArrayList;
20 import java.util.List;
21
22 public class GraphDemo {
23
24     private static final double scale = 50d;
25
26     private static Graph getCosinusGraph(final Point3D location)
27             throws IOException {
28         final List<Point2D> data = new ArrayList<>();
29         for (double x = 0; x < 20; x += 0.25) {
30             final double y = Math.cos(x);
31
32             final Point2D p = new Point2D(x, y);
33             data.add(p);
34         }
35
36         return new Graph(scale, data, "Cosinus", location);
37     }
38
39     private static Graph getFormula1Graph(final Point3D location)
40             throws IOException {
41         final List<Point2D> data = new ArrayList<>();
42         for (double x = 0; x < 20; x += 0.25) {
43             final double y = Math.sin(Math.tan(x));
44
45             final Point2D p = new Point2D(x, y);
46             data.add(p);
47         }
48
49         return new Graph(scale, data, "y = sin(tan(x))", location);
50     }
51
52     private static Graph getFormula2Graph(final Point3D location)
53             throws IOException {
54         final List<Point2D> data = new ArrayList<>();
55         for (double x = 0; x < 20; x += 0.25) {
56             final double y = (Math.pow((10 - x), 2) / 30) - 2;
57
58             final Point2D p = new Point2D(x, y);
59             data.add(p);
60         }
61
62         return new Graph(scale, data, "y = ( (10-x)^2 ) / 30", location);
63     }
64
65     private static Graph getFormula3Graph(final Point3D location)
66             throws IOException {
67         final List<Point2D> data = new ArrayList<>();
68         for (double x = 0; x < 20; x += 0.25) {
69             final double y = Math.sin(x / 2) + Math.sin(x / 1.26);
70
71             final Point2D p = new Point2D(x, y);
72             data.add(p);
73         }
74
75         return new Graph(scale, data, "y = sin(x/2) + sin(x/1.26)", location);
76     }
77
78     private static Graph getSinusGraph(final Point3D location)
79             throws IOException {
80         final List<Point2D> data = new ArrayList<>();
81         for (double x = 0; x < 20; x += 0.25) {
82             final double y = Math.sin(x);
83
84             final Point2D p = new Point2D(x, y);
85             data.add(p);
86         }
87
88         return new Graph(scale, data, "Sinus", location);
89     }
90
91     private static Graph getTangentGraph(final Point3D location)
92             throws IOException {
93         final List<Point2D> data = new ArrayList<>();
94         for (double x = 0; x < 20; x += 0.25) {
95             double y = Math.tan(x);
96
97             if (y > 2)
98                 y = 2;
99             if (y < -2)
100                 y = -2;
101
102             final Point2D p = new Point2D(x, y);
103             data.add(p);
104         }
105
106         return new Graph(scale, data, "Tangent", location);
107     }
108
109     public static void main(final String[] args) throws IOException {
110
111         final ViewFrame viewFrame = new ViewFrame();
112
113         final ShapeCollection geometryCollection = viewFrame.getView()
114                 .getContext().getRootShapeCollection();
115
116         Point3D location = new Point3D(-600, -300, 0);
117         geometryCollection.addShape(getSinusGraph(location));
118
119         location = new Point3D(600, -300, 0);
120         geometryCollection.addShape(getFormula1Graph(location));
121
122         location = new Point3D(-600, 0, 0);
123         geometryCollection.addShape(getCosinusGraph(location));
124
125         location = new Point3D(600, 0, 0);
126         geometryCollection.addShape(getFormula2Graph(location));
127
128         location = new Point3D(-600, 300, 0);
129         geometryCollection.addShape(getTangentGraph(location));
130
131         location = new Point3D(600, 300, 0);
132         geometryCollection.addShape(getFormula3Graph(location));
133
134         viewFrame.getView().getContext().getAvatar()
135                 .setLocation(new Point3D(0, 0, -500));
136
137     }
138 }