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.geometry.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 public class Galaxy extends AbstractCompositeShape {
24 private static List<Color> colors;
26 public Galaxy(final int size, final int tailCount, final int starsCount,
27 Transform transform) {
31 ensureColorsAreInitialized();
33 final double angle1 = Math.random() * 10;
34 final double angle2 = Math.random() * 10;
36 final double angleSin1 = Math.sin(angle1);
37 final double angleCos1 = Math.cos(angle1);
38 final double angleSin2 = Math.sin(angle2);
39 final double angleCos2 = Math.cos(angle2);
41 Random random = new Random();
43 for (int i = 1; i < starsCount; i++) {
44 final double b = Math.random() * 10;
46 final double s = (b * b) / 30;
48 final double v1 = (Math.random() * (11.5 - b)) / 3;
49 final double v1p = v1 / 2;
51 final double ane = ((Math.random() * (s / 2)) / tailCount) * 2;
52 final double sba = ((2 * Math.PI) / tailCount)
53 * random.nextInt(tailCount);
55 final double x = (((Math.sin((b - sba) + ane) * s) + (Math.random() * v1)) - v1p)
57 final double z = (((Math.cos((b - sba) + ane) * s) + (Math.random() * v1)) - v1p)
59 final double y = ((Math.random() * v1) - v1p) * size;
61 final double x1 = (x * angleCos1) + (z * angleSin1);
62 final double z1 = (z * angleCos1) - (x * angleSin1);
64 final double y1 = (y * angleCos2) + (z1 * angleSin2);
65 final double z2 = (z1 * angleCos2) - (y * angleSin2);
67 addStar(new Point3D(x1, y1, z2));
71 private void addStar(final Point3D starLocation) {
73 new GlowingPoint(starLocation, 10,
74 colors.get((int) (Math.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++)