#include #include #include #include #include #include "display.h" #define W 800 #define H 600 #ifndef PLATFORM_WEB #define FPS 120 #else #define FPS 60 #endif #define SCALE 800 #define CYCLES 5 #define RFPS 1/FPS #define PI 3.141592654 #define g -9.81f * SCALE float s = 25; // starting agnle float l = 0.5; // lenght float m = 0.01; // mass in kg float t; float v; float a; float secs = 0.0f; int swings = 2 * CYCLES; int has_reached_starting_amp(float old_t) { if(swings % 2 == 0 && t < 0) { if(old_t < t) return 1; } else if(swings % 2 == 1 && t > 0) { if(old_t > t) return 1; } return 0; } void update() { if(secs == 0.0f) { printf("\nStarting angle: %f", s/(PI/180)); printf("\nLenght: %f", l/SCALE); printf("\nMass: %f\n", m/SCALE); } float old_t = t; a = (g/l) * sinf(t); v += a * RFPS; t += v * RFPS; if(has_reached_starting_amp(old_t)) { swings += 1; } if(swings >= 2 * CYCLES) { printf("period: %f\n", secs/CYCLES); secs = 0.0f; swings = 0; } secs += (float)RFPS; float px = sinf(t) * l; float py = cosf(t) * l; dspl_draw_line(px, py, 0, 0, 0); dspl_draw_circle(px, py, m); } int main(int argc, char **argv) { if(argc != 1 && argc != 4) { puts("Please provide either 0 or 3 arguments:\n\t1st is starting angle in degrees\n\t2nd is lenght in meters\n\t3rd is mass in kilograms"); return 1; } float arg_s = strtof(argv[1], NULL); float arg_l = strtof(argv[2], NULL); float arg_m = strtof(argv[3], NULL); s = ((arg_s == 0.0f) ? s : arg_s) * PI/180; l = ((arg_l == 0.0f) ? l : arg_l) * SCALE; m = ((arg_m == 0.0f) ? m : arg_m) * SCALE; t = s; v = 0; a = 0; dspl_createinfo info = {0}; info.width = W; info.height = H; info.name = "Pendulum"; info.fps = FPS; info.update_func = &update; dspl_create(info); dspl_start(); dspl_destroy(); return 0; }