rect.py :  » Mobile » Python-for-PalmOS » Python-1.5.2+reduced-1.0 » Lib » lib-stdwin » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Mobile » Python for PalmOS 
Python for PalmOS » Python 1.5.2 reduced 1.0 » Lib » lib stdwin » rect.py
# Module 'rect'.
#
# Operations on rectangles.
# There is some normalization: all results return the object 'empty'
# if their result would contain no points.


# Exception.
#
error = 'rect.error'


# The empty rectangle.
#
empty = (0, 0), (0, 0)


# Check if a rectangle is empty.
#
def is_empty(r):
  (left, top), (right, bottom) = r
  return left >= right or top >= bottom


# Compute the intersection or two or more rectangles.
# This works with a list or tuple argument.
#
def intersect(list):
  if not list: raise error, 'intersect called with empty list'
  if is_empty(list[0]): return empty
  (left, top), (right, bottom) = list[0]
  for rect in list[1:]:
    if is_empty(rect):
      return empty
    (l, t), (r, b) = rect
    if left < l: left = l
    if top < t: top = t
    if right > r: right = r
    if bottom > b: bottom = b
    if is_empty(((left, top), (right, bottom))):
      return empty
  return (left, top), (right, bottom)


# Compute the smallest rectangle containing all given rectangles.
# This works with a list or tuple argument.
#
def union(list):
  (left, top), (right, bottom) = list[0]
  for (l, t), (r, b) in list[1:]:
    if not is_empty(((l, t), (r, b))):
      if l < left: left = l
      if t < top: top = t
      if r > right: right = r
      if b > bottom: bottom = b
  res = (left, top), (right, bottom)
  if is_empty(res):
    return empty
  return res


# Check if a point is in a rectangle.
#
def pointinrect((h, v), ((left, top), (right, bottom))):
  return left <= h < right and top <= v < bottom


# Return a rectangle that is dh, dv inside another
#
def inset(((left, top), (right, bottom)), (dh, dv)):
  left = left + dh
  top = top + dv
  right = right - dh
  bottom = bottom - dv
  r = (left, top), (right, bottom)
  if is_empty(r):
    return empty
  else:
    return r


# Conversions between rectangles and 'geometry tuples',
# given as origin (h, v) and dimensions (width, height).
#
def rect2geom((left, top), (right, bottom)):
  return (left, top), (right-left, bottom-top)

def geom2rect((h, v), (width, height)):
  return (h, v), (h+width, v+height)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.