added extra algorithms - made generation abordable

This commit is contained in:
ti_mo
2026-01-09 13:54:17 +01:00
parent a0d8ed79b7
commit a9c157abe0
2 changed files with 41 additions and 7 deletions
@@ -32,6 +32,7 @@ public partial class MainWindowViewModel : ViewModelBase
[ObservableProperty] private bool _trianglesVisible = true;
[ObservableProperty] private bool _doConnectPoints = false;
[ObservableProperty] private bool _doConnectPointsV2 = false;
[ObservableProperty] private bool _doNearestTriangle = false;
[ObservableProperty] private bool _doDelaunayLib = false;
[ObservableProperty] private bool _doBowyerWatson = true;
@@ -39,6 +40,8 @@ public partial class MainWindowViewModel : ViewModelBase
[ObservableProperty] private int? _generationDelay = 20;
private CancellationTokenSource? _genCts;
[ObservableProperty] private bool _isGenerationRunning = false;
public double Width { get; set; } = 1280;
public double Height { get; set; } = 720;
@@ -171,6 +174,24 @@ public partial class MainWindowViewModel : ViewModelBase
}
}
async Task ConnectPointsV2(CancellationToken ct)
{
if(Points.Count == 0) return;
Edges.Clear();
foreach (var p1 in Points)
{
foreach (var p2 in Points)
{
if (p1 == p2) continue;
if(Edges.Any(x => (x.X1 == p1.X && x.Y1 == p1.X && x.X2 == p2.X && x.Y2 == p2.Y) || (x.X1 == p2.X && x.Y1 == p2.X && x.X2 == p1.X && x.Y2 == p1.Y))) continue;
ct.ThrowIfCancellationRequested();
ConnectPoint(p1, p2);
await DelayAsync(ct);
}
}
}
void AddTriangle(Geometries.Point p1, Geometries.Point p2, Geometries.Point p3) => Triangles.Add(new(p1, p2, p3));
async Task Triangulate(CancellationToken ct)
@@ -521,6 +542,8 @@ public partial class MainWindowViewModel : ViewModelBase
}
}
public void CancelGeneration() => _genCts?.CancelAsync();
private Task DelayAsync(CancellationToken ct) =>
GenerationDelay > 0 ? Task.Delay(GenerationDelay ?? 0, ct) : Task.CompletedTask;
@@ -534,6 +557,7 @@ public partial class MainWindowViewModel : ViewModelBase
}
catch (OperationCanceledException)
{
IsGenerationRunning = false;
}
}
@@ -545,11 +569,16 @@ public partial class MainWindowViewModel : ViewModelBase
Triangles.Clear();
VoronoiDiagram.Clear();
IsGenerationRunning = true;
await GenerateRandomPoints(PointCount ?? 20, ct);
if(DoConnectPoints) await ConnectPoints(ct);
if(DoNearestTriangle) await Triangulate(ct);
if(DoDelaunayLib) await DelaunayTriangulateAsync(ct);
if(DoBowyerWatson) await BowyerWatson(ct);
else if(DoConnectPointsV2) await ConnectPointsV2(ct);
else if(DoNearestTriangle) await Triangulate(ct);
else if(DoDelaunayLib) await DelaunayTriangulateAsync(ct);
else if(DoBowyerWatson) await BowyerWatson(ct);
IsGenerationRunning = false;
}
private bool CanGenerate() => true;
+9 -4
View File
@@ -109,16 +109,21 @@
<TextBlock Text="Used Algorithm:" Foreground="White"/>
<Grid ColumnDefinitions="1*, 1*">
<RadioButton GroupName="Algorithm" HorizontalAlignment="Left" Content="Connect Points" IsChecked="{Binding DoConnectPoints}" Foreground="White" Margin="10,0"/>
<RadioButton Grid.Column="1" GroupName="Algorithm" HorizontalAlignment="Left" Content="Nearest Triangle" IsChecked="{Binding DoNearestTriangle}" Foreground="White" Margin="10,0"/>
<RadioButton Grid.Column="1" GroupName="Algorithm" HorizontalAlignment="Left" Content="ConnectPointsV2" IsChecked="{Binding DoConnectPointsV2}" Foreground="White" Margin="10,0"/>
</Grid>
<Grid ColumnDefinitions="1*, 1*">
<RadioButton GroupName="Algorithm" HorizontalAlignment="Left" Content="BowyerWatson" IsChecked="{Binding DoBowyerWatson}" Foreground="White" Margin="10,0"/>
<RadioButton Grid.Column="1" GroupName="Algorithm" HorizontalAlignment="Left" Content="Delaunay (Delaunator)" IsChecked="{Binding DoDelaunayLib}" Foreground="White" Margin="10,0"/>
<RadioButton GroupName="Algorithm" HorizontalAlignment="Left" Content="Nearest Triangle" IsChecked="{Binding DoNearestTriangle}" Foreground="White" Margin="10,0"/>
<RadioButton Grid.Column="1" GroupName="Algorithm" HorizontalAlignment="Left" Content="BowyerWatson" IsChecked="{Binding DoBowyerWatson}" Foreground="White" Margin="10,0"/>
</Grid>
<Grid ColumnDefinitions="1*, 1*">
<RadioButton GroupName="Algorithm" HorizontalAlignment="Left" Content="Delaunay (Delaunator)" IsChecked="{Binding DoDelaunayLib}" Foreground="White" Margin="10,0"/>
<!-- <RadioButton Grid.Column="1" GroupName="Algorithm" HorizontalAlignment="Left" Content="Delaunay (Delaunator)" IsChecked="{Binding DoDelaunayLib}" Foreground="White" Margin="10,0"/> -->
</Grid>
</StackPanel>
</Border>
</StackPanel>
<Button Grid.Row="1" Foreground="White" VerticalAlignment="Bottom" HorizontalAlignment="Stretch" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Content="Generate" Command="{Binding GenerateCommand}"/>
<Button Grid.Row="1" IsVisible="{Binding !IsGenerationRunning}" Foreground="White" VerticalAlignment="Bottom" HorizontalAlignment="Stretch" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Content="Generate" Command="{Binding GenerateCommand}"/>
<Button Grid.Row="1" IsVisible="{Binding IsGenerationRunning}" Foreground="White" VerticalAlignment="Bottom" HorizontalAlignment="Stretch" HorizontalContentAlignment="Center" VerticalContentAlignment="Center" Content="Cancel" Command="{Binding CancelGeneration}"/>
</Grid>
</ScrollViewer>
</Border>