class Mapper:
def __init__(self, old_ul, old_lr, new_ul, new_lr):
self.old_ul = old_ul
self.old_lr = old_lr
self.new_ul = new_ul
self.new_lr = new_lr
self.old_diff = Point(old_lr.x - old_ul.x,
old_ul.y - old_lr.y)
self.new_diff = Point(new_lr.x - new_ul.x,
new_ul.y - new_lr.y)
def map(self, pt):
nx = (self.old_lr.x+pt.x) * (self.new_diff.x / self.old_diff.x) + self.new_ul.x
ny = (self.old_ul.y+pt.y) * (self.new_diff.y / self.old_diff.y) + self.new_lr.y
return Point(nx, ny)
def main(args):
pixelToReal = Mapper(Point(0, 0), Point(600, 600), Point(-2, 2), Point(2, -2))
realToPixel = Mapper(Point(-2, 2), Point(2, -2), Point(0, 0), Point(600, 600))
print(pixelToReal.map(Point(300,300))) # prints 0.0,0.0
print(realToPixel.map(pixelToReal.map(Point(300,300)))) # prints 300,300
for x in range(0,630, 30):
for y in range(0,630, 30):
print(pixelToReal.map(Point(x,y)))
Mapper 的构造函数取原来的左上角和右下角,以及新的左上角和右下角。Mapper.map() 将一个点从原始空间转换为新空间。