2 * Sixth 3D engine. Author: Svjatoslav Agejenko.
3 * This project is released under Creative Commons Zero (CC0) license.
5 package eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite;
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;
15 import java.util.List;
17 public class Graph extends AbstractCompositeShape {
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;
28 public Graph(final double scale, final List<Point2D> data,
29 final String label, final Point3D location) {
40 gridColor = new Color(100, 100, 250, 100);
42 lineWidth = 0.1 * scale;
43 plotColor = new Color(255, 0, 0, 100);
45 addVerticalLines(scale);
47 addHorizontalLinesAndLabels(scale);
48 plotData(scale, data);
50 final Point3D labelLocation = new Point3D(width / 2, yMax + 0.5, 0)
53 final TextCanvas labelCanvas = new TextCanvas(new Transform(
54 labelLocation), label, Color.WHITE, Color.TRANSPARENT);
56 addShape(labelCanvas);
59 private void addHorizontalLinesAndLabels(final double scale) {
60 for (double y = yMin; y <= yMax; y += verticalStep) {
62 final Point3D p1 = new Point3D(0, y, 0).scaleUp(scale);
64 final Point3D p2 = new Point3D(width, y, 0).scaleUp(scale);
66 final Line line = new Line(p1, p2, gridColor, lineWidth);
70 final Point3D labelLocation = new Point3D(-0.5, y, 0)
73 final TextCanvas label = new TextCanvas(
74 new Transform(labelLocation), String.valueOf(y),
75 Color.WHITE, Color.TRANSPARENT);
82 private void addVerticalLines(final double scale) {
83 for (double x = 0; x <= width; x += horizontalStep) {
85 final Point3D p1 = new Point3D(x, yMin, 0).scaleUp(scale);
86 final Point3D p2 = new Point3D(x, yMax, 0).scaleUp(scale);
88 final Line line = new Line(p1, p2, gridColor, lineWidth);
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)
100 final TextCanvas label = new TextCanvas(
101 new Transform(labelLocation), String.valueOf(x),
102 Color.WHITE, Color.TRANSPARENT);
108 private void plotData(final double scale, final List<Point2D> data) {
109 Point3D previousPoint = null;
110 for (final Point2D point : data) {
112 final Point3D p3d = new Point3D(point.x, point.y, 0).scaleUp(scale);
114 if (previousPoint != null) {
116 final Line line = new Line(previousPoint, p3d, plotColor,