summaryrefslogtreecommitdiff
path: root/src/lensdrac/lensdrac.c
blob: abcacb3df9c5e6037f9e9b125abf3dfa8a707a56 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
 * lensdraw.c - source file for trying things with SDL2.
 *
 * Copyright (C) 2025 https://optics-design.com
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * See the COPYING file for the full license text.
 */
#include <SDL.h>
#include <math.h>

#define SCREEN_WIDTH 800
#define SCREEN_HEIGHT 600

void draw_circle(SDL_Renderer *renderer, int cx, int cy, int r) {
    for (int x = -r; x <= r; x++) {
        int y = sqrt(r * r - x * x);  
        SDL_RenderDrawPoint(renderer, cx + x, cy + y);
        SDL_RenderDrawPoint(renderer, cx + x, cy - y);
    }
}

void draw_lens(SDL_Renderer *renderer, int cx, int cy, int r) {
    SDL_SetRenderDrawColor(renderer, 0, 200, 255, 255);  // Light blue color for lens

    for (int x = 0; x <= r; x++) {
        int y1 = sqrt(r * r - x * x);  
        int y2 = -y1;  

        for (int y = y2; y <= y1; y++) {  
            SDL_RenderDrawPoint(renderer, cx + x, cy + y);
        }
    }
}
int main(int argc, char* argv[]) {
    SDL_Init(SDL_INIT_VIDEO);
    SDL_Window *window = SDL_CreateWindow("Spherical Lens", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
    SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);

    SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);  // Black background
    SDL_RenderClear(renderer);

    draw_lens(renderer, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, 100);  // Draw lens

    SDL_RenderPresent(renderer);

    SDL_Delay(5000);  // Show for 5 seconds

    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    SDL_Quit();
    return 0;
}
Back to https://optics-design.com