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