2 * Sixth 3D engine. Copyright ©2012-2018, 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.geometry.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.io.IOException;
21 import java.util.List;
23 public class Graph extends AbstractCompositeShape {
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;
34 public Graph(final double scale, final List<Point2D> data,
35 final String label, final Point3D location) throws IOException {
46 gridColor = new Color(100, 100, 250, 100);
48 lineWidth = 0.1 * scale;
49 plotColor = new Color(255, 0, 0, 100);
51 addVerticalLines(scale);
53 addHorizontalLinesAndLabels(scale);
54 plotData(scale, data);
56 final Point3D labelLocation = new Point3D(width / 2, yMax + 0.5, 0)
59 final TextCanvas labelCanvas = new TextCanvas(new Transform(
60 labelLocation), label, Color.WHITE, Color.TRANSPARENT);
62 addShape(labelCanvas);
65 public void addHorizontalLinesAndLabels(final double scale)
67 for (double y = yMin; y <= yMax; y += verticalStep) {
69 final Point3D p1 = new Point3D(0, y, 0).scaleUp(scale);
71 final Point3D p2 = new Point3D(width, y, 0).scaleUp(scale);
73 final Line line = new Line(p1, p2, gridColor, lineWidth);
77 final Point3D labelLocation = new Point3D(-0.5, y, 0)
80 final TextCanvas label = new TextCanvas(
81 new Transform(labelLocation), String.valueOf(y),
82 Color.WHITE, Color.TRANSPARENT);
89 public void addVerticalLines(final double scale) {
90 for (double x = 0; x <= width; x += horizontalStep) {
92 final Point3D p1 = new Point3D(x, yMin, 0).scaleUp(scale);
93 final Point3D p2 = new Point3D(x, yMax, 0).scaleUp(scale);
95 final Line line = new Line(p1, p2, gridColor, lineWidth);
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)
107 final TextCanvas label = new TextCanvas(
108 new Transform(labelLocation), String.valueOf(x),
109 Color.WHITE, Color.TRANSPARENT);
115 public void plotData(final double scale, final List<Point2D> data) {
116 Point3D previousPoint = null;
117 for (final Point2D point : data) {
119 final Point3D p3d = new Point3D(point.x, point.y, 0).scaleUp(scale);
121 if (previousPoint != null) {
123 final Line line = new Line(previousPoint, p3d, plotColor,