# Fixing issue with Generating 2D Triangles?

Hello Everyone, so i ran into a bit of a problem, after making a “simple” script to Triangulate a polygon using Delaunay Triangulation and using the method EgoMoose graciously provided to make a 2D triangle Here, The polygon generated sometimes would have some weird artifacts:

See Those lines showing, where the border of the triangles are, those are the artifacts i am talking about and i don’t want them to happen. The triangles shouldn’t have borders at all, the are completely white triangles btw. I’ve heard that this can be partly due to how images are processed in roblox or even opengl(dont know much on that one). I’ve also heard this could be due to floating point errors.

Code To make a 2D triangle
``````-----Script from Ego Moose's Tutorial
local extra = 2;

local img = Instance.new("ImageLabel");
img.BackgroundTransparency = 1;
img.BorderSizePixel = 0;

function dotv2(a, b)
return a.x * b.x + a.y * b.y;
end;

function rotateV2(vec, angle)
local x = vec.x * math.cos(angle) + vec.y * math.sin(angle);
local y = -vec.x * math.sin(angle) + vec.y * math.cos(angle);
return Vector2.new(x, y);
end;

function drawTriangle(a, b, c, parent)
local edges = {
{longest = (c - b), other = (a - b), position = b};
{longest = (a - c), other = (b - c), position = c};
{longest = (b - a), other = (c - a), position = a};
};

table.sort(edges, function(a, b) return a.longest.magnitude > b.longest.magnitude end);

local edge = edges[1];
edge.angle = math.acos(dotv2(edge.longest.unit, edge.other.unit));
edge.x = edge.other.magnitude * math.cos(edge.angle);
edge.y = edge.other.magnitude * math.sin(edge.angle);

local r = edge.longest.unit * edge.x - edge.other;
local rotation = math.atan2(r.y, r.x) - math.pi/2;

local tp = -edge.other;
local tx = (edge.longest.unit * edge.x) - edge.other;
local nz = tp.x * tx.y - tp.y * tx.x;

local tlc1 = edge.position + edge.other ;
local tlc2 = nz > 0 and edge.position + edge.longest - tx or edge.position - tx;

local tasize = Vector2.new((tlc1 - tlc2).magnitude, edge.y);
local tbsize = Vector2.new(edge.longest.magnitude - tasize.x, edge.y);

local center1 = nz <= 0 and edge.position + ((edge.longest + edge.other)/2) or (edge.position + edge.other/2);
local center2 = nz > 0 and edge.position + ((edge.longest + edge.other)/2) or (edge.position + edge.other/2);

tlc1 = center1 + rotateV2(tlc1 - center1, rotation);
tlc2 = center2 + rotateV2(tlc2 - center2, rotation);

local ta = img:Clone();
local tb = img:Clone();
ta.Image = "rbxassetid://319692171";
tb.Image = "rbxassetid://319692151";

ta.Position = UDim2.new(0,  tlc1.x , 0, tlc1.y);
tb.Position = UDim2.new(0, tlc2.x  , 0, tlc2.y);
ta.Size = UDim2.new(0, tbsize.x + extra, 0, tbsize.y + extra);
tb.Size = UDim2.new(0, tasize.x + extra, 0, tasize.y + extra);
ta.Rotation = math.deg(rotation);
tb.Rotation = ta.Rotation;
ta.Parent = parent;
tb.Parent = parent;
end

``````

Does anyone know why this can occur or ways to fix it or better methods of generating 2D triangles for polygons in roblox so they don’t have borders or anything like that?

any help or comments are welcome!

You can always ask for clarification if i didn’t explain something!

4 Likes

It might be helpful to not use ImageLabels, because the image might be the issue, though I wouldn’t know, as I am not that great with geometry.

1 Like

i would love not to use image labels, but i’m not sure how to create a triangle from 3 vertices without using them, since there is no triangle shape, i mean it might be possible to use clip descendants but that would create more frames than i preferably want. Though i did fix part of the problem by using Pixel Fix to remove some of the gray borders from the triangle. But i still have an issue in getting the triangles to align correctly together, initially i thought this could be a floating point error with the my points/positions generated from the triangulation , but the positions are all integers.

(The gaps(the gaps in between the triangles not the hole) shouldn’t be there)

I am lead to believe after thinking for a bit every so often on this, that my issue could be some weird floating point error that is occurring. though Im not entirely sure how roblox renders stuff i have however heard things that have do with pixel gaps issue in opengl and some other graphic renderers/libraries.

Did you find a solution to this yet? I’m having the same problem.

Can you find some way to test if having non-integer pixel coordinates even does anything? If the fractional part of every coordinate is being thrown out then that might explain it.