adamfranco / curvature

Find roads that are the most curvy or twisty based on Open Street Map (OSM) data.
http://roadcurvature.com/
225 stars 39 forks source link

Division by zero error on 1-1-2 triangles #40

Open adamfranco opened 8 years ago

adamfranco commented 8 years ago

For pairs of segments that have the same length and a base that is twice their length, the circumcircle-radius algorithm results in a division-by-zero error. The test script below will demonstrate this.

#!/usr/bin/env python
# -*- coding: UTF-8 -*-

import math
import sys

a = 0.0949651002884
b = 0.0949651002884
c = 0.189930200577

a = 1
b = 1
c = 2

sys.stderr.write("\n\na={}\nb={}\nc={}\nfabs={}\n(a+b+c)={}\n(b+c-a)={}\n(c+a-b)={}\n(a+b-c)={}\n\n".format(a,b,c, math.fabs((a+b+c)*(b+c-a)*(c+a-b)*(a+b-c)), (a+b+c), (b+c-a), (c+a-b), (a+b-c)))

r = (a * b * c)/math.sqrt(math.fabs((a+b+c)*(b+c-a)*(c+a-b)*(a+b-c)))

print r

I've not yet found another algorithm for finding the circumcircle radius that doesn't have this issue, but there may be one out there.

Fonsan commented 7 years ago

I think the issue is that the three data points simply are in a straight line. If you imagine a triangle in which the base is equal to the two remaining sides the height will be very close to zero.

I think we can handle this by checking if the sum of a and b is less than c we simply return the max radius

adamfranco commented 7 years ago

Oh, thanks for this explanation -- makes total sense. :-)