Improved code readability
authorSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Wed, 22 Feb 2023 19:16:52 +0000 (21:16 +0200)
committerSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Wed, 22 Feb 2023 19:16:52 +0000 (21:16 +0200)
1  2 
src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/basic/GlowingPoint.java
src/main/java/eu/svjatoslav/sixth/e3d/renderer/raster/shapes/composite/Galaxy.java

@@@ -1,7 -1,12 +1,7 @@@
  /*
 - * Sixth 3D engine. Copyright ©2012-2018, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
 - *
 - * This program is free software; you can redistribute it and/or
 - * modify it under the terms of version 3 of the GNU Lesser General Public License
 - * or later as published by the Free Software Foundation.
 - *
 + * Sixth 3D engine. Author: Svjatoslav Agejenko. 
 + * This project is released under Creative Commons Zero (CC0) license.
   */
 -
  package eu.svjatoslav.sixth.e3d.renderer.raster.shapes.basic;
  
  import eu.svjatoslav.sixth.e3d.geometry.Point3D;
@@@ -12,15 -17,18 +12,18 @@@ import java.util.Collections
  import java.util.Set;
  import java.util.WeakHashMap;
  
+ import static java.lang.Math.pow;
+ import static java.lang.Math.sqrt;
  public class GlowingPoint extends ForwardOrientedTexture {
  
-     private static final int TEXTURE_SIZE = 50;
+     private static final int TEXTURE_RESOLUTION_PIXELS = 100;
      private static final Set<GlowingPoint> glowingPoints = Collections.newSetFromMap(new WeakHashMap<>());
      private final Color color;
  
      public GlowingPoint(final Point3D point, final double pointSize,
                          final Color color) {
-         super(point, pointSize, getTexture(color));
+         super(point, computeScale(pointSize), getTexture(color));
          this.color = color;
  
          synchronized (glowingPoints) {
          }
      }
  
+     private static double computeScale(double pointSize) {
+         return pointSize / ((double) (TEXTURE_RESOLUTION_PIXELS / 50f));
+     }
      private static Texture getTexture(final Color color) {
          // attempt to reuse texture from existing glowing point of the same color
          synchronized (glowingPoints) {
      }
  
      private static Texture createTexture(final Color color) {
-         final Texture texture = new Texture(TEXTURE_SIZE, TEXTURE_SIZE, 1);
-         for (int x = 0; x < TEXTURE_SIZE; x++)
-             for (int y = 0; y < TEXTURE_SIZE; y++) {
+         final Texture texture = new Texture(TEXTURE_RESOLUTION_PIXELS, TEXTURE_RESOLUTION_PIXELS, 1);
+         int halfResolution = TEXTURE_RESOLUTION_PIXELS / 2;
+         for (int x = 0; x < TEXTURE_RESOLUTION_PIXELS; x++)
+             for (int y = 0; y < TEXTURE_RESOLUTION_PIXELS; y++) {
                  int address = texture.primaryBitmap.getAddress(x, y);
  
-                 final int distance = (int) Math
-                         .sqrt((((TEXTURE_SIZE / 2) - x) * ((TEXTURE_SIZE / 2) - x))
-                                 + (((TEXTURE_SIZE / 2) - y) * ((TEXTURE_SIZE / 2) - y)));
+                 final int distanceFromCenter = (int) sqrt(pow (halfResolution - x, 2) + pow (halfResolution - y, 2));
  
-                 int alpha = 255 - ((270 * distance) / (TEXTURE_SIZE / 2));
+                 int alpha = 255 - ((270 * distanceFromCenter) / halfResolution);
                  if (alpha < 0)
                      alpha = 0;
  
@@@ -1,7 -1,12 +1,7 @@@
  /*
 - * Sixth 3D engine. Copyright ©2012-2018, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
 - *
 - * This program is free software; you can redistribute it and/or
 - * modify it under the terms of version 3 of the GNU Lesser General Public License
 - * or later as published by the Free Software Foundation.
 - *
 + * Sixth 3D engine. Author: Svjatoslav Agejenko. 
 + * This project is released under Creative Commons Zero (CC0) license.
   */
 -
  package eu.svjatoslav.sixth.e3d.renderer.raster.shapes.composite;
  
  import eu.svjatoslav.sixth.e3d.geometry.Point3D;
@@@ -14,44 -19,46 +14,46 @@@ import java.util.ArrayList
  import java.util.List;
  import java.util.Random;
  
+ import static java.lang.Math.*;
  public class Galaxy extends AbstractCompositeShape {
  
      private static List<Color> colors;
  
-     public Galaxy(final int size, final int tailCount, final int starsCount,
+     public Galaxy(final int galaxySize, final int tailCount, final int starsCount,
                    Transform transform) {
  
          super(transform);
  
          ensureColorsAreInitialized();
  
-         final double angle1 = Math.random() * 10;
-         final double angle2 = Math.random() * 10;
+         final double angle1 = random() * 10;
+         final double angle2 = random() * 10;
  
-         final double angleSin1 = Math.sin(angle1);
-         final double angleCos1 = Math.cos(angle1);
-         final double angleSin2 = Math.sin(angle2);
-         final double angleCos2 = Math.cos(angle2);
+         final double angleSin1 = sin(angle1);
+         final double angleCos1 = cos(angle1);
+         final double angleSin2 = sin(angle2);
+         final double angleCos2 = cos(angle2);
  
          Random random = new Random();
  
+         double starSize = galaxySize / 70d;
          for (int i = 1; i < starsCount; i++) {
-             final double b = Math.random() * 10;
+             final double b = random() * 10;
  
              final double s = (b * b) / 30;
  
-             final double v1 = (Math.random() * (11.5 - b)) / 3;
+             final double v1 = (random() * (11.5 - b)) / 3;
              final double v1p = v1 / 2;
  
-             final double ane = ((Math.random() * (s / 2)) / tailCount) * 2;
-             final double sba = ((2 * Math.PI) / tailCount)
+             final double ane = ((random() * (s / 2)) / tailCount) * 2;
+             final double sba = ((2 * PI) / tailCount)
                      * random.nextInt(tailCount);
  
-             final double x = (((Math.sin((b - sba) + ane) * s) + (Math.random() * v1)) - v1p)
-                     * size;
-             final double z = (((Math.cos((b - sba) + ane) * s) + (Math.random() * v1)) - v1p)
-                     * size;
-             final double y = ((Math.random() * v1) - v1p) * size;
+             final double x = (((sin((b - sba) + ane) * s) + (random() * v1)) - v1p) * galaxySize;
+             final double z = (((cos((b - sba) + ane) * s) + (random() * v1)) - v1p) * galaxySize;
+             final double y = ((random() * v1) - v1p) * galaxySize;
  
              final double x1 = (x * angleCos1) + (z * angleSin1);
              final double z1 = (z * angleCos1) - (x * angleSin1);
              final double y1 = (y * angleCos2) + (z1 * angleSin2);
              final double z2 = (z1 * angleCos2) - (y * angleSin2);
  
-             addStar(new Point3D(x1, y1, z2));
+             addStar(new Point3D(x1, y1, z2), starSize);
          }
      }
  
-     private void addStar(final Point3D starLocation) {
-         addShape(
-                 new GlowingPoint(starLocation, 10,
-                         colors.get((int) (Math.random() * colors.size()))));
+     private void addStar(final Point3D starLocation, double size) {
+         addShape(new GlowingPoint(starLocation, size, colors.get((int) (random() * colors.size()))));
      }
  
      private synchronized void ensureColorsAreInitialized() {
@@@ -77,9 -82,9 +77,9 @@@
          for (int i = 0; i < 30; i++)
              colors.add(
                      new Color(
-                             Math.random() + 0.5,
-                             Math.random() + 0.5,
-                             Math.random() + 0.5,
+                             random() + 0.5,
+                             random() + 0.5,
+                             random() + 0.5,
                              255));
      }