Error: Invalid polygon data, triangulation failed

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By RoniPerson

I try to draw a colored polygon usingCanvasItem. draw_colored_polygon.
But i get the error message:

E 0:00:01.129   canvas_item_add_polygon: Invalid polygon data, triangulation failed.
  <C++-Fehler>  Condition "indices.empty()" is true.
  <C++-Quellcode>servers/visual/visual_server_canvas.cpp:770 @ canvas_item_add_polygon()
  <Stacktrace>  Water.gd:38 @ _draw()

This is my code:

var pool_points = PoolVector2Array()
pool_points.append(Vector2(10,0))
pool_points.append(Vector2(-10,0))
pool_points.append(Vector2(10,10))
pool_points.append(Vector2(-10,10))
draw_colored_polygon(pool_points, Color("#29adff"))
:bust_in_silhouette: Reply From: p7f

Thats because the order of the points you gave doesnt correspond to a shape that cannot be easily interpreted. Try this other order:

var pool_points = PoolVector2Array()
pool_points.append(Vector2(10,0))
pool_points.append(Vector2(-10,0))
pool_points.append(Vector2(-10,10))
pool_points.append(Vector2(10,10))
draw_colored_polygon(pool_points, Color(1,1,1,1))

If you follow your original order of points, and close the polygon with the starting point, you get overlapping paths, like here

is there a function for checking if an triangulation is possible?
Because im generating a wave effect and it seems that sometimes it is not possible to triangulate.

RoniPerson | 2020-07-06 17:04

Yes, you could try with Geometry.triangulate_polygon function:

var pool_points = PoolVector2Array()
pool_points.append(Vector2(10,0))
pool_points.append(Vector2(-10,0))
pool_points.append(Vector2(10,10))
pool_points.append(Vector2(-10,10))

if Geometry.triangulate_polygon(pool_points).empty():
	print("failed")
else:
	print("triangulated")
    draw_colored_polygon(pool_points, Color(1,1,1,1))

p7f | 2020-07-06 17:42

is there any easy way to fix the points that dont triangulate? Or at least find which points are the problem?

jgee_23_ | 2023-03-30 08:37

1 Like