#!/usr/local/bin/python3 # Author: Dr. Robert Heckendorn, Computer Science Department, University of Idaho, 2013 # # This finds the distance between the closest set of points # in a set of points. This is done by divide and conquer to get # an O(n log(n)) running time from random import * from math import * ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## ## # simple cp # NOTE: this is the SQUARE of distance between two points def dist(p1, p2) : return (p1[0] - p2[0])*(p1[0] - p2[0]) + (p1[1] - p2[1])*(p1[1] - p2[1]) # for a small number of points find the minimum dist # This is O(len(pts)^2) def simplecp(pts, start, end) : # WARNING: assumes there are at least 2 points dmin = dist(pts[start], pts[start+1]) for i in range(start, end) : for j in range(i+1, end) : newdist = dist(pts[i], pts[j]) if newdist 1 : s = partitionaux(pts, left, right, compare) sortaux(pts, left, s, compare) sortaux(pts, s+1, right, compare) # split ypts into two halves around the point pivot # each half is SELECTED BY X < PIVOT POINT # the points in each half are SORTED BY Y # This is O(len(ypts)) def splitsortYX(ypts, pivot) : left = [] right = [] for i in range(0, len(ypts)) : # if ypts[i][0]