Skip to content

Instantly share code, notes, and snippets.

@GuyInGrey
Created September 5, 2020 10:39
Show Gist options
  • Save GuyInGrey/2724fbcd25813eb0313427f2c33865cd to your computer and use it in GitHub Desktop.
Save GuyInGrey/2724fbcd25813eb0313427f2c33865cd to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Processing;
namespace EDStatistics
{
public class GalaxyDisplay : ProcessingCanvas
{
public List<StarSystem> systems;
public PSprite image;
Coordinates i = new Coordinates(-42213.81f, -29359.81f, -23405f);
Coordinates a = new Coordinates(40503.81f, 39518.34f, 65630.16f);
// display area
float left;
float right;
float top;
float bottom;
byte[] pixels;
PColor[] Colors = new[]
{
new PColor(0, 0, 0),
new PColor(0, 150, 255),
new PColor(0, 255, 0),
new PColor(255, 255, 0),
new PColor(255, 0, 255),
new PColor(255, 0, 0),
};
public GalaxyDisplay()
{
CreateCanvas(1000, 1000, 30);
}
public void Setup()
{
DefaultView();
//left = -500;
//right = 500;
//bottom = -500;
//top = 500;
AddKeyAction("E", (d) =>
{
if (!d) { return; }
left *= 0.9f;
right *= 0.9f;
top *= 0.9f;
bottom *= 0.9f;
Render();
});
AddKeyAction("Q", (d) =>
{
if (!d) { return; }
left *= 1.11111f;
right *= 1.11111f;
top *= 1.11111f;
bottom *= 1.11111f;
Render();
});
AddKeyAction("S", (d) =>
{
if (!d) { return; }
top -= (top - bottom) * 0.5f;
bottom -= (top - bottom) * 0.5f;
Render();
});
AddKeyAction("W", (d) =>
{
if (!d) { return; }
top += (top - bottom) * 0.5f;
bottom += (top - bottom) * 0.5f;
Render();
});
image = new PSprite(Width, Height);
Render();
}
public void Draw(float delta)
{
Art.Background(PColor.Black);
Art.DrawImage(image, 0, 0, Width, Height);
Title(FrameRateCurrent);
}
void Render()
{
var width = image.Width;
var height = image.Height;
image.Art.Background(PColor.Black);
pixels = image.Art.GetPixels();
var systemCount = (new FileInfo("systems.bin")).Length / 70;
var perRead = 100000;
var rl = right - left;
var bt = bottom - top;
var currentSystemIndex = 0L;
var counter = 0;
var density = new int[width, height];
var maxDensity = 0;
using (var fileStream = new FileStream("systems.bin", FileMode.Open, FileAccess.Read))
{
while (systemCount > 0)
{
var systemsToRead = systemCount < perRead ? systemCount : perRead;
systemCount -= systemsToRead;
var buffer = new byte[systemsToRead * 70];
fileStream.Read(buffer, 0, (int)systemsToRead * 70);
currentSystemIndex += systemsToRead;
Parallel.For(0, systemsToRead, i =>
{
counter++;
var bytes = new byte[70];
Array.Copy(buffer, 70 * i, bytes, 0, 70);
var x = BitConverter.ToSingle(bytes, 8);
//var y = BitConverter.ToSingle(bytes, 12);
var z = BitConverter.ToSingle(bytes, 16);
var pX = (int)Math.Round(((x - left) / rl) * width);
var pY = (int)Math.Round(((z - top) / bt) * height);
if (pX < 0 || pX >= width || pY < 0 || pY >= height) { return; }
density[pX, pY]++;
if (density[pX, pY] > maxDensity) { maxDensity = density[pX, pY]; }
});
}
}
for (var y = 0; y < height; y++)
{
for (var x = 0; x < width; x++)
{
if (density[x, y] == 0) { continue; }
var address = (x + (y * width)) * 4;
if (!(address >= pixels.Length || address < 0))
{
var colorPercent = density[x, y] / (float)maxDensity;
//colorPercent = f(colorPercent);
colorPercent = -(float)Math.Pow(colorPercent - 1, 24f) + 1;
var color = PColor.LerpMultiple(Colors, colorPercent > 1 ? 1 : colorPercent);
pixels[address + 0] = (byte)color.B;
pixels[address + 1] = (byte)color.G;
pixels[address + 2] = (byte)color.R;
pixels[address + 3] = 255;
//pixels[address + 0] = 0; // Blue
//pixels[address + 1] = 0; // Green
//pixels[address + 2] = 255; // Red
//pixels[address + 3] = 255; // Alpha
}
}
}
Console.WriteLine(counter);
image.Art.SetPixels(pixels);
}
void DefaultView()
{
left = i.x;
right = a.x;
top = a.z;
bottom = i.z;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment