参考
CGALDotNet快速开始:https://blog.csdn.net/liqian_ken/article/details/138274933
CGAL二维可视域计算介绍:https://doc.cgal.org/latest/Visibility_2/index.html#visibility_2_introduction
CGAL相关接口:https://doc.cgal.org/latest/Visibility_2/classCGAL_1_1Triangular__expansion__visibility__2.html
示例
已知多边形ABCDEFGHIJ,多边形内有一点视点K,下面要计算K点能看到的区域。
代码:
using CGALDotNet;
using CGALDotNet.Polygons;
using CGALDotNetGeometry.Numerics;
using System;
using System.Collections.Generic;
using System.Linq;namespace CTestCore31
{public class Program{static void Main(string[] args){var points = new Point2d[]{new Point2d(0, 0),new Point2d(0, 1),new Point2d(3, 1),new Point2d(3, 2),new Point2d(4, 2),new Point2d(4, 1),new Point2d(8, 1),new Point2d(8, -1),new Point2d(5, -1),new Point2d(4, 0),};points = points.Reverse().ToArray();var poly = new Polygon2<EEK>(points);var pv = new PolygonVisibility<EEK>();var pt = new Point2d(0.5, 0.5);pv.ComputeVisibility(pt, poly, out Polygon2<EEK> result);List<Point2d> resultPoints = new List<Point2d>();result.GetPoints(resultPoints);foreach (Point2d point in resultPoints){Console.WriteLine($"{point.x} {point.y}");}Console.ReadLine();}}
}
由此获得可视域多边形,其坐标:
3 1
0 1
0 0
4 0
8 -0.5714285714285714
8 1
4 1
4 1.2000000000000002
根据需要将其绘制出来,如下图所示:
将上面两张图叠加(绿色区域为视点K的可视域):
注意
视点必须位于多边形内部,不能位于外部(计算结果为null)或多边形边界上(报错)。