3

I tried to create an image like the one below as a random background for a website, but after multiple attempts I couldn't find an algorithm that gets the job done.

enter image description here

What should the program do?

It should be able to fill a plane randomly with triangles. These triangles should all be independent, so I don't simply want to draw long lines on the canvas an color the triangles that are created.

Algorithms I've tried so far:

1.

  • Make random Points
  • Make random Connections with a length below a specific value (This could leed to holes in the triangle net)
  • Try to find out what connections make a triangle (I failed here)

2.

  • Start with a single triangle
  • Create a new Point close to an existing connection and add a triangle from there that doesn't cause any intersections. This lead to problems whenever it left a little hole like in this picture:

    enter image description here

3.

  • Make random points
  • Make all possible connections (every point to every other)
  • Sort the connections by length
  • For every connection starting with the shortest draw if it isn't intersecting with any other drawn line. Otherwise delete the connection.

This was actually my best attemt even if it took the program far to long to get it done with only a few points. This is how the result looked:

enter image description here

I didn't find a way to find out what connections make a triangle and therefor I couldn't color them indipendently...

So hopefully you know a way to create a nice triangle-filled canvas like in the first picture and let me know...

Anton Ballmaier
  • 876
  • 9
  • 25
  • Couldnt you set one point, draw e.g. 4 randomly around and connect them with the middle one and around, then repeat with more points. – Jonas Wilms Jul 04 '17 at 19:43
  • 6
    Google *Delaunay triangulation*. – meowgoesthedog Jul 04 '17 at 19:52
  • 2
    Further to @spug's suggestion: Delaunay triangulations have the property that they maximise the smallest angle of any triangle -- that is, they avoid creating "long skinny" triangles (whenever it's possible to do so). – j_random_hacker Jul 04 '17 at 20:04
  • 1
    To be pedantic, the example image isn't a Delauney triangulation so doing that won't produce an image "like the one below", if that's important – samgak Jul 04 '17 at 21:02
  • @samgak Well, all I want it to do is looking good... for that a Delaunay triangulation should work :) – Anton Ballmaier Jul 04 '17 at 21:29

1 Answers1

2

A good solution is to start with random points (with your preferred distribution) and apply some triangulation algorithm. Among these, the Delaunay triangulation is a good candidate, for its low computational complexity and code availability.

  • I'm thankful for the tip, but I don't think this solves my problem with saving every single triangle as an object to be able to ultimately color them... – Anton Ballmaier Jul 04 '17 at 21:39
  • @AntonBallmaier: you are wrong to think that. A triangulation algorithm can allow you to enumerate all the faces, and create a triangle for each. –  Jul 04 '17 at 21:40
  • 3
    Well, then thanks a lot for your advice :) Oh, and take those 15 rep. :P – Anton Ballmaier Jul 04 '17 at 21:45