Counting triangles |
Acme Counting Machines has hired you as a consultant to finish their TCM-2000 project. The TCM-2000 is a machine for counting triangles in pictures by means of a video camera attached to a computer. They have already done the easy part of preprocessing the picture to extract the edges and convert the image into a list of segments. Your job is to write a program to count all the triangles defined by those segments.
Your program will receive a list segments and it has to return the number of triangles in the figure. The segments given to the program may intersect and may overlap (totally or partially). For example, given the following figure:
Your program may receive the following list of segments: AB, AD, AF, BC, BF, CD, CE, CF, DE and DF (it can receive a different input for that same figure). Then, it will have to count the 9 triangles present in the figure, namely: ABC, ABF, ACD, ADC, ADF, BCD, BCF, CDE and CDF.
Hint: remember to allow some tolerance if you ever have to compare floating-point numbers.
The input format is as follows:
An integer in a single line which says the number of figures that your program will have to process. Then, for each figure there will be:
An integer in a single line which says the number of segments in the figure.
A line for each segment, with four numbers each line. The two first numbers are the coordinates of one of the end points of the segment, and the two last numbers are the coordinates of the second end point. The numbers may be either integers or floating point numbers with at most one figure after the decimal point.
The output consists of one line for each figure, with a single integer number each: the number of triangles found.
2 10 0 0 0 1 0 0 0.5 0.5 0 0 1 0 0 1 0.5 0.5 0 1 0.5 1.5 0 1 1 1 0.5 0.5 1 0 0.5 0.5 1 1 0.5 1.5 1 1 1 0 1 1 8 0 0 1 0 0 0 0 1 0 1 1 0 1 0 1 1 1 1 0.5 1.5 0 1 0.5 1.5 0 0 1 1 0 1 1 1
9 9