comparison tools/area_editor.py @ 449:a99e5e724545

Better convex polygon protection
author Neil Muller <drnlmuller@gmail.com>
date Sat, 07 Sep 2013 15:08:59 +0200
parents 707d06a77f39
children fe6546d07d5e
comparison
equal deleted inserted replaced
448:044b53eb22df 449:a99e5e724545
377 result['name'] = text 377 result['name'] = text
378 elif self.fields[val] == self.poly_field and text: 378 elif self.fields[val] == self.poly_field and text:
379 # Evil, but faster than good 379 # Evil, but faster than good
380 try: 380 try:
381 l = eval(text) 381 l = eval(text)
382 args.append(' - - %s' % l[0])
383 for coord in l[1:]:
384 args.append(' - %s' % coord)
385 except Exception: 382 except Exception:
386 alert("Invalid polygon %s" % text) 383 alert("Invalid polygon %s" % text)
387 self.needs_cleanup = False 384 self.needs_cleanup = False
388 return None 385 return None
389 # Check for convexity 386 # Check for convexity
390 hull = pymunk.util.convex_hull(l) 387 if not pymunk.util.is_convex(l):
391 if hull != l:
392 alert("Invalid polygon %s - not convex" % text) 388 alert("Invalid polygon %s - not convex" % text)
393 return None 389 return None
390 if not pymunk.util.is_clockwise(l):
391 l.reverse()
392 if not pymunk.util.is_clockwise(l):
393 alert("Invalid polygon %s - unable to make clockwise"
394 % text)
395 return None
396 args.append(' - - %s' % l[0])
397 for coord in l[1:]:
398 args.append(' - %s' % coord)
394 else: 399 else:
395 args.append(' - ' + text) 400 args.append(' - ' + text)
396 data = "args:\n" + '\n'.join(args) 401 data = "args:\n" + '\n'.join(args)
397 result['args'] = load_s(data)['args'] 402 result['args'] = load_s(data)['args']
398 return result 403 return result