Added new median calculation. Should fix last issues

This commit is contained in:
Alexander Munch-Hansen 2018-10-17 12:09:24 +02:00
parent 05f0aa5551
commit a96c03fa9e

View File

@ -6,7 +6,7 @@ from scipy.optimize import linprog
import scipy import scipy
import random import random
from util import Side, Point, gen_point, display, display_line_only, gen_circular_point, gen_weird_point from util import Side, Point, gen_point, display, display_line_only, gen_circular_point, gen_weird_point, gen_triangular_point
def sidedness(slope: float, intersection: float, p3: Point, flipper: callable, eps=0.0000001) -> Side: def sidedness(slope: float, intersection: float, p3: Point, flipper: callable, eps=0.0000001) -> Side:
@ -54,13 +54,13 @@ assert solve_1dlp(1, [(-1, 3), (-1, 2)]) == -2
def new_solve_1dlp(c, constraints, idx): def new_solve_1dlp(c, constraints, idx):
c1, c2 = c c1, c2 = c
((a1, a2), b) = constraints[idx] ((a1, a2), b) = constraints[-1]
q, p = b / a2, a1 /a2 q, p = b / a2, a1 /a2
interval = [-9999999, 9999999] interval = [-9999999, 9999999]
for new_idx, ((lel_a1, lel_a2), lel_b) in enumerate(constraints): for new_idx, ((lel_a1, lel_a2), lel_b) in enumerate(constraints):
if not new_idx == idx:
bj, aj = (lel_b - lel_a2 * q), (lel_a1 - lel_a2 * p) bj, aj = (lel_b - lel_a2 * q), (lel_a1 - lel_a2 * p)
if aj < 0 and bj / aj > interval[0]: if aj < 0 and bj / aj > interval[0]:
interval[0] = bj / aj interval[0] = bj / aj
@ -154,7 +154,7 @@ def mbc_ch(points: Set[Point], flipper: callable) -> Set[Point]:
# Find the point with median x-coordinate, and partition the points on this point # Find the point with median x-coordinate, and partition the points on this point
med_x = find_median(points) med_x = find_median(points)
med_x = statistics.median(p.x for p in points) #med_x = statistics.median(p.x for p in points)
#print(med_x) #print(med_x)
# Find left and right points in regards to median # Find left and right points in regards to median
@ -190,16 +190,15 @@ def mbc_ch(points: Set[Point], flipper: callable) -> Set[Point]:
#left_point = min(on) #left_point = min(on)
#right_point = max(on) #right_point = max(on)
dist_to_line = lambda p: abs(intercept + slope * p.x - p.y)/sqrt(1 + slope**2)
#dist_to_line = lambda p: abs(intercept + slope * p.x - p.y)/sqrt(1 + slope**2) left_point = min(pl, key = dist_to_line)
#left_point = min(pl, key = dist_to_line) right_point = min(pr, key=dist_to_line)
#right_point = min(pr, key=dist_to_line)
#display_line_only(points, slope, intercept, [left_point, right_point]) #display_line_only(points, slope, intercept, [left_point, right_point])
left_point = next(p for p in pl if sidedness(slope, intercept, p, flipper) == Side.ON) #left_point = next(p for p in pl if sidedness(slope, intercept, p, flipper) == Side.ON)
right_point = next(p for p in pr if sidedness(slope, intercept, p, flipper) == Side.ON) #right_point = next(p for p in pr if sidedness(slope, intercept, p, flipper) == Side.ON)
@ -226,12 +225,14 @@ def mbc(points: Set[Point]) -> Set[Point]:
if __name__ == '__main__': if __name__ == '__main__':
# points = {gen_circular_point(1, 100, 50) for _ in range(200)} random.seed(1337_420)
points = {gen_weird_point(1, 100) for _ in range(200)} points = {gen_point(0, 20) for _ in range(20)}
#points = {gen_circular_point(1, 100, 50) for _ in range(200)}
#points = {gen_triangular_point(Point(1,1), Point(51,1), Point(26, 30)) for _ in range(200)}
#points = {Point(x=-33.11091053638924, y=38.88967778961347), Point(x=61.20269947424177, y=-78.96305419217254), Point(x=99.44053842147957, y=-89.11579172297581), Point(x=-92.40380889537532, y=84.33904351991652), Point(x=-90.63139185545595, y=-91.13793846505985)} #points = {Point(x=-33.11091053638924, y=38.88967778961347), Point(x=61.20269947424177, y=-78.96305419217254), Point(x=99.44053842147957, y=-89.11579172297581), Point(x=-92.40380889537532, y=84.33904351991652), Point(x=-90.63139185545595, y=-91.13793846505985)}
points = {Point(x=5.2, y=9.7), Point(x=5.3, y=4.9), Point(x=3.3, y=3.6), Point(x=9.2, y=4.8), Point(x=9.7, y=5.7), Point(x=5.6, y=8.7)} #points = {Point(x=5.2, y=9.7), Point(x=5.3, y=4.9), Point(x=3.3, y=3.6), Point(x=9.2, y=4.8), Point(x=9.7, y=5.7), Point(x=5.6, y=8.7)}
upper_hull_points = mbc_ch(points, lambda x: x) upper_hull_points = mbc_ch(points, lambda x: x)
lower_hull_points = mbc_ch(points, lambda x: -x) lower_hull_points = mbc_ch(points, lambda x: -x)