Skip to content

Instantly share code, notes, and snippets.

@ForLoopCodes
Created November 7, 2025 14:43
Show Gist options
  • Select an option

  • Save ForLoopCodes/ae980f50df5122f8e1682fcbfec9e236 to your computer and use it in GitHub Desktop.

Select an option

Save ForLoopCodes/ae980f50df5122f8e1682fcbfec9e236 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <math.h>
#define PI 3.14159265358979323846
#define RADIUS 10
int main()
{
char grid[RADIUS * 2][RADIUS * 4];
for (int i = 0; i < RADIUS * 2; i++)
{
for (int j = 0; j < RADIUS * 4; j++)
{
grid[i][j] = ' ';
}
}
double Lx = 1.0 / sqrt(3.0);
double Ly = -1.0 / sqrt(3.0);
double Lz = -1.0 / sqrt(3.0);
char shade_chars[] = ".:-=+*#%@";
int num_shades = sizeof(shade_chars) - 1;
for (double phi = 0.0; phi <= PI; phi += 0.01)
{
for (double theta = 0.0; theta < 2.0 * PI; theta += 0.01)
{
double x_norm = sin(phi) * cos(theta);
double y_norm = sin(phi) * sin(theta);
double z_norm = cos(phi);
int sx = (int)(RADIUS * (x_norm + 1.0) * 2);
int sy = (int)(RADIUS * (y_norm + 1.0));
if (sx >= 0 && sx < RADIUS * 4 && sy >= 0 && sy < RADIUS * 2)
{
double dot = x_norm * Lx + y_norm * Ly + z_norm * Lz;
if (dot < 0.0)
dot = 0.0;
int shade_index = (int)(dot * num_shades);
grid[sy][sx] = shade_chars[shade_index];
}
}
}
for (int i = 0; i < RADIUS * 2; i++)
{
printf("%.*s\n", RADIUS * 4, grid[i]);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment