TheBoussinesqModel  3.2.1
 All Data Structures Files Functions Variables Typedefs Macros Pages
geometry_utilities.c
Go to the documentation of this file.
1 
24 #include "turtle.h"
25 #include "geometry.h"
26 
27 int signum (long a){
34 if (a==0){
35  return 0;
36 }else if(a>0) {
37  return 1;
38 } else if(a<0){
39  return -1;
40 }
41 return 1;
42 }
43 
44 int signum_double (double a){
51 if (a==0.0){
52  return 0;
53 }else if(a>0) {
54  return 1;
55 } else if(a<0){
56  return -1;
57 }
58 return 1;
59 }
60 
69 //int l;
70 double det,det1,det2;
71 double lambda1,lambda2;
72 
74 det=-(L1->end->x-L1->begin->x)*(L2->end->y-L2->begin->y)+(L2->end->x-L2->begin->x)*(L1->end->y-L1->begin->y);
75 det1=-(L2->begin->x-L1->begin->x)*(L2->end->y-L2->begin->y)+(L2->end->x-L2->begin->x)*(L2->begin->y-L1->begin->y);
76 det2=(L1->end->x-L1->begin->x)*(L2->begin->y-L1->begin->y)-(L1->end->y-L1->begin->y)*(L2->begin->x-L1->begin->x);
77 
78 if (det==0.0) {
79  if ((det1==0) || (det2==0)) {
80  return 2;
81  }else {
82  return 0;
83  }
84 } else {
85  lambda1=det1/det;
86  lambda2=det2/det;
87  if ((lambda1>0.0) && (lambda1<1.0) && (lambda2>0.0) && (lambda2<1.0)) {
88  return 1;
89  } else{
90  return 0;
91  }
92 
93 }
94 return 0;
95 
96 }
97 
105 long j,cnt;
106 cnt=0;
107 for (j=lvector->nl;j<=lvector->nh;j++){
108  if (lvector->element[j]==value) cnt++;
109 }
110 return cnt;
111 }
112 
113 int check_vertex_intersection_2_lines(long b1,long e1,long b2,long e2){
127  if (signum(b1-e2)*signum(b1-b2)*signum(e1-e2)*signum(e1-b2)==0) {
128  if ((signum(b1-e2)*signum(b1-e2)+signum(e1-b2)*signum(e1-b2))*(signum(b1-b2)*signum(b1-b2)+signum(e1-e2)*signum(e1-e2))==0) {
129  return 2;
130  }else {
131  return 1;
132  }
133 
134  }
136  return 0;
137 }
138 
139 int check_3_numbers(long a, long b, long c) {
148 //if (((a-b)*(a-b)+(a-c)*(a-c))==0) return 1;
149  if ((a==b) && (a=c)) return TRUE;
150  return FALSE;
151 }
152 
153 
154 double area2d(POINT *P1,POINT *P2, POINT *P3) {
162  double area;
163 
164 
165  area=(P1->x*(P3->y-P2->y)+P2->x*(P1->y-P3->y)+P3->x*(P2->y-P1->y))/2.0;
166 // printf("x= %lf %lf %lf \n",P1->x,P2->x,P3->x);
167 // printf("y= %lf %lf %lf \n",P1->y,P2->y,P3->y);
168 // printf ("area= %lf",area);
169 
170  if (area<0.0) area=-area;
171 
172  return area;
173 
174 }
175 
176 
177 double distance2D(POINT *P1,POINT *P2){
183  return pow(pow(P1->x-P2->x,2.0)+pow(P1->y-P2->y,2.0),0.5);
184 }
185 
186 long shared_edges(POLYGON *PO1, POLYGON *PO2, long no_intersection, long *l_po1, long *l_po2, double *dist_centroids){
204 long l,k,l_shared;
205 double distance;
206 int s;
207 
208 
209 l_shared=no_intersection;
210 s=0;
211 
212 for(l=PO1->edge_indices->nl;l<=PO1->edge_indices->nh;l++){
213  for(k=PO2->edge_indices->nl;k<=PO2->edge_indices->nh;k++){
214  if (PO1->edge_indices->element[l]==PO2->edge_indices->element[k]) {
215  if (s==0){
216  l_shared=PO1->edge_indices->element[l];
217  *l_po1=l;
218  *l_po2=k;
219  s=1;
220  }else {
221  printf ("Warning:: polygons %ld and %ld share more than one edge (%ld,%ld)!! \n",PO1->index,PO2->index,PO1->edge_indices->element[l],l_shared);
222  l_shared=no_intersection;
223  }
224 
225  }
226  }
227 }
228 
229 distance=distance2D(PO1->centroid,PO2->centroid);
230 *dist_centroids=distance;
231 //printf("... PO1=%ld PO2=%ld dist=%lf",PO1->index,PO2->index,distance);
232 //stop_execution();
233 return l_shared;
234 
235 }
236 
237