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