116 lines
3.5 KiB
C#
116 lines
3.5 KiB
C#
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using Avalonia.Media;
|
|
using CommunityToolkit.Mvvm.Input;
|
|
|
|
namespace DelaunayTriangulation.Models;
|
|
|
|
public static class Geometries
|
|
{
|
|
public sealed class Point(double x, double y, double r = 3, Color? color = null)
|
|
{
|
|
public double X { get; } = x;
|
|
public double Y { get; } = y;
|
|
public double R { get; } = r;
|
|
|
|
public Color Color { get; set; } = color ?? new Color(255, 255, 255, 255);
|
|
|
|
public double Left => X - R;
|
|
public double Top => Y - R;
|
|
public double Size => 2 * R;
|
|
}
|
|
|
|
public sealed class PointComparer : IComparer<Point>
|
|
{
|
|
public static readonly PointComparer Instance = new();
|
|
|
|
public int Compare(Point? a, Point? b)
|
|
{
|
|
if (ReferenceEquals(a, b)) return 0;
|
|
if (a is null) return -1;
|
|
if (b is null) return 1;
|
|
|
|
int c = a.X.CompareTo(b.X);
|
|
if (c != 0) return c;
|
|
|
|
c = a.Y.CompareTo(b.Y);
|
|
if (c != 0) return c;
|
|
|
|
return a.R.CompareTo(b.R);
|
|
}
|
|
}
|
|
|
|
public sealed class Edge(double x1, double y1, double x2, double y2)
|
|
{
|
|
public double X1 { get; } = x1;
|
|
public double X2 { get; } = x2;
|
|
public double Y1 { get; } = y1;
|
|
public double Y2 { get; } = y2;
|
|
|
|
public Avalonia.Point Start => new(X1, Y1);
|
|
public Avalonia.Point End => new(X2, Y2);
|
|
|
|
private static bool Almost(double a, double b, double eps = 1e-9) => Math.Abs(a - b) <= eps;
|
|
|
|
public bool EqualsUndirected(Edge other, double eps = 1e-9)
|
|
{
|
|
bool same = Almost(X1, other.X1, eps) && Almost(Y1, other.Y1, eps) &&
|
|
Almost(X2, other.X2, eps) && Almost(Y2, other.Y2, eps);
|
|
|
|
bool swapped = Almost(X1, other.X2, eps) && Almost(Y1, other.Y2, eps) &&
|
|
Almost(X2, other.X1, eps) && Almost(Y2, other.Y1, eps);
|
|
|
|
return same || swapped;
|
|
}
|
|
}
|
|
|
|
public sealed class Circle
|
|
{
|
|
public double X { get; }
|
|
public double Y { get; }
|
|
public double R { get; }
|
|
|
|
public double Left => X - R;
|
|
public double Top => Y - R;
|
|
public double Diameter => 2 * R;
|
|
|
|
public Circle(double x, double y, double r)
|
|
{
|
|
X = x;
|
|
Y = y;
|
|
R = r;
|
|
}
|
|
|
|
public Circle(Point p1, Point p2, Point p3)
|
|
{
|
|
double x1 = p1.X, y1 = p1.Y;
|
|
double x2 = p2.X, y2 = p2.Y;
|
|
double x3 = p3.X, y3 = p3.Y;
|
|
|
|
var d = 2.0 * (x1 * (y2 - y3) + x2 * (y3 - y1) + x3 * (y1 - y2));
|
|
|
|
if (Math.Abs(d) < 1e-12) throw new ArgumentException("Points are collinear; circumcircle undefined.");
|
|
|
|
var x1p = x1 * x1 + y1 * y1;
|
|
var x2p = x2 * x2 + y2 * y2;
|
|
var x3p = x3 * x3 + y3 * y3;
|
|
|
|
var ux = (x1p * (y2 - y3) + x2p * (y3 - y1) + x3p * (y1 - y2)) / d;
|
|
var uy = (x1p * (x3 - x2) + x2p * (x1 - x3) + x3p * (x2 - x1)) / d;
|
|
|
|
X = ux;
|
|
Y = uy;
|
|
R = Math.Sqrt((ux - x1) * (ux - x1) + (uy - y1) * (uy - y1));
|
|
}
|
|
}
|
|
|
|
public sealed class Triangle(Point p1, Point p2, Point p3)
|
|
{
|
|
public Avalonia.Point X { get; } = new(p1.X, p1.Y);
|
|
public Avalonia.Point Y { get; } = new(p2.X, p2.Y);
|
|
public Avalonia.Point Z { get; } = new(p3.X, p3.Y);
|
|
|
|
public IReadOnlyList<Avalonia.Point> Vertices => [X, Y, Z];
|
|
}
|
|
} |