Number of Triangles that can be formed given a set of lines in Euclidean Plane | JavaScript program to find the number of triangles that can be formed using a set of lines in Euclidean Plane ; Double variables can ' t ▁ be ▁ checked ▁ precisely ▁ ▁ using ▁ ' == ' this function returns true if  the double variables are equal ; This function returns the number of triangles for a given set of lines ; Slope array stores the slope of lines ; Slope array is sorted so that all lines with same slope come together ; After sorting slopes , count different slopes . k is index in count [ ] . ; Count of current slope ; Calculating sum1 ( Sum of all slopes ) sum1 = m1 + m2 + ... ; Calculating sum2 . sum2 = m1 * m2 + m2 * m3 + ... ; Needed for sum3 ; Calculating sum3 which gives the final answer m1 * m2 * m3 + m2 * m3 * m4 + ... ; lines are stored as arrays of a , b and c for ' ax + by = c ' ; n is the number of lines
const EPSILON = 1.0842e-19 ; function compareDoubles ( A , B ) { var diff = A - B ; return diff < EPSILON && - diff < EPSILON ; } function numberOfTringles ( a , b , c , n ) { var slope = [ ] ; for ( var i = 0 ; i < n ; i ++ ) slope . push ( parseFloat ( a [ i ] * 1.0 ) / b [ i ] ) ; slope . sort ( ) ; var count = new Array ( n ) . fill ( 0 ) ; var k = 0 ; var this_count = 1 ; for ( var i = 1 ; i < n ; i ++ ) { if ( compareDoubles ( parseFloat ( slope [ i ] ) , parseFloat ( slope [ i - 1 ] ) ) ) this_count ++ ; else { count [ k ++ ] = this_count ; this_count = 1 ; } } count [ k ++ ] = this_count ; var sum1 = 0 ; for ( var i = 0 ; i < k ; i ++ ) sum1 += count [ i ] ; var sum2 = 0 ; var temp = new Array ( n ) . fill ( 0 ) ; for ( var i = 0 ; i < k ; i ++ ) { temp [ i ] = count [ i ] * ( sum1 - count [ i ] ) ; sum2 += temp [ i ] ; } sum2 /= 2 ; var sum3 = 0 ; for ( var i = 0 ; i < k ; i ++ ) sum3 += count [ i ] * ( sum2 - temp [ i ] ) ; sum3 /= 3 ; return sum3 ; } var a = [ 1 , 2 , 3 , 4 ] ; var b = [ 2 , 4 , 5 , 5 ] ; var c = [ 5 , 7 , 8 , 6 ] ; var n = a . length ; document . write ( " " + " " + numberOfTringles ( a , b , c , n ) ) ;