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