Created
June 14, 2026 16:10
-
-
Save eduardoleon/8635a3d9465175a4b9e1fc16d63c0e96 to your computer and use it in GitHub Desktop.
24-cell animation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #define SDL_MAIN_USE_CALLBACKS | |
| #include <SDL3/SDL.h> | |
| #include <SDL3/SDL_main.h> | |
| #include <stdlib.h> | |
| #include <stdio.h> | |
| static SDL_Window *window = NULL; | |
| static SDL_Renderer *renderer = NULL; | |
| typedef struct LP_Quaternion { | |
| float w, x, y, z; | |
| } LP_Quaternion; | |
| static LP_Quaternion logical[97]; | |
| static SDL_FPoint screen [97]; | |
| LP_Quaternion LP_QuaternionMultiply (LP_Quaternion lhs, LP_Quaternion rhs) | |
| { | |
| return (LP_Quaternion) { | |
| .w = lhs.w * rhs.w - lhs.x * rhs.x - lhs.y * rhs.y - lhs.z * rhs.z, | |
| .x = lhs.w * rhs.x + lhs.x * rhs.w + lhs.y * rhs.z - lhs.z * rhs.y, | |
| .y = lhs.w * rhs.y - lhs.x * rhs.z + lhs.y * rhs.w + lhs.z * rhs.x, | |
| .z = lhs.w * rhs.z + lhs.x * rhs.y - lhs.y * rhs.x + lhs.z * rhs.w | |
| }; | |
| } | |
| LP_Quaternion LP_QuaternionRotate (LP_Quaternion g) | |
| { | |
| return (LP_Quaternion) { .w = g.w, .x = g.x, .y = -g.z, .z = g.y }; | |
| } | |
| void LP_GeneratePath () | |
| { | |
| LP_Quaternion g = { .w = 0.5f, .x = 0.5f, .y = 0.5f, .z = -0.5f }; | |
| LP_Quaternion h = { .w = 0.5f, .x = 0.5f, .y = -0.5f, .z = 0.5f }; | |
| logical[0] = (LP_Quaternion) { .w = 1.0f, .x = 1.0f, .y = 0.0f, .z = 0.0f }; | |
| for (int i = 0, j = 0; i < 4; i++) | |
| { | |
| for (int k = 0; k < 4; k++, j += 6) | |
| { | |
| logical[j+1] = LP_QuaternionMultiply (logical[j] , g); | |
| logical[j+2] = LP_QuaternionMultiply (logical[j+1], h); | |
| logical[j+3] = LP_QuaternionMultiply (logical[j+2], h); | |
| logical[j+4] = LP_QuaternionMultiply (logical[j+3], g); | |
| logical[j+5] = LP_QuaternionMultiply (logical[j+4], h); | |
| logical[j+6] = LP_QuaternionMultiply (logical[j+5], g); | |
| } | |
| g = LP_QuaternionRotate (g); | |
| h = LP_QuaternionRotate (h); | |
| } | |
| for (int i = 0; i < 97; i++) | |
| { | |
| printf ("%f %f %f %f\n", logical[i].w, logical[i].x, logical[i].y, logical[i].z); | |
| } | |
| } | |
| void LP_Project () | |
| { | |
| double now = SDL_GetTicks (); | |
| double cos1 = SDL_cos (now / 1000.0); | |
| double sin1 = SDL_sin (now / 1000.0); | |
| double cos2 = SDL_cos (now / 600.0); | |
| double sin2 = SDL_sin (now / 600.0); | |
| for (int i = 0; i < 97; i++) | |
| { | |
| double x = logical[i].w * cos1 - logical[i].x * sin1; | |
| double y = logical[i].w * sin1 + logical[i].x * cos1; | |
| double z = logical[i].y * cos2 - logical[i].z * sin2; | |
| screen[i].x = 400 + 300 * x / (2 + z); | |
| screen[i].y = 300 + 300 * y / (2 + z); | |
| } | |
| } | |
| SDL_AppResult SDL_AppInit (void **appstate, int argc, char *argv[]) | |
| { | |
| SDL_SetAppMetadata ("Example Renderer Clear", "1.0", "com.example.24-cell"); | |
| if (!SDL_Init (SDL_INIT_VIDEO)) | |
| { | |
| SDL_Log ("Couldn't initialize SDL: %s", SDL_GetError ()); | |
| return SDL_APP_FAILURE; | |
| } | |
| window = SDL_CreateWindow ("Renderer Example", 800, 600, SDL_WINDOW_UTILITY); | |
| if (window == NULL) | |
| { | |
| SDL_Log ("Couldn't create window: %s", SDL_GetError ()); | |
| return SDL_APP_FAILURE; | |
| } | |
| renderer = SDL_CreateRenderer (window, NULL); | |
| if (renderer == NULL) | |
| { | |
| SDL_Log ("Couldn't create renderer: %s", SDL_GetError ()); | |
| return SDL_APP_FAILURE; | |
| } | |
| LP_GeneratePath (); | |
| return SDL_APP_CONTINUE; | |
| } | |
| SDL_AppResult SDL_AppEvent (void *appstate, SDL_Event *event) | |
| { | |
| return (event->type == SDL_EVENT_QUIT) ? SDL_APP_SUCCESS : SDL_APP_CONTINUE; | |
| } | |
| SDL_AppResult SDL_AppIterate (void *appstate) | |
| { | |
| LP_Project (); | |
| SDL_SetRenderDrawColor (renderer, 255, 255, 255, SDL_ALPHA_OPAQUE); | |
| SDL_RenderClear (renderer); | |
| SDL_SetRenderDrawColor (renderer, 0, 0, 0, SDL_ALPHA_OPAQUE); | |
| SDL_RenderLines (renderer, screen, 25); | |
| SDL_SetRenderDrawColor (renderer, 127, 0, 0, SDL_ALPHA_OPAQUE); | |
| SDL_RenderLines (renderer, screen + 24, 25); | |
| SDL_SetRenderDrawColor (renderer, 0, 127, 0, SDL_ALPHA_OPAQUE); | |
| SDL_RenderLines (renderer, screen + 48, 25); | |
| SDL_SetRenderDrawColor (renderer, 0, 0, 127, SDL_ALPHA_OPAQUE); | |
| SDL_RenderLines (renderer, screen + 72, 25); | |
| SDL_RenderPresent (renderer); | |
| return SDL_APP_CONTINUE; | |
| } | |
| void SDL_AppQuit (void *appstate, SDL_AppResult result) | |
| { | |
| SDL_DestroyRenderer (renderer); | |
| SDL_DestroyWindow (window); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment