Created
November 7, 2025 19:32
-
-
Save skeeto/e9be81bb6357a65ce2908e643fdbf188 to your computer and use it in GitHub Desktop.
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
| // Ref: https://old.reddit.com/r/C_Programming/comments/1oqxeeq | |
| // This is free and unencumbered software released into the public domain. | |
| #include <math.h> | |
| #include <stdio.h> | |
| #define lenof(a) (int)(sizeof(a) / sizeof(*(a))) | |
| typedef struct { float x, y; } V2; | |
| typedef struct { float x, y, z; } V3; | |
| static V2 mul(V2 v, float s) | |
| { | |
| v.x *= s; | |
| v.y *= s; | |
| return v; | |
| } | |
| static V2 add(V2 a, V2 b) | |
| { | |
| a.x += b.x; | |
| a.y += b.y; | |
| return a; | |
| } | |
| static V2 translate(V2 p, V3 *v, int len, float t) | |
| { | |
| for (int i = 0; i < len; i++) { | |
| p.x += v[i].x * cosf(t*v[i].y + v[i].z); | |
| p.y += v[i].x * sinf(t*v[i].y + v[i].z); | |
| } | |
| return p; | |
| } | |
| static void line(V2 a, V2 b, int color) | |
| { | |
| printf( | |
| " <line x1='%.3g' y1='%.3g' x2='%.3g' y2='%.3g'" | |
| " style='stroke: #%06x; stroke-width: 2px;'/>\n", | |
| (double)a.x, (double)a.y, (double)b.x, (double)b.y, color | |
| ); | |
| } | |
| int main() | |
| { | |
| V3 adobe[] = { // magnitude, angular velocity, phase | |
| {1.0f, -1.0f, 0.0f}, | |
| {1.0f, 2.0f, 0.0f}, | |
| {0.5f, -1.0f, 0.0f}, | |
| }; | |
| enum { size=1000, end=1000, div=100 }; | |
| V2 center = {size/2, size/2}; | |
| float scale = 100; | |
| printf( | |
| "<svg" | |
| " version='1.1'" | |
| " xmlns='http://www.w3.org/2000/svg'" | |
| " width='%d'" | |
| " height='%d'" | |
| ">\n", size, size | |
| ); | |
| V2 prev = translate((V2){}, adobe, lenof(adobe), 0); | |
| for (int t = 0; t < end; t++) { | |
| V2 next = translate((V2){}, adobe, lenof(adobe), (float)t/div); | |
| line( | |
| add(center, mul(prev, scale)), | |
| add(center, mul(next, scale)), | |
| 0x000000 | |
| ); | |
| prev = next; | |
| } | |
| puts("</svg>"); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment