Untitled

 avatar
unknown
plain_text
2 years ago
1.4 kB
3
Indexable
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define WIDTH 640
#define HEIGHT 480

// Load SDF data from a file
float* load_sdf(const char* filename, int size) {
    float* sdf = (float*)malloc(sizeof(float) * size * size);
    FILE* file = fopen(filename, "rb");
    fread(sdf, sizeof(float), size * size, file);
    fclose(file);
    return sdf;
}

// Calculate signed distance field for a circle
float circle_sdf(float x, float y, float cx, float cy, float r) {
    float dx = x - cx;
    float dy = y - cy;
    return sqrt(dx * dx + dy * dy) - r;
}

// Draw a circle using the preloaded SDF data
void draw_circle(float* sdf, float cx, float cy, float r, float scale) {
    int x, y;
    for (y = 0; y < HEIGHT; y++) {
        for (x = 0; x < WIDTH; x++) {
            float dx = (float)x / scale - cx;
            float dy = (float)y / scale - cy;
            float d = sdf[y * WIDTH + x] * scale + r;
            if (dx * dx + dy * dy < d * d) {
                printf("*");
            } else {
                printf(" ");
            }
        }
        printf("\n");
    }
}

int main() {
    // Load SDF data for a circle with radius 32
    int size = 32;
    float* sdf = load_sdf("circle.sdf", size);

    // Draw a circle at (320, 240) with radius 100 and scale 2
    float cx = 320, cy = 240, r = 100, scale = 2;
    draw_circle(sdf, cx, cy, r, scale);

    // Free memory for SDF data
    free(sdf);

    return 0;
}
Editor is loading...