1 | ///////////////////////////////////////////////////////////////////////////// |
---|
2 | // Name: crvgeomtry.h |
---|
3 | // Purpose: |
---|
4 | // Author: Cesar Mauri Loba (cesar at crea-si dot com) |
---|
5 | // Modified by: |
---|
6 | // Created: 22/02/2008 |
---|
7 | // Copyright: (C) 2008 Cesar Mauri Loba - CREA Software Systems |
---|
8 | // |
---|
9 | // This program is free software: you can redistribute it and/or modify |
---|
10 | // it under the terms of the GNU General Public License as published by |
---|
11 | // the Free Software Foundation, either version 3 of the License, or |
---|
12 | // (at your option) any later version. |
---|
13 | // |
---|
14 | // This program is distributed in the hope that it will be useful, |
---|
15 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | // GNU General Public License for more details. |
---|
18 | // |
---|
19 | // You should have received a copy of the GNU General Public License |
---|
20 | // along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
21 | ///////////////////////////////////////////////////////////////////////////// |
---|
22 | |
---|
23 | #ifndef CRVGEOMTRY_H |
---|
24 | #define CRVGEOMTRY_H |
---|
25 | |
---|
26 | typedef double Real; |
---|
27 | |
---|
28 | #define INF_M 999999999 /* valor pendent 'infinita' */ |
---|
29 | #define PI 3.1415926535897932384626433832795 |
---|
30 | #ifndef M_PI |
---|
31 | #define M_PI PI |
---|
32 | #endif |
---|
33 | |
---|
34 | /* TIPUS */ |
---|
35 | |
---|
36 | typedef struct tRPoint { |
---|
37 | Real x, y; |
---|
38 | } tRPoint; |
---|
39 | |
---|
40 | typedef struct t3DRPoint { |
---|
41 | Real x, y, z; |
---|
42 | } t3DRPoint; |
---|
43 | |
---|
44 | /* equació recta en forma y = mx + n */ |
---|
45 | typedef struct tRect{ |
---|
46 | Real m, n; |
---|
47 | } tRect; |
---|
48 | |
---|
49 | |
---|
50 | /* FUNCIONS */ |
---|
51 | |
---|
52 | /* copia d'un punt */ |
---|
53 | static inline |
---|
54 | void cp_point (tRPoint *dst, tRPoint *src) |
---|
55 | { |
---|
56 | dst->x= src->x; |
---|
57 | dst->y= src->y; |
---|
58 | } |
---|
59 | |
---|
60 | /* distància entre 2 punts */ |
---|
61 | Real points_distance (tRPoint *p1, tRPoint *p2); |
---|
62 | |
---|
63 | /* distància entre 2 punts 3D */ |
---|
64 | Real points3d_distance (t3DRPoint *p1, t3DRPoint * p2); |
---|
65 | |
---|
66 | /* equació de la recta que passa per dos punts */ |
---|
67 | void points2rect (tRPoint *p1, tRPoint *p2, tRect *r); |
---|
68 | |
---|
69 | /* distància d'un punt a una recta */ |
---|
70 | Real point_rect_distance (tRect *r, tRPoint *p); |
---|
71 | |
---|
72 | /* calcula l'angle del vector que va de p1 a p2 */ |
---|
73 | Real segment_angle (tRPoint *p1, tRPoint *p2); |
---|
74 | |
---|
75 | /* donada una recta diu si un punt es troba sobre la recta (0) |
---|
76 | o a la banda positiva (1) o negativa de la recta (-1) */ |
---|
77 | int point_rect_where (tRect *r, tRPoint *p, Real error); |
---|
78 | |
---|
79 | /* donat un segment retorna si un determinat punt es troba a la dreta (1), |
---|
80 | sobre el segment (0) o a l'esquerra (-1). La posició es mira respecte |
---|
81 | la direcció del vector entre p1 i p2 */ |
---|
82 | int point_segment_where (tRPoint *p1, tRPoint *p2, tRPoint *p, Real error); |
---|
83 | |
---|
84 | /* calcula la recta perpendicular a una donada que passa per un punt */ |
---|
85 | void perpendicular_rect (tRect *r, tRPoint *p, tRect *perp); |
---|
86 | |
---|
87 | /* troba el punt mig d'un segment */ |
---|
88 | void segment_mean_point (tRPoint *p1, tRPoint *p2, tRPoint *mp); |
---|
89 | |
---|
90 | /* troba el punt de tall entre dues rectes. retorna 0 si no hi ha |
---|
91 | punt de tall, 1 altrament */ |
---|
92 | int rects_cutting_point (tRect *r1, tRect *r2, tRPoint *p); |
---|
93 | |
---|
94 | /* calcula el centre d'una circumferència a partir de 3 punts del perímetre. |
---|
95 | retorna 0 si no es possible determinar el center, 1 si tot correcte */ |
---|
96 | int circle_center (tRPoint *p1, tRPoint *p2, tRPoint *p3, tRPoint *center); |
---|
97 | |
---|
98 | /* donat un punt el rota l'angle indicat respecte del centre */ |
---|
99 | void point_rotate (tRPoint *p, tRPoint *center, Real ang); |
---|
100 | |
---|
101 | /* escalat d'un segment. es modifica el punt *target per tal que |
---|
102 | el segment que va de *p a *target tingui la nova distància indicada */ |
---|
103 | void scale_segment (tRPoint *target, tRPoint *p, Real real_dist); |
---|
104 | |
---|
105 | /* reescala un segment modificant la posició dels 2 extrems per a |
---|
106 | que tingui la mida desitjada */ |
---|
107 | void rescale_segment (tRPoint *p1, tRPoint *p2, Real real_dist); |
---|
108 | |
---|
109 | /* donada una llista de punts que formen un polígon tancat |
---|
110 | en calcula la seva caixa englobant */ |
---|
111 | void calculate_englobing_box (tRPoint *lvertexs, int nvertexs, |
---|
112 | tRPoint *min_box, tRPoint *max_box); |
---|
113 | |
---|
114 | /* comprova si dos caixes englobants intersecten */ |
---|
115 | static inline |
---|
116 | int englobing_box_instersection (tRPoint *min1, tRPoint *max1, |
---|
117 | tRPoint *min2, tRPoint *max2) |
---|
118 | { |
---|
119 | return (!(min1->x> max2->x || min1->y> max2->y || |
---|
120 | min2->x> max1->x || min2->y> max2->y ) |
---|
121 | ); |
---|
122 | } |
---|
123 | |
---|
124 | /* diu si dos segments es tallen en algun punt */ |
---|
125 | int segments_cutting (tRPoint *s1p1, tRPoint *s1p2, tRPoint *s2p1, tRPoint *s2p2); |
---|
126 | |
---|
127 | /* comprova si dos polígons tancats tenen alguna àrea solapada. |
---|
128 | NOTA: no es comprova si un polígon és interior a un altre */ |
---|
129 | int polys_intersect (tRPoint *v1, int lv1, tRPoint *v2, int lv2); |
---|
130 | |
---|
131 | #endif |
---|