Skip to content

Instantly share code, notes, and snippets.

@ForLoopCodes
Created November 7, 2025 15:08
Show Gist options
  • Select an option

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

Select an option

Save ForLoopCodes/cd2f156ead3780986757d4b633816a62 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] = ' ';
}
}
char shade_chars[] = ".:-=+*#%@";
int num_shades = sizeof(shade_chars) - 1;
double angle = 0.0;
while (1)
{
double Lx = cos(angle);
double Ly = sin(angle) * 0.5;
double Lz = -0.7;
double l_norm_constant = sqrt(Lx * Lx + Ly * Ly + Lz * Lz);
if (l_norm_constant != 0.0)
{
Lx /= l_norm_constant;
Ly /= l_norm_constant;
Lz /= l_norm_constant;
}
for (int i = 0; i < RADIUS * 2; i++)
{
for (int j = 0; j < RADIUS * 4; j++)
{
grid[i][j] = ' ';
}
}
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];
}
}
}
printf("\e[1;1H\e[2J");
for (int i = 0; i < RADIUS * 2; i++)
{
printf("%.*s\n", RADIUS * 4, grid[i]);
}
angle += 0.1;
Sleep(50);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment