|
| 1 | +from .utils import * |
| 2 | +from .consts import * |
| 3 | +import random |
| 4 | +import math |
| 5 | + |
| 6 | +class Polygon: |
| 7 | + def __init__(self,points=[]): |
| 8 | + if not list_like(points): |
| 9 | + raise Exception("polygon must be constructed by a list of points") |
| 10 | + self.points = points |
| 11 | + |
| 12 | + def __str__(self): |
| 13 | + buf = [] |
| 14 | + for point in self.points: |
| 15 | + buf.append(str(point[0]) + " " + str(point[1])) |
| 16 | + return '\n'.join(buf) |
| 17 | + |
| 18 | + def perimeter(self): |
| 19 | + ans = 0 |
| 20 | + for i in range(0, len(self.points)): |
| 21 | + a = self.points[i] |
| 22 | + b = self.points[(i + 1) % len(self.points)] |
| 23 | + ans = ans + math.sqrt((a[0] - b[0]) * (a[0] - b[0]) + |
| 24 | + (a[1] - b[1]) * (a[1] - b[1])) |
| 25 | + return ans |
| 26 | + |
| 27 | + def area(self): |
| 28 | + ans = 0 |
| 29 | + for i in range(0, len(self.points)): |
| 30 | + a = self.points[i] |
| 31 | + b = self.points[(i + 1) % len(self.points)] |
| 32 | + ans = ans + a[0] * b[1] - a[1] * b[0] |
| 33 | + if ans < 0: |
| 34 | + ans = -ans |
| 35 | + ans = ans / 2 |
| 36 | + return ans |
| 37 | + |
| 38 | + #generate a convex hull with n points |
| 39 | + #it's possible to have even edges |
| 40 | + @staticmethod |
| 41 | + def convex_hull(n, **kwargs): |
| 42 | + # fx, fy are functions which map [0,1] to int or float |
| 43 | + fx = kwargs.get("fx", lambda x: x) |
| 44 | + fy = kwargs.get("fy", lambda x: x) |
| 45 | + sz = n * 2 |
| 46 | + result = [] |
| 47 | + while len(result) < n: |
| 48 | + points = [] |
| 49 | + # about 10 points will be randomized |
| 50 | + randomize_prob = int(sz / 10) + 1 |
| 51 | + if randomize_prob < 10: |
| 52 | + randomize_prob = 10 |
| 53 | + for i in range(0, sz): |
| 54 | + angle = random.uniform(0, 2 * PI) |
| 55 | + x = 0.5 + math.cos(angle) * 0.48 |
| 56 | + y = 0.5 + math.sin(angle) * 0.48 |
| 57 | + if random.randint(0, randomize_prob) == 0: |
| 58 | + x = x + random.uniform(-0.005, 0.005) |
| 59 | + y = y + random.uniform(-0.005, 0.005) |
| 60 | + points.append([fx(x), fy(y)]) |
| 61 | + # compute convex hull for points and store in rst |
| 62 | + points = sorted(points) |
| 63 | + # unique |
| 64 | + tmp = [] |
| 65 | + for i in range(0, len(points)): |
| 66 | + if i == 0 or points[i - 1] != points[i]: |
| 67 | + tmp.append(points[i]) |
| 68 | + points = tmp |
| 69 | + st = [] # stack |
| 70 | + for i in range(0, len(points)): |
| 71 | + while len(st) >= 2: |
| 72 | + a = st[len(st) - 1] |
| 73 | + b = points[i] |
| 74 | + o = st[len(st) - 2] |
| 75 | + if (a[0] - o[0]) * (b[1] - o[1]) - \ |
| 76 | + (a[1] - o[1]) * (b[0] - o[0]) >= 0: |
| 77 | + break |
| 78 | + st.pop() |
| 79 | + st.append(points[i]) |
| 80 | + g = len(st) + 1 |
| 81 | + for i in range(0, len(points) - 1)[::-1]: |
| 82 | + while len(st) >= g: |
| 83 | + a = st[len(st) - 1] |
| 84 | + b = points[i] |
| 85 | + o = st[len(st) - 2] |
| 86 | + if (a[0] - o[0]) * (b[1] - o[1]) - \ |
| 87 | + (a[1] - o[1]) * (b[0] - o[0]) >= 0: |
| 88 | + break |
| 89 | + st.pop() |
| 90 | + st.append(points[i]) |
| 91 | + st.pop() |
| 92 | + result = st |
| 93 | + sz = int(sz * 1.7) + 3 # if failed, increase size and try again |
| 94 | + ids = [i for i in range(0, len(result))] |
| 95 | + random.shuffle(ids) |
| 96 | + ids = ids[0:n] |
| 97 | + ids = sorted(ids) |
| 98 | + output = [result[ids[i]] for i in range(0, n)] |
| 99 | + poly = Polygon(output) |
| 100 | + return poly |
| 101 | + |
| 102 | + # find a path from points[0] to points[1] and cross all points in [points] |
| 103 | + @staticmethod |
| 104 | + def __conquer(points): |
| 105 | + if len(points) <= 2: |
| 106 | + return points |
| 107 | + if len(points) == 3: |
| 108 | + (points[1],points[2])=(points[2],points[1]) |
| 109 | + return points |
| 110 | + divide_id = random.randint(2, len(points) - 1) |
| 111 | + divide_point1 = points[divide_id] |
| 112 | + divide_k = random.uniform(0.01, 0.99) |
| 113 | + divide_point2 = [divide_k * (points[1][0] - points[0][0]) + points[0][0], |
| 114 | + divide_k * (points[1][1] - points[0][1]) + points[0][1]] |
| 115 | + # path: points[0]->points[divide]->points[1] |
| 116 | + # dividing line in the form Ax+By+C=0 |
| 117 | + divide_line = [divide_point2[1] - divide_point1[1], |
| 118 | + divide_point1[0] - divide_point2[0], |
| 119 | + -divide_point1[0] * divide_point2[1] |
| 120 | + + divide_point1[1] * divide_point2[0]] |
| 121 | + p0 = (divide_line[0] * points[0][0] + divide_line[1] * points[0][1] + divide_line[2] >= 0) |
| 122 | + p1 = (divide_line[0] * points[1][0] + divide_line[1] * points[1][1] + divide_line[2] >= 0) |
| 123 | + if p0 == p1: # the divide point isn't good enough... |
| 124 | + return Polygon.__conquer(points) |
| 125 | + s = [[], []] |
| 126 | + s[p0].append(points[0]) |
| 127 | + s[p0].append(divide_point1) |
| 128 | + s[not p0].append(divide_point1) |
| 129 | + s[not p0].append(points[1]) |
| 130 | + for i in range(2, len(points)): |
| 131 | + if i == divide_id: |
| 132 | + continue |
| 133 | + pt = (divide_line[0] * points[i][0] + divide_line[1] * points[i][1] + divide_line[2] >= 0) |
| 134 | + s[pt].append(points[i]) |
| 135 | + pa = Polygon.__conquer(s[p0]) |
| 136 | + pb = Polygon.__conquer(s[not p0]) |
| 137 | + pb.pop(0) |
| 138 | + return pa + pb |
| 139 | + |
| 140 | + # generate simple polygon from given points (int[2] or float[2]) |
| 141 | + # O(nlogn)~O(n^2) |
| 142 | + @staticmethod |
| 143 | + def simple_polygon(points): |
| 144 | + if not list_like(points): |
| 145 | + raise Exception("source point is not a list") |
| 146 | + random.shuffle(points) |
| 147 | + if len(points) <= 3: |
| 148 | + return points |
| 149 | + # divide by points[0], points[1] |
| 150 | + divide_line = [points[1][1] - points[0][1], |
| 151 | + points[0][0] - points[1][0], |
| 152 | + -points[0][0] * points[1][1] |
| 153 | + + points[0][1] * points[1][0]] |
| 154 | + s = [[], []] |
| 155 | + s[0].append(points[0]) |
| 156 | + s[0].append(points[1]) |
| 157 | + s[1].append(points[1]) |
| 158 | + s[1].append(points[0]) |
| 159 | + for i in range(2, len(points)): |
| 160 | + pt = (divide_line[0] * points[i][0] + divide_line[1] * points[i][1] + divide_line[2] >= 0) |
| 161 | + s[pt].append(points[i]) |
| 162 | + pa = Polygon.__conquer(s[0]) |
| 163 | + pb = Polygon.__conquer(s[1]) |
| 164 | + pa.pop(0) |
| 165 | + pb.pop(0) |
| 166 | + poly = Polygon(pa + pb) |
| 167 | + return poly |
0 commit comments