-
Notifications
You must be signed in to change notification settings - Fork 0
/
dot.c
175 lines (161 loc) · 3.86 KB
/
dot.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include "dot.h"
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
int yardInSteps(int yard) {
return -8*(yard/5) + 80;
}
Point *getPoint(Dot *dot) {
Point *point = malloc(sizeof(Point));
switch (dot->yref) {
case fsideline:
point->y = dot->ysteps;
break;
case bsideline:
point->y = 84+dot->ysteps;
break;
case fhash:
point->y = 28+dot->ysteps;
break;
case bhash:
point->y = 56+dot->ysteps;
break;
}
switch(dot->side) {
case 1:
point->x = (-1*yardInSteps(dot->yard))-dot->xsteps;
break;
case 2:
point->x = yardInSteps(dot->yard)+dot->xsteps;
break;
}
return point;
}
Dot *getDot(Point *point) {
Dot *dot = malloc(sizeof(Dot));
float xsteps = point->x*((point->x < 0)?-1:1);
dot->yard = 50-(((int)(xsteps/8)+((fmod(xsteps, 8) > 4)?1:0))*5);
if (point->x < 0) {
dot->side = 1;
dot->xsteps = -1*(point->x+(8*((50-dot->yard)/5)));
}
else {
dot->side = 2;
dot->xsteps = point->x-(8*((50-dot->yard)/5));
}
if (point->y < 14) {
dot->yref = fsideline;
dot->ysteps = point->y;
}
else if (point->y < 42) {
dot->yref = fhash;
dot->ysteps = point->y-28;
}
else if (point->y < 70) {
dot->yref = bhash;
dot->ysteps = point->y-56;
}
else {
dot->yref = bsideline;
dot->ysteps = point->y-84;
}
return dot;
}
Dot *midDot(Dot *one, Dot *two) {
Point *pone = getPoint(one);
Point *ptwo = getPoint(two);
Point *pmid = midPoint(pone, ptwo);
Dot *mid = getDot(pmid);
free(pone);
free(ptwo);
free(pmid);
return mid;
}
Point *midPoint(Point *one, Point *two) {
Point *mid = malloc(sizeof(Point));
mid->x = (one->x+two->x)/2;
mid->y = (one->y+two->y)/2;
return mid;
}
void printDot(Dot *dot) {
printf("Side %d: ", dot->side);
if (dot->xsteps < 0) {
printf("%.2f inside ", dot->xsteps*-1);
}
else if (dot->xsteps > 0) {
printf("%.2f outside ", dot->xsteps);
}
else {
printf("On ");
}
printf("the %d yardline\t", dot->yard);
if (dot->ysteps < 0) {
printf("%.2f steps in frnt of ", dot->ysteps*-1);
}
else if (dot->ysteps > 0) {
printf("%.2f steps behind ", dot->ysteps);
}
else {
printf("On ");
}
switch(dot->yref) {
case fsideline:
printf("front sideline");
break;
case fhash:
printf("front hash");
break;
case bhash:
printf("back hash");
break;
case bsideline:
printf("back sideline");
break;
}
printf("\n");
}
void uglyPrintDot(Dot *dot) {
printf("%d ", dot->side);
if (dot->xsteps < 0) {
printf("%.2f in ", dot->xsteps*-1);
}
else {
printf("%.2f out ", dot->xsteps);
}
printf("%d ", dot->yard);
if (dot->ysteps < 0) {
printf("%.2f front ", dot->ysteps*-1);
}
else {
printf("%.2f back ", dot->ysteps);
}
switch(dot->yref) {
case fsideline:
printf("fs");
break;
case fhash:
printf("fh");
break;
case bhash:
printf("bh");
break;
case bsideline:
printf("bs");
break;
}
printf("\n");
}
void printPoint(Point *point) {
printf("%.2f, %.2f\n", point->y, point->x);
}
double distanceDot(Dot *one, Dot *two) {
Point *pone = getPoint(one);
Point *ptwo = getPoint(two);
double d = distancePoint(pone, ptwo);
free(pone);
free(ptwo);
return d;
}
double distancePoint(Point *one, Point *two) {
return sqrt(pow(one->x-two->x, 2)+pow(one->y-two->y, 2));
}