i came across this problem in a competitive coding class :
A number of lines (extending infinity) in both directions are drawn on a plane. the lines are specified by the angle (positive or negative) made with the x axis(in degrees,constrained to -89° to 90° ).
the objective is to determine the number of triangles formed by the set of these lines.
An example:
If the lines are given with an angle of 10,70,30,30(with the x-axis) the figure looks like this
here there are two triangles (L1,L2,L3 and L1,L2,L4).
Best Answer
Any set of three lines at different angles will form a triangle unless they pass through a common point. If we exclude cases where three lines go through a point, count the number of lines at each angle. I will take an example where the angles are $-10,-5,0,10,10,10,20,20,30,40,50$. We can represent this as $(1,1,1,3,2,1,1,1)$ because there are $3$ lines at $10$ and $2$ lines at $20$.
If all the lines were at different angles there would be ${11 \choose 3}=165$ triangles. We need to subtract the number of ways to get two parallel lines and one nonparallel, which is ${3 \choose 2}\cdot 8+{2 \choose 2}\cdot 9=33$. We then have to subtract the number of ways of getting three parallel lines, which is ${3\choose 3}=1$. The answer is then $131$.
If there are $n_i$ lines at angle $i$ and $n=\sum_i n_i$ is the total number of lines, the number of triangles is $${n \choose 3}-\sum_i \left((n-n_i){n_i \choose 2} -{n_i \choose 3}\right)$$