序
源码
代码基于C#&&Unity
using System.Collections.Generic;
using UnityEngine;
public class UVertex2D
{
//> id < 0 表示超级三角形的顶点,否则表示正常的顶点
public int Id;
//> 按照水平轴进行排序,SortBy = Point.x
public float SortBy;
//> 顶点
public Vector2 Point;
public UVertex2D(int _id, Vector2 _point)
{
Id = _id;
Point = _point;
SortBy = _point.x;
}
}
public class UEdge2D
{
//> 一条边的两个顶点
public UVertex2D[] Points { get; private set; }
//> 线段的长度
public float Distance { get; private set; }
//> 这条边的全局唯一名称
public string FromName { get; private set; }
public string ToName { get; private set; }
public UEdge2D(UVertex2D _from, UVertex2D _to)
{
Points = new UVertex2D[2];
Points[0] = _from;
Points[1] = _to;
Distance = Vector2.Distance(Points[0].Point, Points[1].Point);
FromName = _from.Id + "_" + _to.Id;
ToName = _to.Id + "_" + _from.Id;
}
//> 检测两条边使用的顶点是否相同,如果是那么这两条边是共享边
public bool IsEquals(UEdge2D _other)
{
return _other.ContainId(Points[0].Id) && _other.ContainId(Points[1].Id);
}
bool ContainId(int _vertexid)
{
return Points[0].Id == _vertexid || Points[1].Id == _vertexid;
}
}
public class UTriangle2D
{
//> 三角形顶点集合
public UVertex2D[] Points = new UVertex2D[3];
//> 三角形边集合
public UEdge2D[] Edges = new UEdge2D[3];
//> 最长