Welcome to mirror list, hosted at ThFree Co, Russian Federation.

GConvexHull.cs « UserGuideExamples « Mono.C5 « class « mcs - github.com/mono/mono.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 48675b3bc69104475b7bd065847a0fef4c7ad347 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
 Copyright (c) 2003-2006 Niels Kokholm and Peter Sestoft
 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:
 
 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 SOFTWARE.
*/

// Compile with 
//    csc /r:C5.dll GConvexHull.cs

using System;
using C5;

namespace GConvexHull
{
// Find the convex hull of a point set in the plane

// An implementation of Graham's (1972) point elimination algorithm,
// as modified by Andrew (1979) to find lower and upper hull separately.

// This implementation correctly handle duplicate points, and
// multiple points with the same x-coordinate.

// 1. Sort the points lexicographically by increasing (x,y), thus 
//    finding also a leftmost point L and a rightmost point R.
// 2. Partition the point set into two lists, upper and lower, according as 
//    point is above or below the segment LR.  The upper list begins with 
//    L and ends with R; the lower list begins with R and ends with L.
// 3. Traverse the point lists clockwise, eliminating all but the extreme
//    points (thus eliminating also duplicate points).
// 4. Join the point lists (in clockwise order) in an array, 
//    leaving out L from lower and R from upper.

  public class Convexhull
  {
    public static Point[] ConvexHull(Point[] pts)
    {
      // 1. Sort points lexicographically by increasing (x, y)
      int N = pts.Length;
      Array.Sort(pts);
      Point left = pts[0], right = pts[N - 1];
      // 2. Partition into lower hull and upper hull
      IList<Point> lower = new LinkedList<Point>(),
        upper = new LinkedList<Point>();
      lower.InsertFirst(left); upper.InsertLast(left);
      for (int i = 0; i < N; i++)
      {
        double det = Point.Area2(left, right, pts[i]);
        if (det < 0)
          lower.InsertFirst(pts[i]);
        else if (det > 0)
          upper.InsertLast(pts[i]);
      }
      lower.InsertFirst(right);
      upper.InsertLast(right);
      // 3. Eliminate points not on the hull
      Eliminate(lower);
      Eliminate(upper);
      // 4. Join the lower and upper hull, leaving out lower.Last and upper.Last
      Point[] res = new Point[lower.Count + upper.Count - 2];
      lower[0, lower.Count - 1].CopyTo(res, 0);
      upper[0, upper.Count - 1].CopyTo(res, lower.Count - 1);
      return res;
    }

    // Graham's scan
    public static void Eliminate(IList<Point> lst)
    {
      IList<Point> view = lst.View(0, 0);
      int slide = 0;
      while (view.TrySlide(slide, 3))
        if (Point.Area2(view[0], view[1], view[2]) < 0)   // right turn
          slide = 1;
        else
        {                                                 // left or straight
          view.RemoveAt(1);
          slide = view.Offset != 0 ? -1 : 0;
        }
    }
  }

// ------------------------------------------------------------

// Points in the plane

  public class Point : IComparable<Point>
  {
    private static readonly C5Random rnd = new C5Random(42);

    public readonly double x, y;

    public Point(double x, double y)
    {
      this.x = x; this.y = y;
    }

    public override string ToString()
    {
      return "(" + x + ", " + y + ")";
    }

    public static Point Random(int w, int h)
    {
      return new Point(rnd.Next(w), rnd.Next(h));
    }

    public bool Equals(Point p2)
    {
      return x == p2.x && y == p2.y;
    }

    public int CompareTo(Point p2)
    {
      int major = x.CompareTo(p2.x);
      return major != 0 ? major : y.CompareTo(p2.y);
    }

    // Twice the signed area of the triangle (p0, p1, p2)
    public static double Area2(Point p0, Point p1, Point p2)
    {
      return p0.x * (p1.y - p2.y) + p1.x * (p2.y - p0.y) + p2.x * (p0.y - p1.y);
    }
  }

// ------------------------------------------------------------

  class GConvexHull
  {
    static void Main(String[] args)
    {
      if (args.Length == 1)
      {
        string arg = args[0];
        int N = int.Parse(arg);
        Point[] pts = new Point[N];
        for (int i = 0; i < N; i++)
          pts[i] = Point.Random(500, 500);
        Point[] chpts = Convexhull.ConvexHull(pts);
        Console.WriteLine("Area is " + Area(chpts));
        Print(chpts);
      }
      else
        Console.WriteLine("Usage: GConvexHull <pointcount>\n");
    }

    // The area of a polygon (represented by an array of ordered vertices)
    public static double Area(Point[] pts)
    {
      int N = pts.Length;
      Point origo = new Point(0, 0);
      double area2 = 0;
      for (int i = 0; i < N; i++)
        area2 += Point.Area2(origo, pts[i], pts[(i + 1) % N]);
      return Math.Abs(area2 / 2);
    }

    public static void Print(Point[] pts)
    {
      int N = pts.Length;
      for (int i = 0; i < N; i++)
        Console.WriteLine(pts[i]);
    }
  }
}