From 664d0e67ac1d0b97c81fdf5d85149f612a51c684 Mon Sep 17 00:00:00 2001 From: Skyler Lehmkuhl Date: Thu, 28 Nov 2013 02:52:13 -0500 Subject: [PATCH] Paint bucketing shapes mostly working --- misc_funcs.py | 25 ++++++++++++ svlgui.py | 109 ++++++++++++++++++++++++++++++++++++++++++++------ 2 files changed, 122 insertions(+), 12 deletions(-) diff --git a/misc_funcs.py b/misc_funcs.py index fa301ce..5cf511f 100644 --- a/misc_funcs.py +++ b/misc_funcs.py @@ -10,6 +10,7 @@ import math import subprocess import re import os +import sys def select_any(self): svlgui.MODE = " " @@ -266,3 +267,27 @@ def pairwise(iterable): a, b = tee(iterable) next(b, None) return izip(a, b) + +def hittest(linelist,x,y): + hits = False + def IsOnLeft(a, b, c): + return Area2(a, b, c) > 0 + def IsOnRight(a, b, c): + return Area2(a, b, c) < 0 + def IsCollinear(a, b, c): + return Area2(a, b, c) == 0 + # calculates the triangle's size (formed by the "anchor" segment and additional point) + def Area2(a, b, c): + return (b[0]-a[0])*(c[1]-a[1])-(c[0]-a[0])*(b[1]-a[1]) + def intersects(a,b,c,d): + return not (IsOnLeft(a,b,c) != IsOnRight(a,b,d)) + def ccw(a,b,c): + return (c[1]-a[1])*(b[0]-a[0]) > (b[1]-a[1])*(c[0]-a[0]) + def intersect(a,b,c,d): + return ccw(a,c,d) != ccw(b,c,d) and ccw(a,b,c) != ccw(a,b,d) + for i in xrange(len(linelist)): + hits = hits != intersect([linelist[i-1].endpoint1.x,linelist[i-1].endpoint1.y], + [linelist[i].endpoint1.x,linelist[i].endpoint1.y],[x,y],[x,sys.maxint]) + print hits, x, y + return hits + diff --git a/svlgui.py b/svlgui.py index 64d15d8..0612ea7 100644 --- a/svlgui.py +++ b/svlgui.py @@ -2631,6 +2631,8 @@ class Group (object): cr.pencolor = Color([0,0,1]).pygui cr.stroke_rect([sorted([self.startx,self.cx])[0], sorted([self.starty,self.cy])[0], \ sorted([self.startx,self.cx])[1], sorted([self.starty,self.cy])[1]]) + for i in self.fills: + i.draw(cr, rect=rect) for i in self.lines: i.draw(cr, rect=rect) def add(self, *args): @@ -2720,17 +2722,15 @@ class Group (object): return elif MODE=="b": nlines = [i for i in self.lines] - endsleft = True - while endsleft: - endsleft = False - print nlines - for i in reversed(nlines): - if not (i.endpoint1 in [j.endpoint1 for j in nlines if not j==i]+[j.endpoint2 for j in nlines if not j==i]): - nlines.remove(i) - endsleft = True - elif not (i.endpoint2 in [j.endpoint1 for j in nlines if not j==i]+[j.endpoint2 for j in nlines if not j==i]): - nlines.remove(i) - endsleft = True + # First, remove all line segments that have at least one free endpoit, not coincident with any other segment. + # Do that repeatedly until no such segment remains. + for i in reversed(nlines): + if not (i.endpoint1 in [j.endpoint1 for j in nlines if not j==i]+[j.endpoint2 for j in nlines if not j==i]): + nlines.remove(i) + elif not (i.endpoint2 in [j.endpoint1 for j in nlines if not j==i]+[j.endpoint2 for j in nlines if not j==i]): + nlines.remove(i) + + # Find the closest segment to the point. if nlines: mindist = sys.maxint point = Point(x, y) @@ -2740,6 +2740,71 @@ class Group (object): if d