Changeset 8017 for trunk/anuga_core/source/anuga/utilities
- Timestamp:
- Sep 16, 2010, 10:38:49 AM (14 years ago)
- Location:
- trunk/anuga_core/source/anuga/utilities
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/anuga_core/source/anuga/utilities/polygon_ext.c
r7276 r8017 20 20 #include "util_ext.h" 21 21 22 #define YES 1 23 #define NO 0 24 22 25 23 26 double dist(double x, … … 89 92 90 93 94 // public domain function by Darel Rex Finley, 2006 95 // http://www.alienryderflex.com/intersect/ 96 // 97 // Determines the intersection point of the line segment defined by points A and B 98 // with the line segment defined by points C and D. 99 // 100 // Returns YES if the intersection point was found, and stores that point in X,Y. 101 // Returns NO if there is no determinable intersection point, in which case X,Y will 102 // be unmodified. 103 104 int __lineSegmentIntersection( 105 double Ax, double Ay, 106 double Bx, double By, 107 double Cx, double Cy, 108 double Dx, double Dy, 109 double *X, double *Y) { 110 111 double distAB, theCos, theSin, newX, ABpos; 112 113 // Fail if either line segment is zero-length. 114 if ( (Ax == Bx && Ay == By) || (Cx == Dx && Cy == Dy) ) return NO ; 115 116 // Fail if the segments share an end-point. 117 if ( (Ax == Cx && Ay == Cy) || (Bx == Cx && By == Cy) 118 || (Ax == Dx && Ay == Dy) || (Bx == Dx && By == Dy) ) { 119 return NO; 120 } 121 122 // (1) Translate the system so that point A is on the origin. 123 Bx -= Ax; 124 By -= Ay; 125 Cx -= Ax; 126 Cy -= Ay; 127 Dx -= Ax; 128 Dy -= Ay; 129 130 // Discover the length of segment A-B. 131 distAB = sqrt(Bx * Bx + By * By); 132 133 // (2) Rotate the system so that point B is on the positive X axis. 134 theCos = Bx / distAB; 135 theSin = By / distAB; 136 newX = Cx * theCos + Cy*theSin; 137 Cy = Cy * theCos - Cx*theSin; 138 Cx = newX; 139 newX = Dx * theCos + Dy*theSin; 140 Dy = Dy * theCos - Dx*theSin; 141 Dx = newX; 142 143 // Fail if segment C-D doesn't cross line A-B. 144 if ( (Cy < 0. && Dy < 0.) || (Cy >= 0. && Dy >= 0.) ) return NO; 145 146 // (3) Discover the position of the intersection point along line A-B. 147 ABpos = Dx + (Cx - Dx) * Dy / (Dy - Cy); 148 149 // Fail if segment C-D crosses line A-B outside of segment A-B. 150 if (ABpos < 0. || ABpos > distAB) return NO; 151 152 // (4) Apply the discovered position to line A-B in the original coordinate system. 153 *X = Ax + ABpos*theCos; 154 *Y = Ay + ABpos*theSin; 155 156 // Success. 157 return YES; 158 } 91 159 92 160 /* -
trunk/anuga_core/source/anuga/utilities/util_ext.h
r7886 r8017 14 14 #include "math.h" 15 15 16 #include <stdio.h> 17 #define STRINGIFY(x) #x 18 #define TOSTRING(x) STRINGIFY(x) 19 #define AT __FILE__ ":" TOSTRING(__LINE__) 20 #define P_ERROR_BUFFER_SIZE 65 21 void report_python_error(const char *location, const char *msg) 22 { 23 24 char buf[P_ERROR_BUFFER_SIZE]; 25 int n; 26 n = snprintf(buf, P_ERROR_BUFFER_SIZE, "Error at %s: %s\n", location, msg); 27 28 PyErr_SetString(PyExc_RuntimeError, buf); 29 } 30 31 16 32 17 33 double max(double x, double y) {
Note: See TracChangeset
for help on using the changeset viewer.