From bacc455b096932428cff56b7e1ce4052c2f1a7eb Mon Sep 17 00:00:00 2001 From: Ole Morud Date: Tue, 7 Jun 2022 16:01:19 +0200 Subject: [PATCH] function link_points added --- physics.c | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/physics.c b/physics.c index 541bdf4..b729d65 100644 --- a/physics.c +++ b/physics.c @@ -3,13 +3,14 @@ #include "graphics.h" #include #include +#include #define GRAVITY 0.01f #define FRAMERATE 10 typedef struct Point{ - double x, y; - px, py; + double x, y, + px, py, fx, fy; } Point; @@ -20,6 +21,7 @@ typedef struct Link { } Link; +Link* link_points(Point* a, Point* b); void draw_link(Canvas* c, Link* l); void update_point(Point* p); @@ -40,12 +42,12 @@ int main(){ 20.0, 10.0, 0.0, GRAVITY/FRAMERATE}; - Link l_ab = {&a, &b, 10}; + Link *l_ab = link_points(&a, &b); while(1){ // clear canvas, calculate next frame and draw memset(c.data, '.', c.x * c.y); - draw_link(&c, &l_ab); + draw_link(&c, l_ab); update_point(&a); update_point(&b); @@ -57,6 +59,22 @@ int main(){ return 0; } +/* + * Returns a link between two points + */ +Link* link_points(Point* a, Point* b){ + Link *output = malloc(sizeof(Link)); + + double dx = a->x - b->x; + double dy = a->y - b->y; + + output->a = a; + output->b = b; + output->length = sqrt( dx*dx + dy*dy ); + + return output; +} + /* * Draws a line between linked points */