Formatting update
[sixth-3d.git] / src / main / java / eu / svjatoslav / sixth / e3d / renderer / raster / shapes / composite / Galaxy.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;
6
7 import eu.svjatoslav.sixth.e3d.geometry.Point3D;
8 import eu.svjatoslav.sixth.e3d.math.Transform;
9 import eu.svjatoslav.sixth.e3d.renderer.raster.Color;
10 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.basic.GlowingPoint;
11 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite.base.AbstractCompositeShape;
12
13 import java.util.ArrayList;
14 import java.util.List;
15 import java.util.Random;
16
17 public class Galaxy extends AbstractCompositeShape {
18
19     private static List<Color> colors;
20
21     public Galaxy(final int size, final int tailCount, final int starsCount,
22                   Transform transform) {
23
24         super(transform);
25
26         ensureColorsAreInitialized();
27
28         final double angle1 = Math.random() * 10;
29         final double angle2 = Math.random() * 10;
30
31         final double angleSin1 = Math.sin(angle1);
32         final double angleCos1 = Math.cos(angle1);
33         final double angleSin2 = Math.sin(angle2);
34         final double angleCos2 = Math.cos(angle2);
35
36         Random random = new Random();
37
38         for (int i = 1; i < starsCount; i++) {
39             final double b = Math.random() * 10;
40
41             final double s = (b * b) / 30;
42
43             final double v1 = (Math.random() * (11.5 - b)) / 3;
44             final double v1p = v1 / 2;
45
46             final double ane = ((Math.random() * (s / 2)) / tailCount) * 2;
47             final double sba = ((2 * Math.PI) / tailCount)
48                     * random.nextInt(tailCount);
49
50             final double x = (((Math.sin((b - sba) + ane) * s) + (Math.random() * v1)) - v1p)
51                     * size;
52             final double z = (((Math.cos((b - sba) + ane) * s) + (Math.random() * v1)) - v1p)
53                     * size;
54             final double y = ((Math.random() * v1) - v1p) * size;
55
56             final double x1 = (x * angleCos1) + (z * angleSin1);
57             final double z1 = (z * angleCos1) - (x * angleSin1);
58
59             final double y1 = (y * angleCos2) + (z1 * angleSin2);
60             final double z2 = (z1 * angleCos2) - (y * angleSin2);
61
62             addStar(new Point3D(x1, y1, z2));
63         }
64     }
65
66     private void addStar(final Point3D starLocation) {
67         addShape(
68                 new GlowingPoint(starLocation, 10,
69                         colors.get((int) (Math.random() * colors.size()))));
70     }
71
72     private synchronized void ensureColorsAreInitialized() {
73         if (colors != null) return;
74
75         colors = new ArrayList<>();
76
77         for (int i = 0; i < 30; i++)
78             colors.add(
79                     new Color(
80                             Math.random() + 0.5,
81                             Math.random() + 0.5,
82                             Math.random() + 0.5,
83                             255));
84     }
85
86 }