-1
我的代碼運行良好,只有一個矩形,但只要我添加第二個矩形,它就會說「'點'對象不可調用」,儘管已成功調用第一個矩形。我已經對矩形上的不同變化進行了足夠多的測試,得出結論:唯一的原因是它現在正在嘗試創建多個矩形。任何人都可以幫忙嗎?給定類的對象可調用一次,但不能再次
下面是代碼的開始,它用於定義不同的元素及其參數。
import matplotlib.pyplot as plt
import numpy
elementset = []
pointxs = []
pointys = []
class point(object):
"""General point in 2d space, with stored x value and y value.
Created and used in elements to give them shape.
"""
def __init__(self, x, y):
self.x = float(x)
self.y = float(y)
self.isAnchor = False
def __repr__(self):
return "(%d, %d)" % (self.x, self.y)
class element(object):
"""Most general class used to define any element to be used in
the cross-section being worked with. Used as a basis for more
specific classes. Has a coordinate value and a number of points that
need to be generated for the general case.
"""
def __init__(self, anchor_x, anchor_y):
self.num_points = 0
self.anchor_x = float(anchor_x)
self.anchor_y = float(anchor_y)
elementset.append(self)
def getinfo(self):
"""Used for debugging, prints all general info for the element.
Never called in the actual code."""
print "Number of points: " + str(self.num_points)
print "x coordinate: " + str(self.anchor_x)
print "y coordinate: " + str(self.anchor_y)
def debug(self):
self.getinfo()
class rectangle(element):
"""A rectangle, assumed to be aligned such that all sides are
either vertical or horizontal. Calls/assigns variables
created in the element class via super().
"""
def __init__(self, anchor_x, anchor_y, width, height):
super(rectangle, self).__init__(anchor_x, anchor_y)
self.title = "rectangle"
self.num_points = 4
self.width = float(width)
self.height = float(height)
self.generate()
self.calculate()
def generate(self):
"""Creates the points that frame the rectangle using coordinates.
For a rectangle, the anchor point represents the bottom left point."""
self.anchor = point(self.anchor_x, self.anchor_y)
self.pointxpos = point(self.anchor_x + self.width, self.anchor_y)
self.pointxypos = point(self.anchor_x + self.width, self.anchor_y + self.height)
self.pointypos = point(self.anchor_x, self.anchor_y + self.height)
self.points = [self.anchor, self.pointxpos, self.pointxypos, self.pointypos]
self.plotpoints = [self.anchor, self.pointxpos, self.pointxypos, self.pointypos, self.anchor]
這裏是調用這些(只有1矩形定義)功能:
ar = rectangle(0,0,50,20)
for element in elementset:
if isinstance(element,rectangle):
element.generate()
for point in element.plotpoints:
pointxs.append(point.x)
pointys.append(point.y)
plt.plot(pointxs,pointys, linewidth=3)
elif isinstance(element,square):
pass #placeholder
elif isinstance(element,circle):
pass #placeholder
elif isinstance(element,semicircle):
pass #placeholder
plt.show()
這是成功的,在(0,0)密謀用左下角一個50x20的長方形。
但如果我是添加另一元件之下AR:
ar = rectangle(0,0,50,20)
br = rectangle(50,20,10,10)
它拋出「‘點’對象不是可調用」。 我真的被這個難住了,所以提前謝謝你的幫助。