def shaped_like_a_side(side_data): """Determines if the given side data is actually shaped like a side. Sides may be flat, out-shaped or in-shaped (the latter two being indistinguishable when looking at just a single shape). Arguments: side_data: a list of (x, y) tuples Returns a boolean. """ fixtures_path = 'quandry/fixtures/' piece_six_path = os.path.join(fixtures_path, 'piece-six.json') with open(piece_six_path) as piece_six_file: canonical_out_side = json.loads(piece_six_file.read())['sides'][1] out_score = util.hausdorff(side_data, canonical_out_side) print 'out score: %s' % out_score if out_score < 10: #return True pass piece_three_path = os.path.join(fixtures_path, 'piece-three.json') with open(piece_three_path) as piece_three_file: canonical_flat_side = json.loads(piece_three_file.read())['sides'][0] flat_score = util.hausdorff(side_data, canonical_flat_side) print 'flat score: %s' % flat_score if flat_score < 10: #return True pass
for filepath in piece_data: for index, side in enumerate(piece_data[filepath]['sides']): key = '%s+%s' % (filepath, index) sides[key] = { 'name': key, 'type': piece_data[filepath]['side_types'][index], 'length': piece_data[filepath]['side_lengths'][index], 'outline': piece_data[filepath]['sides'][index], } ins = [sides[k] for k in sides if sides[k]['type'] == 'in'] outs = [sides[k] for k in sides if sides[k]['type'] == 'out'] # Compare ins and outs. for in_side in ins: print 'analyzing "%s"..' % in_side['name'] side_length_ratios = [] for out_side in outs: if in_side['name'].split('+')[0] == out_side['name'].split('+')[0]: continue diff = util.percent_diff(in_side['length'], out_side['length']) side_length_ratios.append((out_side['name'], diff)) hausdorff_scores = [] for name, ratio in sorted(side_length_ratios, key=lambda v: v[1]): if ratio > 10: continue h_score = util.hausdorff(in_side['outline'], sides[name]['outline']) hausdorff_scores.append((name, h_score)) for h in sorted(hausdorff_scores, key=lambda v: v[1]): print '%10s -> %0.2f' % (h[0].split('/')[1], h[1])