unsigned max-intersects(p, S) mx = 0 for p2 in S if p ≠ p2 mx = max(mx, intersects(p, p2, S)) return mx unsigned intersects(p, p2, S) cnt = 0 for p3 in S if colinear(p1, p2, p3) cnt++ return cnt
max-intersects()
doesn't return the answer if p
isn't in a subset.
max-intersects()
over every point in S.unsigned max-intersects(S) mx = 0 for each p in S mx = max(mx, max-intersects(p, S) return mx
unsigned max_intersects(p, S) slopes = get-slopes(p, S) sort(slopes) return max-group-size(slopes)
slope [] get-slopes(p, S) for p2 in S if p ≠ p2 delta-x = p.x - p2.x delta-y = p.y - p2.y g = gcd(delta-x, delta-y) slopes.append( (delta-x/g, delta-y/g)) return slopes
bool < (slope s1, slope s2) return s1.x*s2.y < s1.y*s2.x bool < (slope s1, slope s2) return s1.x < s2.x ∨ s1.x = s2.x ∧ s1.y < s2.y
unsigned max-group-size(slopes) mx = 0 n = slopes.size i = 0 while i < n - mx while i + mx < n ∧ slopes[i] = slopes[i + mx] mx++ i++ return mx
intersects()
is O(n); max_intersects()
is O(n2).unsigned max-intersects(p, S) mx = 0 for p2 in S if p ≠ p2 mx = max(mx, intersects(p, p2, S)) return mx unsigned intersects(p, p2, S) cnt = 0 for p3 in S if colinear(p1, p2, p3) cnt++ return cnt
max-intersects()
is O(n3).unsigned max-intersects(S) mx = 0 for each p in S mx = max(mx, max-intersects(p, S) return mx
get-slopes()
is O(n).slope [] get-slopes(p, S) for p2 in S if p ≠ p2 delta-x = p.x - p2.x delta-y = p.y - p2.y g = gcd(delta-x, delta-y) slopes.append( (delta-x/g, delta-y/g)) return slopes
max-group-size()
is O(n).unsigned max-group-size(slopes) mx = 0 n = slopes.size i = 0 while i < n - mx while i + mx < n ∧ slopes[i] = slopes[i + mx] mx++ i++ return mx
max-intersects()
is O(n log n).unsigned max-intersects(p, S) slopes = get-slopes(p, S) sort(slopes) return max-group-size(slopes) unsigned max-intersects(p, S) O(n) O(n log n) O(n)
max-intersects()
is O(n2 log n).unsigned max-intersects(S) mx = 0 for each p in S mx = max(mx, max-intersects(p, S) return mx