40ce99e217c3985a74422b0c90b60931b78a665b
[sixth-3d.git] / src / main / java / eu / svjatoslav / sixth / e3d / renderer / raster / shapes / composite / wireframe / WireframeSphere.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.renderer.raster.shapes.basic.line.LineAppearance;
9 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite.base.AbstractCompositeShape;
10
11 import java.util.ArrayList;
12
13 public class WireframeSphere extends AbstractCompositeShape {
14
15     ArrayList<Point3D> previousRing = new ArrayList<>();
16
17     public WireframeSphere(final Point3D location, final float radius,
18                            final LineAppearance lineFactory) {
19         super(location);
20
21         final double step = Math.PI / 10;
22
23         final Point3D center = new Point3D();
24
25         int ringIndex = 0;
26
27         for (double j = 0d; j <= (Math.PI * 2); j += step) {
28
29             Point3D oldPoint = null;
30             int pointIndex = 0;
31
32             for (double i = 0; i <= (Math.PI * 2); i += step) {
33                 final Point3D newPoint = new Point3D(0, 0, radius);
34                 newPoint.rotate(center, i, j);
35
36                 if (oldPoint != null)
37                     addShape(lineFactory.getLine(newPoint, oldPoint));
38
39                 if (ringIndex > 0) {
40                     final Point3D previousRingPoint = previousRing
41                             .get(pointIndex);
42                     addShape(lineFactory.getLine(newPoint, previousRingPoint));
43
44                     previousRing.set(pointIndex, newPoint);
45                 } else
46                     previousRing.add(newPoint);
47
48                 oldPoint = newPoint;
49                 pointIndex++;
50             }
51
52             ringIndex++;
53         }
54
55     }
56
57 }