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