hier die Klasse:
Code: Alles auswählen
class My_Rect():
def __init__(self, left, top, width, height, xyoffsets=(0, 0)):
self.x_offset = int(xyoffsets[0])
self.y_offset = int(xyoffsets[1])
self._x = float(left) + self.x_offset
self._y = float(top) + self.y_offset
self._left = int(left)
self._top = int(top)
self.width = int(width)
self.height = int(height)
#### x y ####
@property
def x(self):
return self._x
@x.setter
def x(self, value):
self._x = float(value)
self._left = int(self._x) - self.x_offset
print "self._left set!", self._left
@property
def y(self):
return self._y
@y.setter
def y(self, value):
self._y = float(value)
self._top = int(self._y) - self.y_offset
#### centerx centery ####
@property
def centerx(self):
return self._left + self.width / 2
@centerx.setter
def centerx(self, value):
self.left = int(value) - self.width / 2
@property
def centery(self):
return self._top + self.height / 2
@centery.setter
def centery(self, value):
self.top = int(value) - self.height / 2
#### left right bottom top ####
@property
def left(self):
return self._left
@left.setter
def left(self, value):
self._left = int(value)
self._x = float(self._left) + self.x_offset
@property
def top(self):
return self._top
@top.setter
def top(self, value):
self._top = int(value)
self._y = float(self._top) + self.y_offset
@property
def right(self):
return self._left + self.width
@right.setter
def right(self, value):
self.left = int(value) - self.width
@property
def bottom(self):
return self._top + self.height
@bottom.setter
def bottom(self, value):
self.top = int(value) - self.height
### topleft topright bottomleft bottomright ###
@property
def topleft(self):
return (self.left, self.top)
@topleft.setter
def topleft(self, pos):
x = int(pos[0])
y = int(pos[1])
self.left = x
self.top = y
@property
def topright(self):
return (self.right, self.top)
@topright.setter
def topright(self, pos):
x = int(pos[0])
y = int(pos[1])
self.left = x - self.width
self.top = y
@property
def bottomleft(self):
return (self.left, self.bottom)
@bottomleft.setter
def bottomleft(self, pos):
x = int(pos[0])
y = int(pos[1])
self.left = x
self.top = y - self.height
@property
def bottomright(self):
return (self.right, self.bottom)
@bottomright.setter
def bottomright(self, value):
x = int(pos[0])
y = int(pos[1])
self.left = x - self.width
self.top = y - self.height
Code: Alles auswählen
from my_rect import My_Rect
ni = My_Rect(0, 0, 16, 16, (8, 8))
print ni
print "x", ni.x
print "y", ni.y
print "left", ni.left
print "topleft", ni.topleft
print "topright", ni.topright
ni.x += 5
print "ni.x += 5 -->"
print "x", ni.x
print "y", ni.y
print "left", ni.left
print "topleft", ni.topleft
print "topright", ni.topright
Code: Alles auswählen
<my_rect.My_Rect instance at 0x02B861E8>
x 8.0
y 8.0
left 0
topleft (0, 0)
topright (16, 0)
ni.x += 5 -->
x 13.0
y 8.0
left 0
topleft (0, 0)
topright (16, 0)