简体   繁体   中英

Can anyone suggest a quick triangle detection algorithm?

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:

  1. Make two fastly searchable storage structures (eg std::multimap<int> ), one for each of the x and y coordinates of the endpoints, associating the coordinates with a pointer to the respective line
  2. In the first structure, search for elements with the same x coordinate. Those are the only "candidates" for being a corner of a triangle. Searching for duplicate entries is fast, because of you using an appropriate data structure.
  3. For each of these, verify whether they are actually an edge. If they are not, discard.
  4. For each of the remaining corners, verify that both "opposite" line ends are part of the same edge. Discard the others. Done.

1) 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM