cd4f4a241a19354e9bc61d77c4235537756083be
[sixth-3d.git] / src / main / java / eu / svjatoslav / sixth / e3d / renderer / raster / shapes / composite / wireframe / Grid2D.java
1 /*
2  * Sixth 3D engine. Author: Svjatoslav Agejenko. 
3  * This project is released under Creative Commons Zero (CC0) license.
4  */
5 package eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite.wireframe;
6
7 import eu.svjatoslav.sixth.e3d.geometry.Point3D;
8 import eu.svjatoslav.sixth.e3d.geometry.Rectangle;
9 import eu.svjatoslav.sixth.e3d.math.Transform;
10 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.basic.line.LineAppearance;
11 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite.base.AbstractCompositeShape;
12
13 public class Grid2D extends AbstractCompositeShape {
14
15     /**
16      * @param transform Grid location
17      * @param rectangle Grid dimensions
18      * @param xDivisionCount Division count along X axis
19      * @param yDivisionCount Division count along Y axis
20      * @param appearance Grid lines appearance
21      */
22     public Grid2D(final Transform transform, final Rectangle rectangle,
23                   final int xDivisionCount, final int yDivisionCount,
24                   final LineAppearance appearance) {
25
26         super(transform);
27
28         final double stepY = rectangle.getHeight() / yDivisionCount;
29         final double stepX = rectangle.getWidth() / xDivisionCount;
30
31         for (int ySlice = 0; ySlice <= yDivisionCount; ySlice++) {
32             final double y = (ySlice * stepY) + rectangle.getLowerY();
33
34             for (int xSlice = 0; xSlice <= xDivisionCount; xSlice++) {
35                 final double x = (xSlice * stepX) + rectangle.getLowerX();
36
37                 final Point3D p1 = new Point3D(x, y, 0);
38                 final Point3D p2 = new Point3D(x + stepX, y, 0);
39                 final Point3D p3 = new Point3D(x, y + stepY, 0);
40
41                 if (xSlice < xDivisionCount)
42                     addShape(appearance.getLine(p1, p2));
43
44                 if (ySlice < yDivisionCount)
45                     addShape(appearance.getLine(p1, p3));
46             }
47
48         }
49
50     }
51
52 }