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.Point3D;
13 import eu.svjatoslav.sixth.e3d.math.Transform;
14 import eu.svjatoslav.sixth.e3d.renderer.raster.Color;
15 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.basic.GlowingPoint;
16 import eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite.base.AbstractCompositeShape;
18 import java.util.ArrayList;
19 import java.util.List;
20 import java.util.Random;
22 import static java.lang.Math.*;
24 public class Galaxy extends AbstractCompositeShape {
26 private static List<Color> colors;
28 public Galaxy(final int galaxySize, final int tailCount, final int starsCount,
29 Transform transform) {
33 ensureColorsAreInitialized();
35 final double angle1 = random() * 10;
36 final double angle2 = random() * 10;
38 final double angleSin1 = sin(angle1);
39 final double angleCos1 = cos(angle1);
40 final double angleSin2 = sin(angle2);
41 final double angleCos2 = cos(angle2);
43 Random random = new Random();
45 double starSize = galaxySize / 70d;
47 for (int i = 1; i < starsCount; i++) {
48 final double b = random() * 10;
50 final double s = (b * b) / 30;
52 final double v1 = (random() * (11.5 - b)) / 3;
53 final double v1p = v1 / 2;
55 final double ane = ((random() * (s / 2)) / tailCount) * 2;
56 final double sba = ((2 * PI) / tailCount)
57 * random.nextInt(tailCount);
59 final double x = (((sin((b - sba) + ane) * s) + (random() * v1)) - v1p) * galaxySize;
60 final double z = (((cos((b - sba) + ane) * s) + (random() * v1)) - v1p) * galaxySize;
61 final double y = ((random() * v1) - v1p) * galaxySize;
63 final double x1 = (x * angleCos1) + (z * angleSin1);
64 final double z1 = (z * angleCos1) - (x * angleSin1);
66 final double y1 = (y * angleCos2) + (z1 * angleSin2);
67 final double z2 = (z1 * angleCos2) - (y * angleSin2);
69 addStar(new Point3D(x1, y1, z2), starSize);
73 private void addStar(final Point3D starLocation, double size) {
74 addShape(new GlowingPoint(starLocation, size, colors.get((int) (random() * colors.size()))));
77 private synchronized void ensureColorsAreInitialized() {
78 if (colors != null) return;
80 colors = new ArrayList<>();
82 for (int i = 0; i < 30; i++)