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
#ifndef __BODY_H__
#define __BODY_H__
#include "color.h"
#include "list.h"
#include "vector.h"
typedef struct body body_t;
/**
* Initialize a body object given a list of vertices.
*
* @param points the list of vertices that make up the body. The body takes
* ownership of the list.
* @param initial_velocity a vector representing the initial velocity of the
* body
* @param rotation_speed the rotation angle of the body per unit time
* @param color the color of the body
* @return a body object pointer
*/
body_t *body_init(list_t *points, vector_t initial_velocity,
double rotation_speed, color_t color);
/**
* Return the list of vectors representing the vertices of the body.
*
* @param body the pointer to the body
* @return a list of vectors
*/
list_t *body_get_points(body_t *body);
/**
* Translate and rotate the body.
*
* @param body the pointer to the body
* @param time_elapsed time/# of frames elapsed since the last tick
*/
void body_move(body_t *body, double time_elapsed);
/**
* Set the x and y components of a body's velocity vector.
*
* @param body the pointer to the body
* @param v_x x component of velocity to set
* @param v_y y component of velocity to set
*/
void body_set_velocity(body_t *body, double v_x, double v_y);
/**
* Free memory allocated for object associated with a body.
*
* @param body the pointer to the body
*/
void body_free(body_t *body);
/**
* Return x-component of body's velocity vector.
*
* @param body the pointer to the body
* @return the x-velocity of a body
*/
double body_get_velocity_x(body_t *body);
/**
* Return y-component of body's velocity vector.
*
* @param body the pointer to the body
* @return the y-velocity of a body
*/
double body_get_velocity_y(body_t *body);
/**
* Computes the area of a body.
* See https://en.wikipedia.org/wiki/Shoelace_formula#Statement.
*
* @param body the pointer to the body
* @return the area of the body
*/
double body_area(body_t *body);
/**
* Computes the center of mass of a body.
* See https://en.wikipedia.org/wiki/Centroid#Of_a_polygon.
*
* @param body the pointer to the body
* @return the centroid of the body
*/
vector_t body_centroid(body_t *body);
/**
* Translates all vertices in a body by a given vector.
* Note: mutates the original body.
*
* @param body the pointer to the body
* @param translation the vector to add to each vertex's position
*/
void body_translate(body_t *body, vector_t translation);
/**
* Rotates vertices in a body by a given angle about a given point.
*
* @param body the pointer to the body
* @param angle the angle to rotate the body, in radians.
* A positive angle means counterclockwise.
* @param point the point to rotate around
*/
void body_rotate(body_t *body, double angle, vector_t point);
/**
* Return the body's color.
*
* @param body the pointer to the body
* @return the color_t struct representing the color
*/
color_t body_get_color(body_t *body);
#endif // #ifndef __BODY_H__