Skip to content

Instantly share code, notes, and snippets.

@krrrr38
Created January 23, 2012 10:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save krrrr38/1662334 to your computer and use it in GitHub Desktop.
Save krrrr38/1662334 to your computer and use it in GitHub Desktop.
sonyの問題
#!/usr/bin/python
# -*- coding:utf-8 -*-
'''
他人のコード使って重要な所間違ってたからそこだけ修正したやつ
'''
from itertools import product
from collections import namedtuple
Point3d = namedtuple("Point3", "x y z")
Size3d = namedtuple("Size3d", "x y z")
class Rectangular:
__slots__ = "_point _size _normalized".split()
def __init__(self, point, size):
self._point = point
self._size = size
self._normalized = None
@property
def point(self):
return self._point
@property
def size(self):
return self._size
def is_normal(self):
"""辺の長さが全て正ならTrue"""
return all(s >= 0 for s in self._size)
def normalized(self):
"""正規化する"""
if self._normalized is not None:
# 既に正規化されていたならそれを返す
return self._normalized
if self.is_normal():
# 正規化する必要がない(全ての辺が正)なら
self._normalized = self
return self
# 正規化の処理
point = []
size = []
for x, length in zip(self._point, self._size):
if length < 0:
x += length + 1
length = -length
point.append(x)
size.append(length)
self._normalized = Rectangular(Point3d(*point), Size3d(*size))
return self._normalized
def overwraps(self, rec):
# 修正
unoverwraps = True
for x0, x1, w0, w1 in zip(self._point, rec._point, self._size, self._size):
temp_result = ((x0 + w0) < x1) or ((x1 + w1) < x0)
unoverwraps = unoverwraps and temp_result
return not unoverwraps
def __eq__(self, rec):
n0 = self.normalized()
n1 = rec.normalized()
return n0._point == n1._point and n0._size == n1._size
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment