...

Source file src/oss.terrastruct.com/d2/lib/geo/math.go

Documentation: oss.terrastruct.com/d2/lib/geo

     1  package geo
     2  
     3  import "math"
     4  
     5  func EuclideanDistance(x1, y1, x2, y2 float64) float64 {
     6  	if x1 == x2 {
     7  		return math.Abs(y1 - y2)
     8  	} else if y1 == y2 {
     9  		return math.Abs(x1 - x2)
    10  	} else {
    11  		return math.Sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2))
    12  	}
    13  }
    14  
    15  // compare a and b and consider them equal if
    16  // difference is less than precision e (e.g. e=0.001)
    17  func PrecisionCompare(a, b, e float64) int {
    18  	if math.Abs(a-b) < e {
    19  		return 0
    20  	}
    21  	if a < b {
    22  		return -1
    23  	}
    24  	return 1
    25  }
    26  
    27  // TruncateDecimals truncates floats to keep up to 3 digits after decimal, to avoid issues with floats on different machines.
    28  // Since we're not launching rockets, 3 decimals is enough precision for what we're doing
    29  func TruncateDecimals(v float64) float64 {
    30  	return float64(int(v*1000)) / 1000
    31  }
    32  
    33  func Sign(i float64) int {
    34  	if i < 0 {
    35  		return -1
    36  	}
    37  	if i > 0 {
    38  		return 1
    39  	}
    40  	return 0
    41  }
    42  

View as plain text