/
newmain.c
144 lines (108 loc) · 2.63 KB
/
newmain.c
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#ifdef ANIMATE
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#define X_SIZE 800
#define Y_SIZE 800
#endif
#define prec float
#define PI 3.14159265359
static prec gdt = 0.0001;
typedef struct body {
int position_x;
int position_y;
int mass;
int velocity_x;
int velocity_y;
int acceleration_x;
int acceleration_y;
} body;
static void update(body* a, prec dt)
{
}
static void resetForce(body* b) {
}
static float distance(body a, body b) {
// int test = a.position_x;
int x = (a.position_x - b.position_x) * (a.position_x - b.position_x);
int y = (a.position_y - b.position_y) * (a.position_y - b.position_y);
float distance = sqrt (x+y);
return distance;
}
static void addForce(body* a, body* b)
{
}
static prec newRand()
{
prec r = (prec)((double)rand()/(double)RAND_MAX);
return r;
}
void init(int N, body* star)
{
}
static void updateForces(int N, body* star)
{
}
// Manually copy coordinates from stars into points (to be drawn).
// Look at the manual file for XPoint to see which
// format XPoint accepts its coordinates in.
#ifdef ANIMATE
static void copyToXBuffer(body* star, XPoint* points, int N)
{
}
#endif
int main(int argc, char* argv[]) {
struct body a = {2, 2, 0, 0, 0, 0, 0};
struct body b = {1, 1, 0, 0, 0, 0, 0};
int N = 200;
int iter = 1000;
if(argc == 1) {
float dist = distance (a, b);
printf("Test distance: %f\n", dist);
}
if(argc == 3)
{
N = atoi(argv[1]);
iter = atoi(argv[2]);
}
#ifdef ANIMATE
XPoint* points = malloc(sizeof(XPoint)*N);
Display* disp;
Window window, rootwin;
int screen;
disp = XOpenDisplay(NULL);
screen = DefaultScreen(disp);
rootwin = RootWindow(disp,screen);
window = XCreateSimpleWindow(disp,rootwin,
0,0,X_SIZE,Y_SIZE,1,0,0);
GC gc = XCreateGC(disp, window, 0, 0);
XSetForeground(disp, gc, WhitePixel(disp, screen));
XSetBackground(disp, gc, BlackPixel(disp, screen));
XMapWindow(disp,window);
XClearWindow(disp,window);
copyToXBuffer(star, points, N);
XDrawPoints(disp, window, gc, points, N, 0);
XFlush(disp);
#endif
clock_t start = clock();
for(int i = 0; i < iter; i++)
{
#ifdef ANIMATE
copyToXBuffer(star, points, N);
XDrawPoints(disp, window, gc, points, N, CoordModeOrigin);
XClearWindow(disp,window);
#endif
}
clock_t stop = clock();
float diff = (float)(stop - start)/CLOCKS_PER_SEC;
printf("Total: %lf seconds\n",diff);
printf("Bodies: %d\n",N);
printf("Iterations: %d\n", iter);
#ifdef ANIMATE
XCloseDisplay(disp);
#endif
return 0;
}