More generic use of event.
[familia.git] / src / math / positions.c
1 #include <stdlib.h>
2 #include <math.h>
3
4 #include "positions.h"
5 #include "yearline.h"
6 #include "storage/individual.h"
7 #include "storage/family.h"
8
9 #include "../debug.h"
10
11 #define FAMILY_AVAIL_RANGE 180
12
13 struct position * calculate_individual_position(struct familia_individual * individual)
14 {
15         struct position * pos = NULL;
16         struct familia_family * parents = NULL;
17         unsigned int children_no = 1;
18         int nth = 0;
19
20         parents = familia_individual_get_parents(individual);
21         if (parents == NULL) {
22                 debug("TODO: Calculate position for root individual.");
23                 return NULL;
24         }
25
26         nth = familia_individual_nth_child_of_family(individual, parents);
27         if (nth < 0) {
28                 debug("Cannot calculate child number.");
29                 return NULL;
30         }
31
32         children_no = parents->children_no;
33
34         if (children_no == 0) {
35                 debug("Children number mismatch: I have child of family, " \
36                       "but parents does not have any children!");
37                 return NULL;
38         }
39
40         pos = (struct position *)malloc(sizeof(struct position));
41         pos->x = 0;
42         pos->y = 0;
43         pos->z = 0;
44
45         /* If there is only one child in family, place it vertically
46          * over the family (do not change x and z). For all other
47          * calculate these positions.
48          */
49         if (children_no != 1) {
50                 /* Angle by which every child is rotated. */
51                 float theta = 0;
52                 /* Angle by which this child will be rotated */
53                 float alpha = 0;
54                 theta = FAMILY_AVAIL_RANGE / children_no;
55
56                 /* We subtract from FAMILY_AVAIL_RANGE (e.g. 180),
57                         * to count angle from left instead of from right.
58                         * This makes oldest children display in this order.
59                 */
60                 alpha = FAMILY_AVAIL_RANGE - theta * nth;
61
62                 pos->x = cos(alpha);
63                 pos->z = sin(alpha);
64         }
65
66         /* pos.y is a local coordinate now. */
67         pos->y = convert_date_to_height(NULL);
68
69         return pos;
70 }
71
72 struct position * calculate_family_position(struct familia_family * family)
73 {
74 /*      TODO: Implement this function*/
75         return NULL;
76 }