This has been discussed at StackOverflow.
Everything rests on seeing that the problem you are describing is a graph problem. So, everything should begin with expressing your shape as a graph. There is an implementation in C which gives an example of the representation as a graph and which performs the task of calculating how many triangles there are.
Someone pointed at this paper, saying that the problem is computationally as difficult as Matrix Multiplication. Essentially the time complexity will be $O(n^3)$.
One of the answers there by evhen13 outlines the following candidate algorithm which is recursive in nature:
You will need depth first search. The algorithm will be:
1) For the current node ask all unvisited adjacent nodes
2) for each of those nodes run depth two check to see if a node at depth 2 is your current node from step one
3) mark current node as visited
4) on make each unvisited adjacent node your current node (1 by 1) and run the same algorithm