Python:从另一个类调用方法

问题描述 投票:0回答:1

如何将getOrigin中的“来源”称为Point()类的旋转我正在尝试使用Canvas.getOrigin(self),但是它报告了AttributeError:'Point'对象没有属性'height或Canvas()。getOrigin()->缺少2个必需的位置参数:“宽度”和“高度”

class Canvas():
    def __init__(self, width, height, background = " "):
        """Constructor for the Canvas class"""
        self.width = width
        self.height = height
        self.background = background

    def getOrigin(self):
        """
        Returns the origin of the canvas. The origin is positioned in the middle of the canvas.
        You may assume that the width and height of every canvas is odd.
        """
        origin = [(self.height - 1)//2,(self.width - 1)//2]
        return origin
        pass


class Point():
    def __init__(self, x, y):
        """Constructor for the point class"""
        self.x = x
        self.y = y
        pass

    def rotate(self, angle):
        """Rotates the point around the origin. Any angles are processed clockwise"""

        self.x = origin[0] + math.cos(math.radian(self.angle)) * (self.x - origin[0]) - math.sin(math.radian(self.angle)) * (self.y - origin[1])
        self.y = origin[1] + math.sin(math.radian(self.angle)) * (self.x - origin[0]) + math.cos(math.radian(self.angle)) * (self.y - origin[1])

        pass


python class call
1个回答
0
投票
import math
class Canvas():
      def __init__(self, width, height, background = " "):
      """Constructor for the Canvas class"""
          self.width = width
          self.height = height
          self.background = background

      def getOrigin(self):
      """Returns the origin of the canvas. The origin is positioned in the middle of the canvas. You may assume that the width and height of every canvas are odd."""
          origin = [(self.height - 1)//2,(self.width - 1)//2]
          return origin


class Point():
      def __init__(self, x, y):
      """Constructor for the point class"""
          self.x = x
          self.y = y
          self.origin=Canvas(self.x,self.y).getOrigin()
          pass

      def rotate(self, angle):
      """Rotates the point around the origin. Any angles are processed clockwise"""

          self.x = self.origin[0] + math.cos(math.radians(angle)) * (self.x-self.origin[0]) - math.sin(math.radians(angle)) * (self.y - self.origin[1])
          self.y = self.origin[1] + math.sin(math.radians(angle)) * (self.x - self.origin[0]) + math.cos(math.radians(angle)) * (self.y - self.origin[1])
          print(self.origin)

p=Point(8,8)
p.rotate(90)
© www.soinside.com 2019 - 2024. All rights reserved.