Changeset 671
- Timestamp:
- Dec 5, 2004, 8:21:43 PM (20 years ago)
- Location:
- inundation/ga/storm_surge/pyvolution
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
inundation/ga/storm_surge/pyvolution/test_util.py
r664 r671 391 391 392 392 #Endpoints first 393 assert point_on_line( (0, 0), [(0,0), (1,0)])394 assert point_on_line( (1, 0), [(0,0), (1,0)])393 assert point_on_line( 0, 0, 0,0, 1,0 ) 394 assert point_on_line( 1, 0, 0,0, 1,0 ) 395 395 396 396 #Then points on line 397 assert point_on_line( (0.5, 0), [(0,0), (1,0)])398 assert point_on_line( (0, 0.5), [(0,1), (0,0)])399 assert point_on_line( (1, 0.5), [(1,1), (1,0)])400 assert point_on_line( (0.5, 0.5), [(0,0), (1,1)])397 assert point_on_line( 0.5, 0, 0,0, 1,0 ) 398 assert point_on_line( 0, 0.5, 0,1, 0,0 ) 399 assert point_on_line( 1, 0.5, 1,1, 1,0 ) 400 assert point_on_line( 0.5, 0.5, 0,0, 1,1 ) 401 401 402 402 #Then points not on line 403 assert not point_on_line( (0.5, 0), [(0,1), (1,1)])404 assert not point_on_line( (0, 0.5), [(0,0), (1,1)])403 assert not point_on_line( 0.5, 0, 0,1, 1,1 ) 404 assert not point_on_line( 0, 0.5, 0,0, 1,1 ) 405 405 406 406 #From real example that failed 407 assert not point_on_line( (40,50), [(40,20), (40,40)])407 assert not point_on_line( 40,50, 40,20, 40,40 ) 408 408 409 409 410 410 #From real example that failed 411 assert not point_on_line( (40,19), [(40,20), (40,40)])411 assert not point_on_line( 40,19, 40,20, 40,40 ) 412 412 413 413 -
inundation/ga/storm_surge/pyvolution/util.py
r664 r671 48 48 49 49 50 def point_on_line(point, line): 50 #def point_on_line(point, line): 51 def point_on_line(x, y, x0, y0, x1, y1): 51 52 """Determine whether a point is on a line segment 52 53 53 Input: 54 point : (x, y)55 line : [ (x0, y0), (x1, y1) ]54 Input: x, y, x0, x0, x1, y1: where 55 point is given by x, y 56 line is given by (x0, y0) and (x1, y1) 56 57 57 58 """ … … 61 62 from Numeric import array, dot, allclose 62 63 from math import sqrt 63 64 x = point[0]65 y = point[1]66 67 x0 = line[0][0]68 x1 = line[1][0]69 y0 = line[0][1]70 y1 = line[1][1]71 64 72 65 a = array([x - x0, y - y0]) … … 173 166 174 167 #Check for case where point is contained in line segment 175 if point_on_line( (x,y), [ [px[i], py[i]], [px[j], py[j]] ]): 168 ##if point_on_line( (x,y), [ [px[i], py[i]], [px[j], py[j]] ]): 169 if point_on_line(x, y, px[i], py[i], px[j], py[j]): 176 170 if closed: 177 171 inside = True … … 515 509 import compile 516 510 if compile.can_use_C_extension('util_ext.c'): 517 from util_ext import gradient 511 from util_ext import gradient, point_on_line 518 512 else: 519 513 gradient = gradient_python -
inundation/ga/storm_surge/pyvolution/util_ext.c
r258 r671 18 18 19 19 20 // Gateway to Python 20 21 int _point_on_line(double x, double y, 22 double x0, double y0, 23 double x1, double y1) { 24 /*Determine whether a point is on a line segment 25 26 Input: x, y, x0, x0, x1, y1: where 27 point is given by x, y 28 line is given by (x0, y0) and (x1, y1) 29 30 */ 31 32 double a0, a1, a_normal0, a_normal1, b0, b1, len_a, len_b; 33 34 a0 = x - x0; 35 a1 = y - y0; 36 37 a_normal0 = a1; 38 a_normal1 = -a0; 39 40 b0 = x1 - x0; 41 b1 = y1 - y0; 42 43 if ( a_normal0*b0 + a_normal1*b1 == 0 ) { 44 //Point is somewhere on the infinite extension of the line 45 46 len_a = sqrt(a0*a0 + a1*a1); 47 len_b = sqrt(b0*b0 + b1*b1); 48 49 if (a0*b0 + a1*b1 >= 0 && len_a <= len_b) { 50 return 1; 51 } else { 52 return 0; 53 } 54 } else { 55 return 0; 56 } 57 } 58 59 60 61 // Gateways to Python 62 PyObject *point_on_line(PyObject *self, PyObject *args) { 63 // 64 // point_on_line(x, y, x0, y0, x1, y1) 65 // 66 67 double x, y, x0, y0, x1, y1; 68 int res; 69 PyObject *result; 70 71 // Convert Python arguments to C 72 if (!PyArg_ParseTuple(args, "dddddd", &x, &y, &x0, &y0, &x1, &y1)) 73 return NULL; 74 75 76 // Call underlying routine 77 res = _point_on_line(x, y, x0, y0, x1, y1); 78 79 // Return values a and b 80 result = Py_BuildValue("i", res); 81 return result; 82 } 83 84 21 85 PyObject *gradient(PyObject *self, PyObject *args) { 22 86 // … … 52 116 //{"rotate", (PyCFunction)rotate, METH_VARARGS | METH_KEYWORDS, "Print out"}, 53 117 {"gradient", gradient, METH_VARARGS, "Print out"}, 118 {"point_on_line", point_on_line, METH_VARARGS, "Print out"}, 54 119 {NULL, NULL, 0, NULL} /* sentinel */ 55 120 };
Note: See TracChangeset
for help on using the changeset viewer.