【发布时间】:2014-05-13 07:48:14
【问题描述】:
我正在尝试在我的 Class Circle 中构建一个函数 draw ,它获取一个由列表列表表示的矩阵,它有 m 行和 n 列,每个单元格中都有 0,(i,j) cell 表示 (i,j) 点,该函数应将给定圆圈中包含的每个 (i,j) 单元格更改为一个。
例子:
>>> mat =[[0 for j in range(5)] for i in range(7)]
>>> mat
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0,
0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
>>> Circle(40,10,1).draw(mat)
>>> mat
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 1, 0, 0], [0, 1, 1, 1,
0], [0, 0, 1, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
这是我写的代码:
import math
class Point():
""" Holds data on a point (x,y) in the plane """
def __init__(self, x=0, y=0):
assert isinstance(x,(int, float)) and isinstance(y,(int, float))
self.x = x
self.y = y
class Circle():
""" Holds data on a circle in the plane """
def __init__(self,*args):
if len(args)==2:
if isinstance(args[0],Point) and isinstance(args[1],(float,int)):
assert args[1]>0
self.center= args[0]
self.radius= args[1]
if len(args)==3:
assert args[2]>0
self.a=args[0]
self.b=args[1]
self.center= Point(self.a,self.b)
self.radius= args[2]
def contains(self,check):
if isinstance(check,(Point)):
if math.sqrt((self.center.x-check.x)**2 + (self.center.y-check.y)**2) <= self.radius:
return True
if isinstance(check,Circle):
test= math.sqrt((self.center.x-check.center.x)**2 + (self.center.x-check.center.x)**2)
if test < (abs((self.radius)-(check.radius))):
return True
else:
return False
def intersect(self,other):
check= math.sqrt((self.center.x-other.center.x)**2 + (self.center.y-other.center.y)**2)
if check >(self.radius+other.radius):
return False
if check < (self.radius+other.radius):
return True
def draw(self,mat):
for lst in mat:
for j in lst:
if Circle.contains(self,(lst,j)):
mat[lst[j]]=1
【问题讨论】:
-
有什么问题?看起来它将几个值更改为 1。
-
进入循环?你甚至不调用该方法。其余代码在哪里?
-
它应该将值更改为一个,但是当我运行我的代码并调用该方法时,列表根本没有改变! @thefourtheye
标签: python list class oop matrix