2 * Sixth 3D engine. Author: Svjatoslav Agejenko.
3 * This project is released under Creative Commons Zero (CC0) license.
8 package eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite;
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;
18 import java.util.List;
20 public class Graph extends AbstractCompositeShape {
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;
31 public Graph(final double scale, final List<Point2D> data,
32 final String label, final Point3D location) {
43 gridColor = new Color(100, 100, 250, 100);
45 lineWidth = 0.1 * scale;
46 plotColor = new Color(255, 0, 0, 100);
48 addVerticalLines(scale);
50 addHorizontalLinesAndLabels(scale);
51 plotData(scale, data);
53 final Point3D labelLocation = new Point3D(width / 2, yMax + 0.5, 0)
56 final TextCanvas labelCanvas = new TextCanvas(new Transform(
57 labelLocation), label, Color.WHITE, Color.TRANSPARENT);
59 addShape(labelCanvas);
62 private void addHorizontalLinesAndLabels(final double scale) {
63 for (double y = yMin; y <= yMax; y += verticalStep) {
65 final Point3D p1 = new Point3D(0, y, 0).scaleUp(scale);
67 final Point3D p2 = new Point3D(width, y, 0).scaleUp(scale);
69 final Line line = new Line(p1, p2, gridColor, lineWidth);
73 final Point3D labelLocation = new Point3D(-0.5, y, 0)
76 final TextCanvas label = new TextCanvas(
77 new Transform(labelLocation), String.valueOf(y),
78 Color.WHITE, Color.TRANSPARENT);
85 private void addVerticalLines(final double scale) {
86 for (double x = 0; x <= width; x += horizontalStep) {
88 final Point3D p1 = new Point3D(x, yMin, 0).scaleUp(scale);
89 final Point3D p2 = new Point3D(x, yMax, 0).scaleUp(scale);
91 final Line line = new Line(p1, p2, gridColor, lineWidth);
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)
103 final TextCanvas label = new TextCanvas(
104 new Transform(labelLocation), String.valueOf(x),
105 Color.WHITE, Color.TRANSPARENT);
111 private void plotData(final double scale, final List<Point2D> data) {
112 Point3D previousPoint = null;
113 for (final Point2D point : data) {
115 final Point3D p3d = new Point3D(point.x, point.y, 0).scaleUp(scale);
117 if (previousPoint != null) {
119 final Line line = new Line(previousPoint, p3d, plotColor,