I have some data in the form of Line objects (eg Line1(start, end), where start and end are coordinates in the form of point objects). Is there a quick way to go through all the lines to see if any of them form a triangle? By quick I mean anything better than going through all nC3 possibilities.
Edit: Just realised I may not understand all the replies (I'm no Adrian Lamo). Please try and explain wrt Python.
Is there a quick way to go through all the lines to see if any of them form a triangle?
Yes. Assuming that your Points are integers (or can be easily converted to such, because they have fixed significant digits or similar):
Being creative for you here:
std::multimap<int>
), one for each of the x and y coordinates of the endpoints, associating the coordinates with a pointer to the respective line1) geometric step: enter all line segments in a dictionary, with the first endpoint as the key and the second endpoint as the value. There will be duplicate keys, so you will keep a list of values rather than single values. In principle there will be no duplicates in the lists (unless you enter the same edge twice).
2) topological step: for all entries P
in the dictionary, consider all the pairs of elements from its list, let (Q, R)
. Lookup Q
and check if R
belongs to the list of Q
. If yes, you have found the triangle (P, Q, R)
.
By symmetry, all six permutations of every triangle will be reported. You can avoid that by enforcing that P<Q<R
in the lexicographical sense.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.