From f3475011dc52cbcc300d5dc5eaf11562475d0567 Mon Sep 17 00:00:00 2001 From: ti_mo Date: Tue, 21 Oct 2025 22:49:10 +0200 Subject: [PATCH] added circles - default all visible - boundary check to Triangulation to not try to triang less than 3 points --- .../ViewModels/MainWindowViewModel.cs | 33 ++++++++++--------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/DelauneyTriangulation/ViewModels/MainWindowViewModel.cs b/DelauneyTriangulation/ViewModels/MainWindowViewModel.cs index 19f4cb9..b8501cf 100644 --- a/DelauneyTriangulation/ViewModels/MainWindowViewModel.cs +++ b/DelauneyTriangulation/ViewModels/MainWindowViewModel.cs @@ -12,23 +12,26 @@ using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using DelaunatorSharp; using DelauneyTriangulation.Models; +using DelauneyTriangulation.Views; namespace DelauneyTriangulation.ViewModels; public partial class MainWindowViewModel : ViewModelBase { - [ObservableProperty] private int _pointCount = 20; - [ObservableProperty] private int _minPointDistance = 5; + public MainWindow? View { get; set; } = null; + [ObservableProperty] private int? _pointCount = 20; + [ObservableProperty] private int? _minPointDistance = 5; [ObservableProperty] private double _panX; [ObservableProperty] private double _panY; [ObservableProperty] private double _zoom = 1.0; [ObservableProperty] private bool _pointsVisible = true; - [ObservableProperty] private bool _edgesVisible = false; - [ObservableProperty] private bool _circlesVisible = false; - [ObservableProperty] private bool _trianglesVisible = false; + [ObservableProperty] private bool _edgesVisible = true; + [ObservableProperty] private bool _circlesVisible = true; + [ObservableProperty] private bool _voronoiVisible = true; + [ObservableProperty] private bool _trianglesVisible = true; - [ObservableProperty] private int _generationDelay = 20; + [ObservableProperty] private int? _generationDelay = 20; private CancellationTokenSource? _genCts; public double Width { get; set; } = 1280; @@ -160,10 +163,10 @@ public partial class MainWindowViewModel : ViewModelBase { Triangles.Clear(); Edges.Clear(); + + if(Points.Count < 3) return; - var pts = Points - .Select(p => (DelaunatorSharp.IPoint)new DelaunatorSharp.Point(p.X, p.Y)) - .ToArray(); + var pts = Points.Select(p => (DelaunatorSharp.IPoint)new DelaunatorSharp.Point(p.X, p.Y)).ToArray(); var d = new Delaunator(pts); @@ -196,10 +199,9 @@ public partial class MainWindowViewModel : ViewModelBase await DelayAsync(ct); } Edges.Add(new Geometries.Edge(first.X, first.Y, last.X, last.Y)); - - /* + // Voronoi Diagram Extraction - foreach (var cell in d.GetVoronoiCellsBasedOnCircumcenters()) + foreach (var cell in d.GetVoronoiCells()) { ct.ThrowIfCancellationRequested(); var f = cell.Points[0]; @@ -207,14 +209,15 @@ public partial class MainWindowViewModel : ViewModelBase { var s = cell.Points[i]; VoronoiDiagram.Add(new Geometries.Edge(f.X, f.Y, s.X, s.Y)); + Circles.Add(new Geometries.Circle(f.X, f.Y, Points.Min(x => PointDistance(new Geometries.Point(f.X, f.Y), x)))); + f = s; await DelayAsync(ct); } } - */ } - private Task DelayAsync(CancellationToken ct) => GenerationDelay > 0 ? Task.Delay(GenerationDelay, ct) : Task.CompletedTask; + private Task DelayAsync(CancellationToken ct) => GenerationDelay > 0 ? Task.Delay(GenerationDelay ?? 0, ct) : Task.CompletedTask; async Task RunGenerateAsync() { @@ -233,7 +236,7 @@ public partial class MainWindowViewModel : ViewModelBase Triangles.Clear(); VoronoiDiagram.Clear(); - await GenerateRandomPoints(_pointCount, ct); + await GenerateRandomPoints(PointCount ?? 20, ct); // await ConnectPoints(ct); // await Triangulate(ct); await DelaunayTriangulateAsync(ct);