Ignore:
Timestamp:
Dec 5, 2004, 8:21:43 PM (20 years ago)
Author:
ole
Message:

Implemented point_on_line in C.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • inundation/ga/storm_surge/pyvolution/util_ext.c

    r258 r671  
    1818
    1919
    20 // Gateway to Python
     20
     21int _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
     62PyObject *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
    2185PyObject *gradient(PyObject *self, PyObject *args) {
    2286  //
     
    52116  //{"rotate", (PyCFunction)rotate, METH_VARARGS | METH_KEYWORDS, "Print out"}, 
    53117  {"gradient", gradient, METH_VARARGS, "Print out"},   
     118  {"point_on_line", point_on_line, METH_VARARGS, "Print out"},     
    54119  {NULL, NULL, 0, NULL}   /* sentinel */
    55120};
Note: See TracChangeset for help on using the changeset viewer.