How to generate a map of random, connectd polygons

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

I’m attempting to generate a map of connected, randomly generated, polygons to act as a map of city districts for a management game.

Something like this:

However the results I’m getting are… not great. I can generate an initial starting polygon fine but the others added on get distorted. In this test the center is fine, but all the surrounding polygons should be 4 sided with the same dimension.

enter link description here

This is the current code I’m using to spawn the secondary polygons (‘poly’ is the central one that’s already created at this step) with all RNG disabled for testing ATM.

On top of the distortion I also need to figure out to how to ensure any adjacent polygons are connected and to fill in gaps, looking to generate a connected map of between 20-30 polygons.

I’m certain I’m overlooking a very simple solution but my brain is just not parsing it.

	for index in poly.polygon.size():
	var point_a = poly.polygon[index]
	var point_b
	var point_c
	
	if (index + 1 < poly.polygon.size()):
		point_b = poly.polygon[index+1]
	else:
		point_b = poly.polygon[0]
		
	point_c = Vector2((point_a.x + point_b.x) / 2 , (point_a.y + point_b.y) / 2)
	
	var lamba = center.angle_to_point(point_c)
	radius = 80 #* rng.randf_range(0.9, 1.1)
	var center_2 = Vector2(point_c.x + radius * cos(lamba), point_c.y + radius * sin(lamba))
	var polygon_array_2 = []
	number_of_vertix = 4 #rng.randi_range(4, 7)

	for count in number_of_vertix:
		if (count == 0):
			polygon_array_2.append(Vector2(point_a.x , point_a.y))
			
		elif (count == 1):
			polygon_array_2.append(Vector2(point_b.x , point_b.y))
			
		else:
			radius = 80 #* rng.randf_range(0.9, 1.1)
			
			theta = deg_to_rad(((360 / (number_of_vertix)) * 1) * (count))
			theta = theta #* rng.randf_range(0.99, 1.05)
			
			this_x = center_2.x + (cos(theta) * radius)
			this_y = center_2.y - (sin(theta) * radius)
			
			this_x = this_x #* rng.randf_range(0.99, 1.01)
			this_y = this_y #* rng.randf_range(0.99, 1.01)
			
			polygon_array_2.append(Vector2(this_x, this_y))
	polygon_array_2.sort_custom(func(a,b): return (a - center_2).angle() < (b - center_2).angle())
	map_array.append(polygon_array_2)