def testShift(self): lt = point.fromXY(-4.0, 6.0) rb = point.fromXY(10.0, -10.0) rect = fromPoints(lt, rb) rect.shiftX(1.0) rect.shiftY(-1.0) self.assertEqual(rect.topLeft(), point.fromXY(-3.0, 5.0)) self.assertEqual(rect.rightBottom(), point.fromXY(11.0, -11.0))
def testInitEmpty(self): lt = point.empty() rb = point.empty() rect = fromPoints(lt, rb) self.assertEqual(rect.topLeft(), lt) self.assertEqual(rect.rightBottom(), rb) self.assertEqual(rect.topRight(), point.fromXY(rb.x, lt.y)) self.assertEqual(rect.leftBottom(), point.fromXY(lt.x, rb.y))
def rectDP2LP(dpRect, dptZero, lPix=1.0): """Convert device coordinates into logical coordinates dpRect- rect in device coordinate dptZero - device coordinates of logical 0,0 point lPix - zoom value, number of logical points inside one device point (aka pixel) return rect in logical coordinates """ return rect.fromPoints( point.fromXY(pointDP2LP(top_left, dptZero, lPix), point.fromXY(pointDP2LP(right_bottom, dptZero, lPix))))
def rectLP2DP(lpRect, lptLT, lPix=1.0): """Convert logical coordinates into device coordinates lpRect- rect with logical coordinate lptLT - logical coordinates of left top screen corner lPix - zoom value, number of logical points inside one device point (aka pixel) return rect in device coordinates """ return rect.fromPoints( point.fromXY(pointLP2DP(top_left, lptLT, lPix), point.fromXY(pointLP2DP(right_bottom, lptLT, lPix))))
def testUnion2(self): rect1 = fromSizes(point.empty(), 50, 50) rect2 = fromSizes(point.fromXY(30, 30), 10, 10) rect = rect1.unionWith(rect2) self.assertEqual(rect.topLeft().x, 0) self.assertEqual(rect.topLeft().y, 50) self.assertEqual(rect.rightBottom().x, 50) self.assertEqual(rect.rightBottom().y, 0)
def pointCoordsDP2LP(dpX, dpY, dptZero, lPix=1.0): """Convert device coordinates into logical coordinates dpX - x device coordinate dpY - y device coordinate dptZero - device coordinates of logical 0,0 point lPix - zoom value, number of logical points inside one device point (aka pixel) return point in logical coordinates """ return point.fromXY(xDP2LP(dpX, dptZero, lPix), yDP2LP(dpY, dptZero, lPix))
def pointCoordsLP2DP(lpX, lpY, lptLT, lPix=1.0): """Convert logical coordinates into device coordinates lpX - x logical coordinate lpY - y logical coordinate lptLT - logical coordinates of left top screen corner lPix - zoom value, number of logical points inside one device point (aka pixel) return point in device coordinates """ return point.fromXY(xLP2DP(lpX, lptLT, lPix), yLP2DP(lpY, lptLT, lPix))
def constructPositive(screenWidth, screenHeight, mapWidth, mapHeight, tileSize=0): """Constructs map coordinates to place the map inside positive X and Y range to the centre of the screen""" screenRect = rect.fromSizes(point.fromXY(0.0, 0.0), screenWidth, screenHeight) # set screen sizes mapRect = rect.fromSizes(point.fromXY(0.0, 0.0), mapWidth, mapHeight) # set map sizes screenZero = screenRect.centre( ) # set logical 0.0 to the centre of ths screen mapCoordinates = MapCoordinates( screenRect, screenZero, mapRect, tileSize) # construct map coordinates translator mapCoordinates.shift( mapRect.centre().x, mapRect.centre().y) # place map at the centre of the screen return mapCoordinates
def testInitPointValues(self): lt = point.fromXY(4.0, 6.0) rb = point.fromXY(10.0, 10.0) rect = fromPoints(lt, rb) self.assertEqual(rect.topLeft(), point.fromXY(4.0, 10.0)) self.assertEqual(rect.rightBottom(), point.fromXY(10.0, 6.0)) self.assertEqual(rect.topRight(), point.fromXY(10.0, 10.0)) self.assertEqual(rect.leftBottom(), point.fromXY(4.0, 6.0))
def centre(self): """Get rect centre point""" return point.fromXY((self.__rightBottom.x + self.__topLeft.x) / 2.0, (self.__topLeft.y + self.__rightBottom.y) / 2.0)
def leftBottom(self): """Get left bottom rect point""" return point.fromXY(self.__topLeft.x, self.__rightBottom.y)
def topRight(self): """Get top right rect point""" return point.fromXY(self.__rightBottom.x, self.__topLeft.y)
def testNegativeCentre(self): lt = point.fromXY(-4.0, 6.0) rb = point.fromXY(10.0, -10.0) rect = fromPoints(lt, rb) pt = rect.centre() self.assertEqual(pt, point.fromXY(3.0, -2.0))
def testCentre(self): lt = point.fromXY(4.0, 6.0) rb = point.fromXY(10.0, 10.0) rect = fromPoints(lt, rb) pt = rect.centre() self.assertEqual(pt, point.fromXY(7.0, 8.0))
def fromSizes(ptStart, rectWidth, rectHeight): """Initialize rect with starting point, width and length""" return Rect(ptStart, point.fromXY(ptStart.x + rectWidth, ptStart.y + rectHeight))