added circles - default all visible - boundary check to Triangulation to not try to triang less than 3 points

This commit is contained in:
ti_mo
2025-10-21 22:49:10 +02:00
parent 70f73d00d1
commit f3475011dc
@@ -12,23 +12,26 @@ using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input; using CommunityToolkit.Mvvm.Input;
using DelaunatorSharp; using DelaunatorSharp;
using DelauneyTriangulation.Models; using DelauneyTriangulation.Models;
using DelauneyTriangulation.Views;
namespace DelauneyTriangulation.ViewModels; namespace DelauneyTriangulation.ViewModels;
public partial class MainWindowViewModel : ViewModelBase public partial class MainWindowViewModel : ViewModelBase
{ {
[ObservableProperty] private int _pointCount = 20; public MainWindow? View { get; set; } = null;
[ObservableProperty] private int _minPointDistance = 5; [ObservableProperty] private int? _pointCount = 20;
[ObservableProperty] private int? _minPointDistance = 5;
[ObservableProperty] private double _panX; [ObservableProperty] private double _panX;
[ObservableProperty] private double _panY; [ObservableProperty] private double _panY;
[ObservableProperty] private double _zoom = 1.0; [ObservableProperty] private double _zoom = 1.0;
[ObservableProperty] private bool _pointsVisible = true; [ObservableProperty] private bool _pointsVisible = true;
[ObservableProperty] private bool _edgesVisible = false; [ObservableProperty] private bool _edgesVisible = true;
[ObservableProperty] private bool _circlesVisible = false; [ObservableProperty] private bool _circlesVisible = true;
[ObservableProperty] private bool _trianglesVisible = false; [ObservableProperty] private bool _voronoiVisible = true;
[ObservableProperty] private bool _trianglesVisible = true;
[ObservableProperty] private int _generationDelay = 20; [ObservableProperty] private int? _generationDelay = 20;
private CancellationTokenSource? _genCts; private CancellationTokenSource? _genCts;
public double Width { get; set; } = 1280; public double Width { get; set; } = 1280;
@@ -160,10 +163,10 @@ public partial class MainWindowViewModel : ViewModelBase
{ {
Triangles.Clear(); Triangles.Clear();
Edges.Clear(); Edges.Clear();
if(Points.Count < 3) return;
var pts = Points var pts = Points.Select(p => (DelaunatorSharp.IPoint)new DelaunatorSharp.Point(p.X, p.Y)).ToArray();
.Select(p => (DelaunatorSharp.IPoint)new DelaunatorSharp.Point(p.X, p.Y))
.ToArray();
var d = new Delaunator(pts); var d = new Delaunator(pts);
@@ -196,10 +199,9 @@ public partial class MainWindowViewModel : ViewModelBase
await DelayAsync(ct); await DelayAsync(ct);
} }
Edges.Add(new Geometries.Edge(first.X, first.Y, last.X, last.Y)); Edges.Add(new Geometries.Edge(first.X, first.Y, last.X, last.Y));
/*
// Voronoi Diagram Extraction // Voronoi Diagram Extraction
foreach (var cell in d.GetVoronoiCellsBasedOnCircumcenters()) foreach (var cell in d.GetVoronoiCells())
{ {
ct.ThrowIfCancellationRequested(); ct.ThrowIfCancellationRequested();
var f = cell.Points[0]; var f = cell.Points[0];
@@ -207,14 +209,15 @@ public partial class MainWindowViewModel : ViewModelBase
{ {
var s = cell.Points[i]; var s = cell.Points[i];
VoronoiDiagram.Add(new Geometries.Edge(f.X, f.Y, s.X, s.Y)); 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; f = s;
await DelayAsync(ct); 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() async Task RunGenerateAsync()
{ {
@@ -233,7 +236,7 @@ public partial class MainWindowViewModel : ViewModelBase
Triangles.Clear(); Triangles.Clear();
VoronoiDiagram.Clear(); VoronoiDiagram.Clear();
await GenerateRandomPoints(_pointCount, ct); await GenerateRandomPoints(PointCount ?? 20, ct);
// await ConnectPoints(ct); // await ConnectPoints(ct);
// await Triangulate(ct); // await Triangulate(ct);
await DelaunayTriangulateAsync(ct); await DelaunayTriangulateAsync(ct);