0% found this document useful (0 votes)
2 views3 pages

CG Mini Project

This document contains a C program that simulates a pendulum clock using the SDL2 library. It sets up a window, performs physics calculations for the pendulum's motion, and renders the pendulum's position on the screen. The program includes functions for drawing a circle and handles events to keep the window responsive until closed.

Uploaded by

ankurkumar99421
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

CG Mini Project

This document contains a C program that simulates a pendulum clock using the SDL2 library. It sets up a window, performs physics calculations for the pendulum's motion, and renders the pendulum's position on the screen. The program includes functions for drawing a circle and handles events to keep the window responsive until closed.

Uploaded by

ankurkumar99421
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <SDL2/SDL.

h>
#include <math.h>
#include <stdbool.h>
#define M_PI 3.14159265358979323846

const int WIDTH = 800;


const int HEIGHT = 600;
const int RADIUS = 100; // Length of the pendulum
const float GRAVITY = 0.1f; // Gravity effect
const float DAMPING = 0.995f; // Damping factor

void drawCircle(SDL_Renderer *renderer, int x, int y, int radius) {


for (int w = 0; w < radius * 2; w++) {
for (int h = 0; h < radius * 2; h++) {
int dx = radius - w; // horizontal offset
int dy = radius - h; // vertical offset
if ((dx * dx + dy * dy) <= (radius * radius)) {
SDL_RenderDrawPoint(renderer, x + dx, y + dy);
}
}
}
}

int main() {
SDL_Init(SDL_INIT_VIDEO);

SDL_Window *window = SDL_CreateWindow("Pendulum Clock",


SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED, WIDTH, HEIGHT, 0);
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1,
SDL_RENDERER_ACCELERATED);

float angle = M_PI / 4; // Start at 45 degrees


float angleVelocity = 0.0f;
float angleAcceleration;

bool running = true;


SDL_Event event;

while (running) {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
running = false;
}
}

// Physics calculations
angleAcceleration = (-1 * GRAVITY / RADIUS) * sin(angle);
angleVelocity += angleAcceleration;
angleVelocity *= DAMPING; // Damping effect
angle += angleVelocity;

// Clear the screen


SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_RenderClear(renderer);

// Calculate pendulum position


int pendulumX = WIDTH / 2 + RADIUS * sin(angle);
int pendulumY = HEIGHT / 2 + RADIUS * cos(angle);
// Draw the pendulum
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderDrawLine(renderer, WIDTH / 2, HEIGHT / 2, pendulumX, pendulumY);
SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);
drawCircle(renderer, pendulumX, pendulumY, 10); // Draw pendulum bob

// Present the drawn frame


SDL_RenderPresent(renderer);
SDL_Delay(16); // Roughly 60 frames per second
}

SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();

return 0;
}

You might also like