2 * Sixth 3D engine. Copyright ©2012-2020, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
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.
10 package eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite;
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;
20 import java.util.List;
22 public class Graph extends AbstractCompositeShape {
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;
33 public Graph(final double scale, final List<Point2D> data,
34 final String label, final Point3D location) {
45 gridColor = new Color(100, 100, 250, 100);
47 lineWidth = 0.1 * scale;
48 plotColor = new Color(255, 0, 0, 100);
50 addVerticalLines(scale);
52 addHorizontalLinesAndLabels(scale);
53 plotData(scale, data);
55 final Point3D labelLocation = new Point3D(width / 2, yMax + 0.5, 0)
58 final TextCanvas labelCanvas = new TextCanvas(new Transform(
59 labelLocation), label, Color.WHITE, Color.TRANSPARENT);
61 addShape(labelCanvas);
64 private void addHorizontalLinesAndLabels(final double scale) {
65 for (double y = yMin; y <= yMax; y += verticalStep) {
67 final Point3D p1 = new Point3D(0, y, 0).scaleUp(scale);
69 final Point3D p2 = new Point3D(width, y, 0).scaleUp(scale);
71 final Line line = new Line(p1, p2, gridColor, lineWidth);
75 final Point3D labelLocation = new Point3D(-0.5, y, 0)
78 final TextCanvas label = new TextCanvas(
79 new Transform(labelLocation), String.valueOf(y),
80 Color.WHITE, Color.TRANSPARENT);
87 private void addVerticalLines(final double scale) {
88 for (double x = 0; x <= width; x += horizontalStep) {
90 final Point3D p1 = new Point3D(x, yMin, 0).scaleUp(scale);
91 final Point3D p2 = new Point3D(x, yMax, 0).scaleUp(scale);
93 final Line line = new Line(p1, p2, gridColor, lineWidth);
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)
105 final TextCanvas label = new TextCanvas(
106 new Transform(labelLocation), String.valueOf(x),
107 Color.WHITE, Color.TRANSPARENT);
113 private void plotData(final double scale, final List<Point2D> data) {
114 Point3D previousPoint = null;
115 for (final Point2D point : data) {
117 final Point3D p3d = new Point3D(point.x, point.y, 0).scaleUp(scale);
119 if (previousPoint != null) {
121 final Line line = new Line(previousPoint, p3d, plotColor,