Created
January 6, 2020 02:35
-
-
Save ianlintner-wf/e3e5e4010d341beaf1d3b71139546ab9 to your computer and use it in GitHub Desktop.
Godot Voroni Diagram (Source https://www.reddit.com/r/godot/comments/bazs8m/quick_voronoi_diagram/)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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