-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsatc.h
1469 lines (1353 loc) · 43.6 KB
/
satc.h
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* vim: set ft=c: */
/** @file */
#ifndef __SATC__
#define __SATC__
#include "stdio.h"
#include "stdlib.h"
#include "stdbool.h"
#include "math.h"
#include "float.h"
// -------------------------------------
// Forward declarations for the structs.
// -------------------------------------
/** The type of a circle struct. */
typedef struct satc_circle satc_circle_t;
/** The type of a polygon struct. */
typedef struct satc_polygon satc_polygon_t;
/** The type of a box struct. */
typedef struct satc_box satc_box_t;
/** The type of a response struct. */
typedef struct satc_response satc_response_t;
/** A circle shape, with a position and radius. */
struct satc_circle {
/** The shape type of the struct. */
int type;
/** The position of the circle. */
double *pos;
/** The radius of the circle. */
double r;
};
/** A polygon shape, with a position, angle, offset, and points. */
struct satc_polygon {
/** The shape type of the struct. */
int type;
/** The position of the polygon. */
double *pos;
/** The number of points in the polygon. */
size_t num_points;
/**
* The list of points in the polygon, as an array of arrays of doubles.
*
* Use `satc_polygon_copy_points` to change this. Or, if you need to do it
* manually, call `_satc_polygon_recalc` afterward.
*/
double **points;
/**
* The angle of rotation of the polygon.
*
* Use `satc_polygon_set_angle` to change this. Or, if you need to do it
* manually, call `_satc_polygon_recalc` afterward.
*/
double angle;
/**
* The offset as an array of doubles (a point) of the polygon.
*
* Use `satc_polygon_set_offset` to change this. Or, if you need to do it
* manually, call `_satc_polygon_recalc` afterward.
*/
double *offset;
/**
* The number of calculated points for the polygon. This will match the
* number of points.
*
* This should not be modified manually.
*/
size_t num_calc_points;
/**
* An array of arrays of doubles (array of points) representing the
* calculated points of the polygon. This will match the number of points.
*
* This should not be modified manually.
*/
double **calc_points;
/**
* The number of calculated edges for the polygon. This will match the
* number of points.
*
* This should not be modified manually.
*/
size_t num_edges;
/**
* An array of arrays of doubles (array of points) representing the
* calculated edges of the polygon. This will match the number of points.
*
* This should not be modified manually.
*/
double **edges;
/**
* The number of calculated normals for the polygon. This will match the
* number of points.
*
* This should not be modified manually.
*/
size_t num_normals;
/**
* An array of arrays of doubles (array of points) representing the
* calculated normals of the polygon. This will match the number of points.
*
* This should not be modified manually.
*/
double **normals;
};
/** A box shape, with a position, width, and height. */
struct satc_box {
/** The shape type of the struct. */
int type;
/** The position of the box. */
double *pos;
/** The width of the box. */
double w;
/** The height of the box. */
double h;
};
/** A response, representing an overlap between two shapes. */
struct satc_response {
/** The first shape participating in the collision. */
void *a;
/** The second shape participating in the collision. */
void *b;
/** The length of overlap of the collision. */
double overlap;
/** The unit vector of the overlap of the collision. */
double *overlap_n;
/**
* The unit vector of the overlap of the collision, scaled by the length of
* overlap.
*/
double *overlap_v;
/** True if shape `a` is entirely within shape `b`. */
bool a_in_b;
/** True if shape `b` is entirely within shape `a`. */
bool b_in_a;
};
// ------
// Macros
// ------
/** Denotes an undefined type in a struct with a `type` field. */
#define satc_type_none 0
/** Denotes a circle type in a struct with a `type` field. */
#define satc_type_circle 1
/** Denotes a polygon type in a struct with a `type` field. */
#define satc_type_polygon 2
/** Denotes a box type in a struct with a `type` field. */
#define satc_type_box 3
/** Given a struct, returns an int representing the type.. */
#define satc_shape_get_type(s) s->type;
/**
* Creates an array of doubles, with undefined values. Since it uses `alloca`,
* the array will automatically be deallocated when it falls out of scope.
*
* This macro is considered a statement.
*
* @param name the variable name for the array.
* @param size the number of doubles in the array.
*/
#define satc_double_array_alloca(name, size) \
double *name = NULL; \
name = (double *) alloca(sizeof(double) * size);
/**
* Creates an array of pointers to `double *` (a point), with undefined,
* not-yet-allocated pointer values.
*
* Since it uses `alloca`, the array will automatically be deallocated when it
* falls out of scope.
*
* The members will not be automatically deallocated when the array falls out
* of scope, unless the members were allocated with `alloca` too!
*
* This macro is considered a statement.
*
* @param name the variable name for the array.
* @param size the number of pointers to points in the array.
*/
#define satc_point_array_alloca(name, size) \
double **name = NULL; \
name = (double **) alloca(sizeof(double *) * size);
/**
* Creates a pointer to an array of doubles (a point), with undefined `x` and
* `y` values.
*
* Since it uses `alloca`, the point will automatically be deallocated when it
* falls out of scope.
*
* This macro is considered a statement.
*
* @param name the variable name for the point.
*/
#define satc_point_alloca(name) \
double *name = NULL; \
name = (double *) alloca(sizeof(double) * 2);
/**
* Creates a pointer to an array of doubles (a point), with defined `x` and `y`
* values.
*
* Since it uses `alloca`, the point will automatically be deallocated when it
* falls out of scope.
*
* This macro is considered a statement.
*
* @param name the variable name for the array.
* @param x the x value, as a double, for the point.
* @param y the y value, as a double, for the point.
*/
#define satc_point_alloca_xy(name, x, y) \
satc_point_alloca(name); \
satc_point_set_xy(name, x, y);
/** Denotes a left voronoi region, for polygon collision detection. */
#define SATC_LEFT_VORONOI_REGION -1
/** Denotes a middle voronoi region, for polygon collision detection. */
#define SATC_MIDDLE_VORONOI_REGION 0
/** Denotes a right voronoi region, for polygon collision detection. */
#define SATC_RIGHT_VORONOI_REGION 1
/** The index of the double array which contains the `x` value.. */
#define SATC_POINT_X 0
/** The index of the double array which contains the `y` value.. */
#define SATC_POINT_Y 1
/**
* Get the `x` value from an array of doubles (a point).
*
* @param p the array of doubles (point) to fetch from.
* @return the `x` value as a double.
*/
#define satc_point_get_x(p) p[SATC_POINT_X]
/**
* Set the `x` value in an array of doubles (a point).
*
* This macro is considered a statement.
*
* @param p the array of doubles (point) whose value should change.
* @param v the `x` value to change.
*/
#define satc_point_set_x(p, v) p[SATC_POINT_X] = v;
/**
* Get the `y` value from an array of doubles (a point).
*
* @param p the array of doubles (point) to fetch from.
* @return the `y` value as a double.
*/
#define satc_point_get_y(p) p[SATC_POINT_Y]
/**
* Set the `y` value in an array of doubles (a point).
*
* This macro is considered a statement.
*
* @param p the array of doubles (point) whose value should change.
* @param v the `y` value to change.
*/
#define satc_point_set_y(p, v) p[SATC_POINT_Y] = v;
/**
* Set both the `x` and `y` values in an array of doubles (a point).
*
* This macro is considered a statement.
*
* @param p the array of doubles (point) whose value should change.
* @param x the `x` value to change.
* @param y the `y` value to change.
*/
#define satc_point_set_xy(p, x, y) \
p[SATC_POINT_X] = x; \
p[SATC_POINT_Y] = y;
/**
* Given a point, allocate a separate array of doubles (a point) with
* equivalent values.
*
* This macro is considered a statement.
*
* You are responsible for deallocating the point.
*
* @param p the point to clone.
*/
#define satc_point_clone(p) satc_point_create(satc_point_get_x(p), satc_point_get_y(p))
// --------------------------------------------------------------------
// Forward declarations of certain functions called by other functions.
// --------------------------------------------------------------------
/** Forward declaration of `satc_polygon_set_points`. */
satc_polygon_t *satc_polygon_set_points (satc_polygon_t *polygon, size_t num_points, double **points);
/** Forward declaration of `_satc_polygon_recalc`. */
satc_polygon_t *_satc_polygon_recalc (satc_polygon_t *polygon);
/** Forward declaration of `satc_box_create`. */
satc_box_t *satc_box_create (double *pos, double w, double h);
/** Forward declaration of `satc_box_destroy`. */
void satc_box_destroy (satc_box_t *box);
/** Forward declaration of `satc_box_to_polygon`. */
satc_polygon_t *satc_box_to_polygon (satc_box_t *box);
/** Forward declaration of `satc_test_polygon_polygon`. */
bool satc_test_polygon_polygon (satc_polygon_t *a, satc_polygon_t *b, satc_response_t *response);
// ---------
// Functions
// ---------
/**
* Copy the values of some array of doubles (a point) `q`, onto some point `p`.
*
* `p` will be mutated.
*
* @param p the point to mutate.
* @param q the point whose values to use
* @return the mutated point.
*/
double *satc_point_copy (double *p, double *q) {
satc_point_set_xy(p, satc_point_get_x(q), satc_point_get_y(q));
return p;
}
/**
* Modify the values of some array of doubles (a point) `p` so they match a
* point perpendicular to the original point.
*
* `p` will be mutated.
*
* @param p the point to mutate.
* @return the mutated point.
*/
double *satc_point_perp (double *p) {
double x = satc_point_get_y(p);
double y = -satc_point_get_x(p);
satc_point_set_xy(p, x, y);
return p;
}
/**
* Modify the values of some array of doubles (a point) `p` so they match an
* inverse point to the original point.
*
* `p` will be mutated.
*
* @param p the point to mutate.
* @return the mutated point.
*/
double *satc_point_reverse (double *p) {
satc_point_set_xy(p, -satc_point_get_x(p), -satc_point_get_y(p));
return p;
}
/**
* Find the dot product of two arrays of doubles (points).
*
* @param p the first point.
* @param q the second point.
* @return the dot product, as a double.
*/
#define satc_point_dot(p, q) satc_point_get_x(p) * satc_point_get_x(q) + satc_point_get_y(p) * satc_point_get_y(q)
/**
* Find the length, squared, of an array of doubles (a point).
*
* @param p the point to query.
* @return the length squared, as a double.
*/
#define satc_point_len2(p) satc_point_dot(p, p)
/**
* Find the length, of an array of doubles (a point). This is slightly more
* intensive than finding the length squared.
*
* @param p the point to query.
* @return the length, as a double.
*/
#define satc_point_len(p) sqrt(satc_point_len2(p))
/**
* Find the sum of two arrays of doubles (points), and replace the values of the
* first point to match the sum.
*
* `p` will be mutated.
*
* @param p the first point.
* @param q the second point.
* @return the mutated point.
*/
double *satc_point_add (double *p, double *q) {
satc_point_set_xy(p, satc_point_get_x(p) + satc_point_get_x(q), satc_point_get_y(p) + satc_point_get_y(q));
return p;
}
/**
* Find the difference of two arrays of doubles (points), and replace the
* values of the first point to match the difference.
*
* `p` will be mutated.
*
* @param p the first point.
* @param q the second point.
* @return the mutated point.
*/
double *satc_point_sub (double *p, double *q) {
satc_point_set_xy(p, satc_point_get_x(p) - satc_point_get_x(q), satc_point_get_y(p) - satc_point_get_y(q));
return p;
}
/**
* Scale some array of doubles (a point) by some value, `x`, and some value,
* `y`, and replace the values of the point to match the scaled result.
*
* `p` will be mutated.
*
* @param p the point to mutate.
* @param x the `x` value to scale by.
* @param y the `y` value to scale by.
* @return the mutated point.
*/
double *satc_point_scale_xy (double *p, double x, double y) {
satc_point_set_xy(p, satc_point_get_x(p) * x, satc_point_get_y(p) * y);
return p;
}
/**
* Scale some array of doubles (a point) by some value, `x`, along both axes.
*
* `p` will be mutated.
*
* @param p the point to mutate.
* @param x the `x` value to scale by, horizontally, and vertically.
* @return the mutated point.
*/
double *satc_point_scale_x (double *p, double x) {
return satc_point_scale_xy(p, x, x);
}
/**
* Allocates a new array of doubles (a point). The values will be undefined.
*
* You are responsible for deallocating the point.
*
* @return the newly-created point.
*/
double *satc_point_create (double x, double y) {
double *point = NULL;
point = (double *) malloc(sizeof(double) * 2);
satc_point_set_xy(point, x, y);
return point;
}
/**
* Deallocates an array of doubles (a point).
*
* @param point the point to deallocate.
*/
void satc_point_destroy (double *point) {
free(point);
}
/**
* Rotates a point by some angle.
*
* `p` will be mutated.
*
* @param p the point to rotate.
* @param angle the angle to rotate by.
*/
double *satc_point_rotate (double *p, double angle) {
double x = satc_point_get_x(p);
double y = satc_point_get_y(p);
satc_point_set_xy(p, x * cos(angle) - y * sin(angle), x * sin(angle) - y * cos(angle));
return p;
}
/**
* Normalize some array of doubles (a point) into a unit vector.
*
* `p` will be mutated.
*
* @param p the point to mutate.
* @return the normalized point.
*/
double *satc_point_normalize (double *p) {
double d = satc_point_len(p);
if (d > 0) satc_point_set_xy(p, satc_point_get_x(p) / d, satc_point_get_y(p) / d);
return p;
}
/**
* Project some array of doubles (a point) `q` onto `p`.
*
* `p` will be mutated.
*
* @param p the point to mutate.
* @param q the point used to project.
* @return the projected point point.
*/
double *satc_point_project (double *p, double *q) {
double amt = satc_point_dot(p, q) / satc_point_len2(q);
satc_point_set_xy(p, amt * satc_point_get_x(p), amt * satc_point_get_y(p));
return p;
}
/**
* Project some array of doubles (a point) `q` onto `p`, but the result is not
* divided by the length of `q` squared.
*
* `p` will be mutated.
*
* @param p the point to mutate.
* @param q the point used to project.
* @return the projected point point.
*/
double *satc_point_project_n (double *p, double *q) {
double amt = satc_point_dot(p, q);
satc_point_set_xy(p, amt * satc_point_get_x(p), amt * satc_point_get_y(p));
return p;
}
/**
* Reflect some array of doubles (a point) along some axis.
*
* `p` will be mutated.
*
* @param p the point to mutate.
* @param axis the angle to reflect on.
* @return the normalized point.
*/
double *satc_point_reflect (double *p, double *axis) {
double x = satc_point_get_x(p);
double y = satc_point_get_y(p);
satc_point_project(p, axis);
satc_point_scale_x(p, 2.0);
satc_point_set_xy(p, satc_point_get_x(p) - x, satc_point_get_y(p) - y);
return p;
}
/**
* Reflect some array of doubles (a point) along some axis. When projecting, the
* value is not divided by the length of `p` squared.
*
* `p` will be mutated.
*
* @param p the point to mutate.
* @param axis the angle to reflect on.
* @return the normalized point.
*/
double *satc_point_reflect_n (double *p, double *axis) {
double x = satc_point_get_x(p);
double y = satc_point_get_y(p);
satc_point_project_n(p, axis);
satc_point_scale_x(p, 2.0);
satc_point_set_xy(p, satc_point_get_x(p) - x, satc_point_get_y(p) - y);
return p;
}
/**
* Create a struct representing a circle, with a given position and radius.
*
* You are responsible for deallocating the circle.
*
* @param pos the position of the circle.
* @param r the radius of the circle.
* @return a circle struct.
*/
satc_circle_t *satc_circle_create (double *pos, double r) {
satc_circle_t *circle = NULL;
circle = (satc_circle_t *) malloc(sizeof(satc_circle_t));
circle->type = satc_type_circle;
circle->pos = satc_point_clone(pos);
circle->r = r;
return circle;
}
/**
* Deallocates a struct representing a circle.
*
* @param circle the circle to deallocate.
*/
void satc_circle_destroy (satc_circle_t *circle) {
satc_point_destroy(circle->pos);
circle->type = satc_type_none;
circle->pos = NULL;
circle->r = -1.0;
free(circle);
}
/**
* Returns a struct representing a rectangular polygon which is equivalent to
* the bounding box of a circle.
*
* You are responsible for deallocating the polygon.
*
* @param circle the circle whose bounding box is of interest.
* @return a polygon struct.
*/
satc_polygon_t *satc_circle_get_aabb (satc_circle_t *circle) {
double r = circle->r;
satc_point_alloca(corner);
satc_point_copy(corner, circle->pos);
satc_point_set_xy(corner, satc_point_get_x(corner) - r, satc_point_get_y(corner) - r);
satc_box_t *box = satc_box_create(corner, r * 2.0, r * 2.0);
satc_polygon_t *polygon = satc_box_to_polygon(box);
satc_box_destroy(box);
return polygon;
}
/**
* Create a struct representing a polygon, with a given position and points.
*
* The points passed in are copied, and so you must handle the deallocation of
* the passed-in points.
*
* You are responsible for deallocating the polygon.
*
* @param pos the position of the polygon.
* @param num_points the number of points provided.
* @param points an array of arrays of doubles (array of points).
* @return a polygon struct.
*/
satc_polygon_t *satc_polygon_create (double *pos, size_t num_points, double **points) {
satc_polygon_t *polygon = NULL;
polygon = (satc_polygon_t *) malloc(sizeof(satc_polygon_t));
polygon->type = satc_type_polygon;
polygon->pos = satc_point_clone(pos);
polygon->angle = 0.0;
polygon->offset = satc_point_create(0.0, 0.0);
polygon->num_points = 0;
polygon->points = NULL;
polygon->num_calc_points = 0;
polygon->calc_points = NULL;
polygon->num_edges = 0;
polygon->edges = NULL;
polygon->num_normals = 0;
polygon->normals = NULL;
satc_polygon_set_points(polygon, num_points, points);
return polygon;
}
/**
* Deallocates a struct representing a polygon.
*
* @param polygon the polygon to deallocate.
*/
void satc_polygon_destroy (satc_polygon_t *polygon) {
satc_point_destroy(polygon->pos);
satc_point_destroy(polygon->offset);
size_t i = 0;
for (; i < polygon->num_points; i++) {
satc_point_destroy(polygon->points[i]);
}
free(polygon->points);
polygon->type = satc_type_none;
polygon->pos = NULL;
polygon->num_points = -1;
polygon->points = NULL;
polygon->angle = 0.0;
polygon->offset = NULL;
free(polygon);
}
/**
* Replace a polygon struct's list of points.
*
* The old points will be deallocated. The new points will be deallocated when
* the polygon is deallocated.
*
* The calculated values of the polygon will be recalculated.
*
* The points passed in are copied, and so you must handle the deallocation of
* the passed-in points.
*
* For internal use.
*
* @param polygon the polygon whose points to replace.
* @param num_points the number of new points.
* @param points an array of arrays of doubles (array of points).
*/
void _satc_polygon_copy_points (satc_polygon_t *polygon, size_t num_points, double **points) {
if (polygon->points != NULL) {
size_t i = 0;
for (; i < polygon->num_points; i++) satc_point_destroy(polygon->points[i]);
free(polygon->points);
polygon->num_points = 0;
polygon->points = NULL;
}
double **new_points = NULL;
new_points = (double **) malloc(sizeof(double *) * num_points);
size_t i = 0;
for (; i < num_points; i++) new_points[i] = satc_point_clone(points[i]);
polygon->num_points = num_points;
polygon->points = new_points;
_satc_polygon_recalc(polygon);
}
/**
* Clear out the calculated points of a polygon and replace them with some
* number of preallocated empty points.
*
* The old calculated points will be deallocated. The new points will be
* deallocated when the polygon is deallocated.
*
* For internal use.
*
* @param polygon the polygon whose calculated points should be replaced.
* @param num_calc_points the number of pristine points to add.
*/
void _satc_polygon_reset_calc_points (satc_polygon_t *polygon, size_t num_calc_points) {
if (polygon->calc_points != NULL) {
size_t i = 0;
for (; i < polygon->num_calc_points; i++) satc_point_destroy(polygon->calc_points[i]);
free(polygon->calc_points);
polygon->num_calc_points = 0;
polygon->calc_points = NULL;
}
double **new_calc_points = NULL;
new_calc_points = (double **) malloc(sizeof(double *) * num_calc_points);
size_t i = 0;
for (; i < num_calc_points; i++) {
new_calc_points[i] = satc_point_create(0.0, 0.0);
}
polygon->num_calc_points = num_calc_points;
polygon->calc_points = new_calc_points;
}
/**
* Replace the calculated edges of a polygon with some number of pristine
* points representing edges.
*
* The old calculated edges will be deallocated. The new edges will be
* deallocated when the polygon is deallocated.
*
* For internal use.
*
* @param polygon the polygon whose edges should be replaced.
* @param num_edges the number of pristine points to add.
*/
void _satc_polygon_reset_edges (satc_polygon_t *polygon, size_t num_edges) {
if (polygon->edges != NULL) {
size_t i = 0;
for (; i < polygon->num_edges; i++) satc_point_destroy(polygon->edges[i]);
free(polygon->edges);
polygon->num_edges = 0;
polygon->edges = NULL;
}
double **new_edges = NULL;
new_edges = (double **) malloc(sizeof(double *) * num_edges);
size_t i = 0;
for (; i < num_edges; i++) {
new_edges[i] = satc_point_create(0.0, 0.0);
}
polygon->num_edges = num_edges;
polygon->edges = new_edges;
}
/**
* Replace the calculated normals of a polygon.
*
* The old calculated normals will be deallocated. The new calculated normals
* will be deallocated when the polygon is deallocated.
*
* For internal use.
*
* @param polygon the polygon whose calculated normals should be replaced.
* @param num_normals the number of pristine normals to add.
*/
void _satc_polygon_reset_normals (satc_polygon_t *polygon, size_t num_normals) {
if (polygon->normals != NULL) {
size_t i = 0;
for (; i < polygon->num_normals; i++) satc_point_destroy(polygon->normals[i]);
free(polygon->normals);
polygon->num_normals = 0;
polygon->normals = NULL;
}
double **new_normals = NULL;
new_normals = (double **) malloc(sizeof(double *) * num_normals);
size_t i = 0;
for (; i < num_normals; i++) {
new_normals[i] = satc_point_create(0.0, 0.0);
}
polygon->num_normals = num_normals;
polygon->normals = new_normals;
}
/**
* Given a polygon and number of points, it will clear out all of the
* calculated values, replace the points already on the polygon, and
* recalculate all the calculated values.
*
* All deallocation and allocation is handled for you.
*
* The points passed in are copied, and so you must handle the deallocation of
* the passed-in points.
*
* @param polygon the polygon whose points should be replaced.
* @param num_points the number of new points.
* @param points an array of arrays of doubles (array of points).
* @return the polygon passed in.
*/
satc_polygon_t *satc_polygon_set_points (satc_polygon_t *polygon, size_t num_points, double **points) {
bool length_changed = num_points != polygon->num_points;
if (length_changed) {
_satc_polygon_reset_calc_points(polygon, num_points);
_satc_polygon_reset_edges(polygon, num_points);
_satc_polygon_reset_normals(polygon, num_points);
}
_satc_polygon_copy_points(polygon, num_points, points);
return polygon;
}
/**
* Set the angle of rotation of the polygon. All calculated values will be
* recalculated.
*
* @param polygon the polygon to rotate.
* @param angle the angle to rotate by.
* @return the passed-in polygon.
*/
satc_polygon_t *satc_polygon_set_angle (satc_polygon_t *polygon, double angle) {
polygon->angle = angle;
_satc_polygon_recalc(polygon);
return polygon;
}
/**
* Set the offset of the polygon. All calculated values will be recalculated.
*
* @param polygon the polygon to offset.
* @param offset the array of doubles (a point) to offset by.
* @return the passed-in polygon.
*/
satc_polygon_t *satc_polygon_set_offset (satc_polygon_t *polygon, double *offset) {
satc_point_copy(polygon->offset, offset);
_satc_polygon_recalc(polygon);
return polygon;
}
/**
* In lieu of setting the angle of the polygon, you can actually rotate all of
* the points. All calculated values will be recalculated.
*
* @param polygon the polygon to offset.
* @param angle the array of doubles (a point) to offset by.
* @return the passed-in polygon.
*/
satc_polygon_t *satc_polygon_rotate (satc_polygon_t *polygon, double angle) {
size_t i = 0;
for (; i < polygon->num_points; i++) satc_point_rotate(polygon->points[i], angle);
_satc_polygon_recalc(polygon);
return polygon;
}
/**
* In lieu of setting the offset of the polygon, you can actually translate all
* of the points by some `x` and `y`. All calculated values will be
* recalculated.
*
* @param polygon the polygon to offset.
* @param x the horizontal amount to translate by.
* @param y the vertical amount to translate by.
* @return the passed-in polygon.
*/
satc_polygon_t *satc_polygon_translate (satc_polygon_t *polygon, double x, double y) {
size_t i = 0;
for (; i < polygon->num_points; i++) satc_point_set_xy(polygon->points[i], x, y);
_satc_polygon_recalc(polygon);
return polygon;
}
/**
* Recalculates all the calculated values for a struct representing a polygon
* shape.
*
* For internal use.
*
* @param polygon a polygon whose values should be recalculated.
* @return the passed-in polygon.
*/
satc_polygon_t *_satc_polygon_recalc (satc_polygon_t *polygon) {
double **calc_points = polygon->calc_points;
double **edges = polygon->edges;
double **normals = polygon->normals;
double **points = polygon->points;
double *offset = polygon->offset;
double angle = polygon->angle;
size_t num_points = polygon->num_points;
size_t i = 0;
for (; i < num_points; i++) {
double *calc_point = satc_point_copy(calc_points[i], points[i]);
satc_point_add(calc_point, offset);
if (angle != 0.0) satc_point_rotate(calc_point, angle);
}
i = 0;
for (; i < num_points; i++) {
double *p1 = calc_points[i];
double *p2 = (i < num_points - 1) ? calc_points[i + 1] : calc_points[0];
double *edge = edges[i];
satc_point_copy(edge, p2);
satc_point_sub(edge, p1);
double *normal = normals[i];
satc_point_copy(normal, edge);
satc_point_perp(normal);
satc_point_normalize(normal);
}
return polygon;
}
/**
* Returns a polygon struct representing the bounding box of a polygon.
*
* You are responsible for deallocating the returned polygon.
*
* @param polygon the polygon whose bounding box should be calculated.
* @return a polygon representing the bounding box.
*/
satc_polygon_t *satc_polygon_get_aabb (satc_polygon_t *polygon) {
double x_min = satc_point_get_x(polygon->points[0]);
double y_min = satc_point_get_y(polygon->points[0]);
double x_max = x_min;
double y_max = x_max;
size_t i = 1;
for (; i < polygon->num_points; i++) {
double x = satc_point_get_x(polygon->points[i]);
double y = satc_point_get_y(polygon->points[i]);
if (x < x_min) x_min = x;
if (x > x_max) x_max = x;
if (y < y_min) y_min = y;
if (y > y_max) y_max = y;
}
satc_point_alloca(pos);
satc_point_set_xy(pos, satc_point_get_x(pos) + x_min, satc_point_get_y(pos) + y_min);
satc_box_t *box = satc_box_create(pos, x_max - x_min, y_max - y_min);
satc_polygon_t *new_polygon = satc_box_to_polygon(box);
satc_box_destroy(box);
return new_polygon;
}
/**
* Get the "centroid" (the estimated center) of a polygon as an array of
* doubles (a point).
*
* You are responsible for deallocating the point.
*
* @param polygon the polygon whose centroid should be calculated.
* @return an array of doubles (a point) representing the centroid.
*/
double *satc_polygon_get_centroid (satc_polygon_t *polygon) {
double **points = polygon->calc_points;
size_t len = polygon->num_calc_points;
double cx = 0.0;
double cy = 0.0;
double ar = 0.0;
size_t i = 0;
for (; i < len; i++) {
double *p1 = points[i];
double *p2 = (i == len - 1) ? points[0] : points[i + 1];
double a = satc_point_get_x(p1) * satc_point_get_y(p2) - satc_point_get_x(p2) * satc_point_get_y(p1);
cx += (satc_point_get_x(p1) + satc_point_get_x(p2)) * a;
cy += (satc_point_get_y(p1) + satc_point_get_y(p2)) * a;
ar += a;
}
ar = ar * 3.0;
cx = cx / ar;
cy = cy / ar;
return satc_point_create(cx, cy);
}
/**
* Creates a struct representing a box shape.
*
* @param pos the position of the box, from the top left.
* @param w the width of the box.
* @param h the height of the box.
* @return a box struct.
*/
satc_box_t *satc_box_create (double *pos, double w, double h) {