Skip to content

Instantly share code, notes, and snippets.

@ianlintner-wf
Created January 6, 2020 02:35
Show Gist options
  • Save ianlintner-wf/e3e5e4010d341beaf1d3b71139546ab9 to your computer and use it in GitHub Desktop.
Save ianlintner-wf/e3e5e4010d341beaf1d3b71139546ab9 to your computer and use it in GitHub Desktop.
func generate_voronoi_diagram(imgSize : Vector2, num_cells: int):
var img = Image.new()
img.create(imgSize.x, imgSize.y, false, Image.FORMAT_RGBH)
var points = []
var colors = []
for i in range(num_cells):
points.push_back(Vector2(int(randf()*img.get_size().x), int(randf()*img.get_size().y)))
randomize()
var colorPossibilities = [ Color.blue, Color.red, Color.green, Color.purple, Color.yellow, Color.orange]
colors.push_back(colorPossibilities[randi()%colorPossibilities.size()])
for y in range(img.get_size().y):
for x in range(img.get_size().x):
var dmin = img.get_size().length()
var j = -1
for i in range(num_cells):
var d = (points[i] - Vector2(x, y)).length()
if d < dmin:
dmin = d
j = i
img.lock()
img.set_pixel(x, y, colors[j])
img.unlock()
var texture = ImageTexture.new()
texture.create_from_image(img)
self.set_texture(texture)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment