physics
This commit is contained in:
34
graphics.c
34
graphics.c
@@ -19,27 +19,27 @@ typedef struct Canvas {
|
|||||||
void clear();
|
void clear();
|
||||||
void render(Canvas* c);
|
void render(Canvas* c);
|
||||||
void line(Canvas* c, uint start_x, uint start_y, uint end_x, uint end_y);
|
void line(Canvas* c, uint start_x, uint start_y, uint end_x, uint end_y);
|
||||||
void dot(Canvas* c, uint x, uint y);
|
void dot(Canvas* c, const uint x, const uint y);
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* main
|
* main
|
||||||
*/
|
*/
|
||||||
int main(){
|
//int main(){
|
||||||
char *data = malloc(40*40);
|
// char *data = malloc(40*40);
|
||||||
Canvas c = {40, 40, data};
|
// Canvas c = {40, 40, data};
|
||||||
memset(c.data, OFF, c.x * c.y);
|
// memset(c.data, OFF, c.x * c.y);
|
||||||
|
//
|
||||||
//line(&c, 1, 1, 8, 8);
|
// //line(&c, 1, 1, 8, 8);
|
||||||
//line(&c, 8, 8, 1, 1);
|
// //line(&c, 8, 8, 1, 1);
|
||||||
//line(&c, 0, 5, 8, 8);
|
// //line(&c, 0, 5, 8, 8);
|
||||||
line(&c, 8, 8, 35, 39);
|
// line(&c, 8, 8, 35, 39);
|
||||||
|
//
|
||||||
|
//
|
||||||
render(&c);
|
// render(&c);
|
||||||
|
//
|
||||||
return 0;
|
// return 0;
|
||||||
}
|
//}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -71,7 +71,7 @@ void render(Canvas* c){
|
|||||||
/*
|
/*
|
||||||
* Sets pixel at x,y to ON
|
* Sets pixel at x,y to ON
|
||||||
*/
|
*/
|
||||||
void dot(Canvas* c, uint x, uint y){
|
void dot(Canvas* c, const uint x, const uint y){
|
||||||
c->data[ y*(c->x) + x] = ON;
|
c->data[ y*(c->x) + x] = ON;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
16
graphics.h
Normal file
16
graphics.h
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
|
||||||
|
#ifndef GRAPHICS_H_
|
||||||
|
#define GRAPHICS_H_
|
||||||
|
|
||||||
|
typedef struct Canvas {
|
||||||
|
int x;
|
||||||
|
int y;
|
||||||
|
char* data;
|
||||||
|
} Canvas;
|
||||||
|
|
||||||
|
void clear();
|
||||||
|
void render(Canvas*);
|
||||||
|
void line(Canvas*, unsigned int, unsigned int, unsigned int, unsigned int);
|
||||||
|
void dot(Canvas*, const unsigned int, const unsigned int);
|
||||||
|
|
||||||
|
#endif
|
||||||
64
physics.c
Normal file
64
physics.c
Normal file
@@ -0,0 +1,64 @@
|
|||||||
|
|
||||||
|
|
||||||
|
#include "graphics.h"
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
|
typedef struct Point{
|
||||||
|
uint x;
|
||||||
|
uint y;
|
||||||
|
uint px;
|
||||||
|
uint py;
|
||||||
|
uint fx;
|
||||||
|
uint fy;
|
||||||
|
} Point;
|
||||||
|
|
||||||
|
typedef struct Link {
|
||||||
|
Point* a;
|
||||||
|
Point* b;
|
||||||
|
double length;
|
||||||
|
} Link;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void draw_link(Canvas* c, Link* l){
|
||||||
|
line(&c, l->a->x, l->a->y, l->b->x, l->b->y);
|
||||||
|
}
|
||||||
|
|
||||||
|
void update_point(Point* p){
|
||||||
|
uint tempx = p->x;
|
||||||
|
uint tempy = p->y;
|
||||||
|
p->x += (p->x - p->px) + fx;
|
||||||
|
p->y += (p->y - p->py) + fy;
|
||||||
|
p->px = tempx;
|
||||||
|
p->py = tempy;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int main(){
|
||||||
|
char *data = malloc(40*40);
|
||||||
|
Canvas c = {40, 40, data};
|
||||||
|
memset(c.data, '.', c.x * c.y);
|
||||||
|
|
||||||
|
|
||||||
|
Point a = {10, 10, 10, 10, 0, 5};
|
||||||
|
Point b = {20, 10, 10, 10, 0, 5};
|
||||||
|
Link l_ab = {&a, &b, 10};
|
||||||
|
|
||||||
|
//line(&c, 1, 1, 8, 8);
|
||||||
|
//line(&c, 8, 8, 1, 1);
|
||||||
|
//line(&c, 0, 5, 8, 8);
|
||||||
|
|
||||||
|
for(int i=0; i<100; i++){
|
||||||
|
draw_link(&c, &l_ab);
|
||||||
|
update_point(&a);
|
||||||
|
update_point(&b);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
render(&c);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user