How to get the amount of triangles?

The answer is 24, how to get it by GH?


1.gh (2.1 KB)

Here is one solution

Triangles_240107-RG.gh (16.6 KB)

3 Likes

here is another solution that uses geometry only to define adjacency, the traverses the graph recognizing in-line-nodes as single edges (which I believe is the most pressing issue, or at least it was for me when trying to just traverse the graph :slight_smile: )

Python code taken from here: python - Counting triangles in a hexagon - Stack Overflow

1_Re.gh (18.6 KB)

6 Likes

The answer is nine. :rofl:


triangles_2024Jan7a.gh (13.3 KB)

P.S. Wait, only five of the fragments from SrfSplit are triangles:


triangles_2024Jan7b.gh (17.0 KB)

The goal of this thread needs clarification :question:

1 Like

Took me quite awhile to understand the objective here, and then I finally understood @inno’s solution.

So I started with his code and added a little bit to his Python to output his list of triangles as ‘a’:

trishapes = []
print('--- triangles ---')
for triangle in sorted(triangles):
    print(triangle)
    trishapes.append(triangle)

a = trishapes

Then I added the purple group to parse that list of points that make each triangle.
Needed this one line of Python to get only the numbers (point indices):

a = filter(str.isdecimal,s)

And finally added List Item to view each triangle, one at a time.


triangles_2024Jan7c.gh (28.6 KB)

Nice work @inno :exclamation:

P.S. Now I realize that @RadovanG also figured it out. Nice work @RadovanG :exclamation:

Took me awhile to wake up. :sunglasses:

2 Likes

well, consider that the whole code comes from the big brains @ stackoverflow, not from mine :slight_smile:

the interesting part is indeed to use geometry just to get the initial data, and detaching the problem solving process from any geometry by transforming the whole thing into a graph traversal

also, it’s still a bit vague in my mind how to exactly shatter line AB with points 1, 2, 3 in GH, in such a way to get all the possible segment combinations [A1, 1B, A2, 2B, A3, 3B, 12, 23, 13, … AB] :rofl: (thing I believe @RadovanG has solved in the first part of his definition, but I didn’t have time to explore yet [edit] oh, seen it now, it’s sort of bruteforce approach by crossreferencing edges, culls duplicate combinations of edge indexes that would result in the same data, and pushes the hard work -to understand if the candidate edges create a tringle- to Curve.CreateBooleanRegions :+1: )

2 Likes

Still, well done! Python has some great tools for combinations:

Hadn’t thought about this that way… :thinking:

it think it mostly depends whether it’s a one-shot well-definite problem, or a solution for a “general case”

I believe the problem you linked can be identified in the class of “coin change problem”, bruteforcing it works while the number you have to reach is “resonable”
but if the problems wants you to list all different combinations to get to something overwhelming, like to 1 million, by summing the numbers [1, 2, 3] then I would drift toward a dynamic programming approach ( https://www.youtube.com/watch?v=H9bfqozjoqs&ab_channel=NeetCode )

in a similar flavor, finding triangles for 7 lines can be solved geometrically and it gives a perfect final solution in a matter of milliseconds, but if you have 15 thousand lines then I’d go for a graph-based approach :slight_smile:

2 Likes

Unfortunately, graph theory is missing from my education and experience. I remember the coin change problem, from way back (~52 years!) in my introductory Basic and Fortran classes. That video is good but gets tedious… I stopped watching at 12:20. Thanks.

1 Like

I use the same Python code shared by @inno with a little modification

I test shapes from these videos:

count triangles.gh (21.3 KB)

2 Likes