|
1 | 1 | using System; |
| 2 | +using System.Collections.Generic; |
2 | 3 |
|
3 | 4 | public class DelaunayTriangulation |
4 | 5 | { |
5 | | - public static int Compute(int[] arr) |
| 6 | + private struct Point |
6 | 7 | { |
7 | | - int n = arr[0]; |
8 | | - if (n < 3) return 0; |
| 8 | + public int X; |
| 9 | + public int Y; |
9 | 10 |
|
10 | | - double[] px = new double[n], py = new double[n]; |
11 | | - for (int i = 0; i < n; i++) |
| 11 | + public Point(int x, int y) |
12 | 12 | { |
13 | | - px[i] = arr[1 + 2 * i]; |
14 | | - py[i] = arr[1 + 2 * i + 1]; |
| 13 | + X = x; |
| 14 | + Y = y; |
15 | 15 | } |
| 16 | + } |
16 | 17 |
|
17 | | - double EPS = 1e-9; |
18 | | - int count = 0; |
| 18 | + private static long Cross(Point o, Point a, Point b) |
| 19 | + { |
| 20 | + return (long)(a.X - o.X) * (b.Y - o.Y) - (long)(a.Y - o.Y) * (b.X - o.X); |
| 21 | + } |
19 | 22 |
|
20 | | - for (int i = 0; i < n; i++) |
| 23 | + private static int ConvexHullVertexCount(Point[] points) |
| 24 | + { |
| 25 | + if (points.Length <= 1) return points.Length; |
| 26 | + |
| 27 | + Array.Sort(points, (a, b) => |
21 | 28 | { |
22 | | - for (int j = i + 1; j < n; j++) |
| 29 | + int cmp = a.X.CompareTo(b.X); |
| 30 | + return cmp != 0 ? cmp : a.Y.CompareTo(b.Y); |
| 31 | + }); |
| 32 | + |
| 33 | + var hull = new List<Point>(); |
| 34 | + |
| 35 | + foreach (Point p in points) |
| 36 | + { |
| 37 | + while (hull.Count >= 2 && Cross(hull[hull.Count - 2], hull[hull.Count - 1], p) <= 0) |
23 | 38 | { |
24 | | - for (int k = j + 1; k < n; k++) |
25 | | - { |
26 | | - double ax = px[i], ay = py[i]; |
27 | | - double bx = px[j], by = py[j]; |
28 | | - double cx = px[k], cy = py[k]; |
29 | | - |
30 | | - double d = 2.0 * (ax * (by - cy) + bx * (cy - ay) + cx * (ay - by)); |
31 | | - if (Math.Abs(d) < EPS) continue; |
32 | | - |
33 | | - double ux = ((ax*ax + ay*ay) * (by - cy) + |
34 | | - (bx*bx + by*by) * (cy - ay) + |
35 | | - (cx*cx + cy*cy) * (ay - by)) / d; |
36 | | - double uy = ((ax*ax + ay*ay) * (cx - bx) + |
37 | | - (bx*bx + by*by) * (ax - cx) + |
38 | | - (cx*cx + cy*cy) * (bx - ax)) / d; |
39 | | - |
40 | | - double rSq = (ux - ax) * (ux - ax) + (uy - ay) * (uy - ay); |
41 | | - |
42 | | - bool valid = true; |
43 | | - for (int m = 0; m < n; m++) |
44 | | - { |
45 | | - if (m == i || m == j || m == k) continue; |
46 | | - double distSq = (ux - px[m]) * (ux - px[m]) + (uy - py[m]) * (uy - py[m]); |
47 | | - if (distSq < rSq - EPS) { valid = false; break; } |
48 | | - } |
49 | | - |
50 | | - if (valid) count++; |
51 | | - } |
| 39 | + hull.RemoveAt(hull.Count - 1); |
52 | 40 | } |
| 41 | + hull.Add(p); |
| 42 | + } |
| 43 | + |
| 44 | + int lowerCount = hull.Count; |
| 45 | + for (int i = points.Length - 2; i >= 0; i--) |
| 46 | + { |
| 47 | + Point p = points[i]; |
| 48 | + while (hull.Count > lowerCount && Cross(hull[hull.Count - 2], hull[hull.Count - 1], p) <= 0) |
| 49 | + { |
| 50 | + hull.RemoveAt(hull.Count - 1); |
| 51 | + } |
| 52 | + hull.Add(p); |
| 53 | + } |
| 54 | + |
| 55 | + return Math.Max(0, hull.Count - 1); |
| 56 | + } |
| 57 | + |
| 58 | + public static int Compute(int[] arr) |
| 59 | + { |
| 60 | + int n = arr[0]; |
| 61 | + if (n < 3) return 0; |
| 62 | + |
| 63 | + Point[] points = new Point[n]; |
| 64 | + for (int i = 0; i < n; i++) |
| 65 | + { |
| 66 | + points[i] = new Point(arr[1 + 2 * i], arr[1 + 2 * i + 1]); |
53 | 67 | } |
54 | 68 |
|
55 | | - return count; |
| 69 | + int hullVertices = ConvexHullVertexCount(points); |
| 70 | + return (2 * n) - 2 - hullVertices; |
56 | 71 | } |
57 | 72 | } |
0 commit comments