Formatting update
[sixth-3d.git] / src / main / java / eu / svjatoslav / sixth / e3d / renderer / raster / shapes / composite / Graph.java
1 /*
2  * Sixth 3D engine. Author: Svjatoslav Agejenko. 
3  * This project is released under Creative Commons Zero (CC0) license.
4  */
5 package eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite;
6
7 import eu.svjatoslav.sixth.e3d.geometry.Point2D;
8 import eu.svjatoslav.sixth.e3d.geometry.Point3D;
9 import eu.svjatoslav.sixth.e3d.math.Transform;
10 import eu.svjatoslav.sixth.e3d.renderer.raster.Color;
11 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.basic.line.Line;
12 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite.base.AbstractCompositeShape;
13 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite.textcanvas.TextCanvas;
14
15 import java.util.List;
16
17 public class Graph extends AbstractCompositeShape {
18
19     private final double width;
20     private final double yMin;
21     private final double yMax;
22     private final double horizontalStep;
23     private final double verticalStep;
24     private final Color gridColor;
25     private final double lineWidth;
26     private final Color plotColor;
27
28     public Graph(final double scale, final List<Point2D> data,
29                  final String label, final Point3D location) {
30         super(location);
31
32         width = 20;
33
34         yMin = -2;
35         yMax = 2;
36
37         horizontalStep = 0.5;
38         verticalStep = 0.5;
39
40         gridColor = new Color(100, 100, 250, 100);
41
42         lineWidth = 0.1 * scale;
43         plotColor = new Color(255, 0, 0, 100);
44
45         addVerticalLines(scale);
46         addXLabels(scale);
47         addHorizontalLinesAndLabels(scale);
48         plotData(scale, data);
49
50         final Point3D labelLocation = new Point3D(width / 2, yMax + 0.5, 0)
51                 .scaleUp(scale);
52
53         final TextCanvas labelCanvas = new TextCanvas(new Transform(
54                 labelLocation), label, Color.WHITE, Color.TRANSPARENT);
55
56         addShape(labelCanvas);
57     }
58
59     private void addHorizontalLinesAndLabels(final double scale) {
60         for (double y = yMin; y <= yMax; y += verticalStep) {
61
62             final Point3D p1 = new Point3D(0, y, 0).scaleUp(scale);
63
64             final Point3D p2 = new Point3D(width, y, 0).scaleUp(scale);
65
66             final Line line = new Line(p1, p2, gridColor, lineWidth);
67
68             addShape(line);
69
70             final Point3D labelLocation = new Point3D(-0.5, y, 0)
71                     .scaleUp(scale);
72
73             final TextCanvas label = new TextCanvas(
74                     new Transform(labelLocation), String.valueOf(y),
75                     Color.WHITE, Color.TRANSPARENT);
76
77             addShape(label);
78
79         }
80     }
81
82     private void addVerticalLines(final double scale) {
83         for (double x = 0; x <= width; x += horizontalStep) {
84
85             final Point3D p1 = new Point3D(x, yMin, 0).scaleUp(scale);
86             final Point3D p2 = new Point3D(x, yMax, 0).scaleUp(scale);
87
88             final Line line = new Line(p1, p2, gridColor, lineWidth);
89
90             addShape(line);
91
92         }
93     }
94
95     private void addXLabels(final double scale) {
96         for (double x = 0; x <= width; x += horizontalStep * 2) {
97             final Point3D labelLocation = new Point3D(x, yMin - 0.4, 0)
98                     .scaleUp(scale);
99
100             final TextCanvas label = new TextCanvas(
101                     new Transform(labelLocation), String.valueOf(x),
102                     Color.WHITE, Color.TRANSPARENT);
103
104             addShape(label);
105         }
106     }
107
108     private void plotData(final double scale, final List<Point2D> data) {
109         Point3D previousPoint = null;
110         for (final Point2D point : data) {
111
112             final Point3D p3d = new Point3D(point.x, point.y, 0).scaleUp(scale);
113
114             if (previousPoint != null) {
115
116                 final Line line = new Line(previousPoint, p3d, plotColor,
117                         0.4 * scale);
118
119                 addShape(line);
120             }
121
122             previousPoint = p3d;
123         }
124     }
125 }