This is a very interesting, and very hard problem.
Unfortunately, there isn't any neat solution to turn a set of points into triangles. What exactly would the points represent? One option is the that they would be the corners of the triangles. But how do you assign which three form a triangle? One way would be a Delaunay triangulation though this is not straightforward to compute.
I'm going to consider other ways to get this effect, first.
In my discussion of the solution, I'm going to assume you have a function triangle(a, b, c) which makes a triangle with the corners a, b, and c. You can look up functions that do this on freemodels.
I'm also going to assume that we're working around the origin or in some other "nice" place, so that things are really simple. Moving it to somewhere else wouldn't ever be terribly challenging.
Simpler Solution
We can recursively break the rectangle up into smaller rectangles -- then the rectangles eventually get broken into two triangles: http://i.imgur.com/8woT1Tg.png
The result looks like this: http://i.imgur.com/LSZWv2X.png
Here is the code I used to make it:
function rectangle(left, bottom, right, top)
wait();
local stop = math.random() < 0.2 or right - left < 2 or top - bottom < 2
if stop then
if math.random(2) == 1 then
triangle.render(
Vector3.new(left, bottom),
Vector3.new(right, top),
Vector3.new(right, bottom)
);
triangle.render(
Vector3.new(left, bottom),
Vector3.new(right, top),
Vector3.new(left, top)
);
else
triangle.render(
Vector3.new(left, top),
Vector3.new(right, bottom),
Vector3.new(left, bottom)
);
triangle.render(
Vector3.new(left, top),
Vector3.new(right, bottom),
Vector3.new(right, top)
);
end
else
local h1 = (right - left) * math.random() + left
local h2 = (right - left) * math.random() + left
h1, h2 = math.min(h1, h2), math.max(h1, h2)
local v1 = (top - bottom) * math.random() + bottom
local v2 = (top - bottom) * math.random() + bottom
v1, v2 = math.min(v1, v2), math.max(v1, v2)
rectangle(left, bottom, h1, v1);
rectangle(h1, bottom, h2, v1);
rectangle(h2, bottom, right, v1);
rectangle(left, v1, h1, v2);
rectangle(h1, v1, h2, v2);
rectangle(h2, v1, right, v2);
rectangle(left, v2, h1, top);
rectangle(h1, v2, h2, top);
rectangle(h2, v2, right, top);
end
end
rectangle(-6, -6, 6, 6);
This turns out better than I expected it to! In the picture I included, you'll notice that most of the triangles aren't quite horizontal / vertical. That's because the triangle drawing routine I have splits all triangles along the altitude from their hypotenuse -- that wouldn't be necessary, and you could just cut all the rectangles into precisely 2 WedgeParts (instead of 4)
Unfortunately, that will make it look fairly regular, which probably isn't what you want. Depending on circumstance, it's possible no one would notice.
Voronoi Observation
There's something called a Voronoi diagram which groups a plane by which control point is closest.
The idea is based on using the control points, and the vertices of their regions.
... The problem is the edges, which I don't have an answer for. I can edit with a little more later.
Making Triangles
http://hastebin.com/mejoyaniwo.js
6 comments
That makes loads of sense! But like you mentioned that regular look is a little off... I'll give you best answer if you end up finding anything on that Voroni diagram. If not, I'll still credit you, just get back to me on that please. On the edges, if the problem lies in how irregular the edges are, couldn't you just split those complex pieces into rectangles and wedges?
No, the tricky part is that there's no reason it should include the edges at all. I'm pretty sure there's some regular way to fix it, but it hasn't come to mind yet.
What are the triangle.render s supposed to be in the code?
They make a triangle with the given parameters as the corners, e.g., what this plugin does http://www.roblox.com/Triangle-Workshop-GUI-Tool-item?id=47368728 I will include my implementation as a link at the bottom
Thanks! The script creates a bit too many triangles though... Any way I could adjust that?
This makes general triangles, so they don't have to be right (90 degrees). That means for every single
render, you need 2 right triangles. You would have to write your own that simplifies it if you wanted to avoid that (which for the rectangly case, you can)