From a677e2d70fd7f2d1a8861d9fe0d44daf6ec4c314 Mon Sep 17 00:00:00 2001 From: Valentin Stark Date: Mon, 3 Oct 2022 21:43:07 +0200 Subject: [PATCH] troncage du poisson --- Poisson.gd | 71 ++++++++++++++++++++++++++++++++++++++++++++ Poisson.tscn | 32 +++++++++++++++----- Previsualisation2.gd | 23 ++++++++++++++ 3 files changed, 119 insertions(+), 7 deletions(-) create mode 100644 Previsualisation2.gd diff --git a/Poisson.gd b/Poisson.gd index fe47ae9..1164727 100644 --- a/Poisson.gd +++ b/Poisson.gd @@ -10,11 +10,19 @@ var data = { spacing = default_spacing } +var polygon_data = { + width = 500, + height = 500, + spacing = 1.5 +} + var poisson_disc_sampling: PoissonDiscSampling = PoissonDiscSampling.new() var points = PoolByteArray() var poissons = [] +var truncs = [] signal new_poisson(points) +signal new_truncs(truncs) func _ready(): reset_options(default_width, default_height, default_spacing) @@ -72,3 +80,66 @@ func _on_OpenFileDialog_file_selected(path): reset_options(new_data.width, new_data.height, new_data.spacing) points = file.get_var() emit_signal("new_poisson", points) + + +func _on_trunc_pressed(): + truncs = [] + # Création d'un polygon + var polygon = PoolVector2Array([ + Vector2(0, 0), + Vector2(polygon_data.width, 0), + Vector2(polygon_data.width, + polygon_data.height), + Vector2(0, polygon_data.height) + ]) + + # Exemple d'hexagone + polygon = PoolVector2Array([ + Vector2(200, 0), + Vector2(400, 133), + Vector2(400, 266), + Vector2(200, 400), + Vector2(0, 266), + Vector2(0, 133), + ]) + + + + # Spacing + # Si sup à celui de base, le polygon doit se rétrécir proportionnellement + # Les positions des points augmentent à la fin + # Et inversement + + var factor = float(polygon_data.spacing) / float(data.spacing) + + for i in polygon.size(): + var vector = Vector2(polygon[i].x / factor, polygon[i].y / factor) + polygon.set(i, vector) + + var max_point = Vector2(0, 0) + for point in polygon: + if point.x > max_point.x: + max_point.x = point.x + if point.y > max_point.y: + max_point.y = point.y + + # Déplacement du polygon de telle sorte qu'il reste dans le carré original. + var rng = RandomNumberGenerator.new() + rng.randomize() + var offset = Vector2(rng.randi_range(0, data.width - max_point.x), rng.randi_range(0, data.height - max_point.y)) + polygon = Transform2D(0, offset).xform(polygon) + max_point += offset + + + + # On récupère les points à l'intérieur du polygon + for i in range(offset.x, max_point.x): + for j in range(offset.y, max_point.y): + var trunc = Vector2(i, j) + if Geometry.is_point_in_polygon(trunc, polygon): + if points[i + j * data.width]: + trunc -= offset + trunc *= factor + truncs.append(trunc) + + emit_signal("new_truncs", truncs) diff --git a/Poisson.tscn b/Poisson.tscn index 3eafc7b..760fcfe 100644 --- a/Poisson.tscn +++ b/Poisson.tscn @@ -1,7 +1,8 @@ -[gd_scene load_steps=3 format=2] +[gd_scene load_steps=4 format=2] [ext_resource path="res://Poisson.gd" type="Script" id=1] [ext_resource path="res://Previsualisation.gd" type="Script" id=2] +[ext_resource path="res://Previsualisation2.gd" type="Script" id=3] [node name="Poisson" type="Control"] anchor_right = 1.0 @@ -14,7 +15,7 @@ margin_bottom = 40.0 [node name="Properties" type="VBoxContainer" parent="HBoxContainer"] margin_right = 121.0 -margin_bottom = 152.0 +margin_bottom = 176.0 [node name="width" type="HBoxContainer" parent="HBoxContainer/Properties"] margin_right = 121.0 @@ -69,26 +70,41 @@ margin_right = 121.0 margin_bottom = 104.0 text = "Générer" -[node name="open" type="Button" parent="HBoxContainer/Properties"] +[node name="trunc" type="Button" parent="HBoxContainer/Properties"] margin_top = 108.0 margin_right = 121.0 margin_bottom = 128.0 -text = "Ouvrir" +text = "Tronquer" -[node name="save" type="Button" parent="HBoxContainer/Properties"] +[node name="open" type="Button" parent="HBoxContainer/Properties"] margin_top = 132.0 margin_right = 121.0 margin_bottom = 152.0 +text = "Ouvrir" + +[node name="save" type="Button" parent="HBoxContainer/Properties"] +margin_top = 156.0 +margin_right = 121.0 +margin_bottom = 176.0 text = "Enregistrer" [node name="ScrollContainer" type="ScrollContainer" parent="."] margin_left = 125.0 -margin_right = 653.0 -margin_bottom = 502.0 +margin_right = 640.0 +margin_bottom = 503.0 [node name="Previsualisation" type="TextureRect" parent="ScrollContainer"] script = ExtResource( 2 ) +[node name="ScrollContainer2" type="ScrollContainer" parent="."] +margin_left = 678.0 +margin_top = 2.0 +margin_right = 1193.0 +margin_bottom = 505.0 + +[node name="Previsualisation" type="TextureRect" parent="ScrollContainer2"] +script = ExtResource( 3 ) + [node name="SaveFileDialog" type="FileDialog" parent="."] margin_left = 83.0 margin_top = 133.0 @@ -107,7 +123,9 @@ mode = 0 access = 2 [connection signal="new_poisson" from="." to="ScrollContainer/Previsualisation" method="_on_Control_new_poisson"] +[connection signal="new_truncs" from="." to="ScrollContainer2/Previsualisation" method="_on_Poisson_new_truncs"] [connection signal="pressed" from="HBoxContainer/Properties/generate" to="." method="_on_Button_pressed"] +[connection signal="pressed" from="HBoxContainer/Properties/trunc" to="." method="_on_trunc_pressed"] [connection signal="pressed" from="HBoxContainer/Properties/open" to="." method="_on_open_pressed"] [connection signal="pressed" from="HBoxContainer/Properties/save" to="." method="_on_save_pressed"] [connection signal="file_selected" from="SaveFileDialog" to="." method="_on_SaveFileDialog_file_selected"] diff --git a/Previsualisation2.gd b/Previsualisation2.gd new file mode 100644 index 0000000..0c1405e --- /dev/null +++ b/Previsualisation2.gd @@ -0,0 +1,23 @@ +extends TextureRect + +var image + +func create_image(points): + var width = get_parent().get_parent().polygon_data.width + var height = get_parent().get_parent().polygon_data.height + image = Image.new() + image.create(width, height, false, Image.FORMAT_RGBA8) + image.fill(Color.white) + image.lock() + for point in points: + image.set_pixel(point.x, point.y, Color.black) + image.unlock() + +func update_texture(): + var texture = ImageTexture.new() + texture.create_from_image(image) + set_texture(texture) + +func _on_Poisson_new_truncs(truncs): + create_image(truncs) + update_texture()