GeoscienceAustralia/anuga_core

improvement in quad_tree.c in subroutine __point_on_line(...)

chriswebers opened this issue · 1 comments

Here is an improvement to the subroutine __point_on_line(...) in the file quad_tree.c:

Instead of comparing the length of a and b in the variables len_a/len_b, it suffices to compare their squared length. That saves two sqrt operations and allows to reuse the squared length of b which was already calculated in denominator. Furthermore, the calculation of the square length of 'a' can be delayed until it is proven that the point x,y is inside the line segment from one end point.

   if (is_parallel) {
        // Point is somewhere on the infinite extension of the line
        // subject to specified absolute tolerance

//        len_a = dist(a0, a1); //sqrt(a0*a0 + a1*a1);
//        len_b = dist(b0, b1); //sqrt(b0*b0 + b1*b1);

//        if (a0*b0 + a1*b1 >= 0 && len_a <= len_b) {
          if (a0*b0 + a1*b1 >= 0) {  // inside line segment from one end point
            double len_a2 = a0*a0 + a1*a1;
            if (len_a2 <= denominator) {  // inside line segment from the other end point
                return 1;
            }
        }
    }
    return 0;

@chriswebers thanks for the improvement to the code. I have just updated the code.

I'm sure there are lots of other places which need improvement ;-)