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 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;
@@ -161,9 +164,9 @@ public partial class MainWindowViewModel : ViewModelBase
Triangles.Clear();
Edges.Clear();
var pts = Points
.Select(p => (DelaunatorSharp.IPoint)new DelaunatorSharp.Point(p.X, p.Y))
.ToArray();
if(Points.Count < 3) return;
var pts = Points.Select(p => (DelaunatorSharp.IPoint)new DelaunatorSharp.Point(p.X, p.Y)).ToArray();
var d = new Delaunator(pts);
@@ -197,9 +200,8 @@ public partial class MainWindowViewModel : ViewModelBase
}
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);