2 * Sixth 3D engine. Author: Svjatoslav Agejenko.
3 * This project is released under Creative Commons Zero (CC0) license.
5 package eu.svjatoslav.sixth.e3d.examples.galaxy_demo;
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;
13 import java.util.ArrayList;
14 import java.util.List;
15 import java.util.Random;
17 import static java.lang.Math.*;
19 public class Galaxy extends AbstractCompositeShape {
21 private static List<Color> colors;
23 public Galaxy(final int galaxySize, final int tailCount, final int starsCount,
24 Transform transform) {
28 ensureColorsAreInitialized();
30 final double angle1 = random() * 10;
31 final double angle2 = random() * 10;
33 final double angleSin1 = sin(angle1);
34 final double angleCos1 = cos(angle1);
35 final double angleSin2 = sin(angle2);
36 final double angleCos2 = cos(angle2);
38 Random random = new Random();
40 double starSize = galaxySize / 70d;
42 for (int i = 1; i < starsCount; i++) {
43 final double b = random() * 10;
45 final double s = (b * b) / 30;
47 final double v1 = (random() * (11.5 - b)) / 3;
48 final double v1p = v1 / 2;
50 final double ane = ((random() * (s / 2)) / tailCount) * 2;
51 final double sba = ((2 * PI) / tailCount)
52 * random.nextInt(tailCount);
54 final double x = (((sin((b - sba) + ane) * s) + (random() * v1)) - v1p) * galaxySize;
55 final double z = (((cos((b - sba) + ane) * s) + (random() * v1)) - v1p) * galaxySize;
56 final double y = ((random() * v1) - v1p) * galaxySize;
58 final double x1 = (x * angleCos1) + (z * angleSin1);
59 final double z1 = (z * angleCos1) - (x * angleSin1);
61 final double y1 = (y * angleCos2) + (z1 * angleSin2);
62 final double z2 = (z1 * angleCos2) - (y * angleSin2);
64 addStar(new Point3D(x1, y1, z2), starSize);
68 private void addStar(final Point3D starLocation, double size) {
69 addShape(new GlowingPoint(starLocation, size, colors.get((int) (random() * colors.size()))));
72 private synchronized void ensureColorsAreInitialized() {
73 if (colors != null) return;
75 colors = new ArrayList<>();
77 for (int i = 0; i < 30; i++)