diff --git a/h2/mbc.py b/h2/mbc.py index b0758a6..18cd630 100644 --- a/h2/mbc.py +++ b/h2/mbc.py @@ -1,11 +1,7 @@ -import random import statistics from math import inf from typing import Set, List, Tuple -from scipy.optimize import linprog - -import util from util import Side, Point, gen_point, display @@ -46,10 +42,12 @@ def solve_2dlp(c: Tuple[float, float], constraints: List[Tuple[Tuple[float, floa :return: x1, x2 """ c1, c2 = c - x1, x2 = (-inf, -inf) if c1 > 0 else (inf, inf) + x1 = -inf if c1 > 0 else inf + x2 = -inf if c2 > 0 else inf + #random.shuffle(constraints) - for i, ((a1, a2), b) in enumerate(constraints[1:], start=0): + for i, ((a1, a2), b) in enumerate(constraints[1:]): if not a1*x1 + a2*x2 <= b: x1 = solve_1dlp(c1 - c2*a1/a2, [(ai1 - ai2*a1 / a2, bi - ai2*b / a2) for (ai1, ai2), bi in constraints[:i]]) @@ -70,8 +68,8 @@ def mbc_ch(points: Set[Point], flipper: callable) -> Set[Point]: pr = {p for p in points if p.x >= med_x} # Find the bridge over the vertical line in pm - slope, intercept = solve_2dlp((flipper(med_x), flipper(1)), - [((flipper(-p.x), flipper(-1)), flipper(-p.y)) for p in points]) + slope, intercept = solve_2dlp((flipper(med_x), flipper(1)), # confirmed correct + [((flipper(-p.x), flipper(-1)), flipper(-p.y)) for p in points]) # confirmed correct # Find the two points which are on the line, should work on = {p for p in points if sidedness(slope, intercept, p, flipper) == Side.ON} @@ -79,8 +77,8 @@ def mbc_ch(points: Set[Point], flipper: callable) -> Set[Point]: right_point = max(on) # Prune the points between the two line points - pl = {p for p in points if p.x <= left_point.x} - pr = {p for p in points if p.x >= right_point.x} + pl = {p for p in pl if p.x <= left_point.x} + pr = {p for p in pr if p.x >= right_point.x} return set.union(mbc_ch(pl, flipper), {left_point, right_point}, mbc_ch(pr, flipper)) diff --git a/h2/tmptest.py b/h2/tmptest.py index e75124c..6c473ad 100644 --- a/h2/tmptest.py +++ b/h2/tmptest.py @@ -7,11 +7,11 @@ from graham import graham_scan from mbc import mbc from quick_hull import quick_hull -#random.seed(1337_420) +random.seed(1337_420) while True: print("="*100) - points = {util.gen_point(-100, 100) for i in range(100)} + points = {util.gen_point(-100, 100) for i in range(5)} print("Points:", sorted(points)) # Sanity check