FC2ブログ

Python::user-defined_class::point2d

# -*- coding: utf-8 -*-
import copy
class point2d:
    def __init__ (self, x, y):
        self.x = x
        self.y = y
        
    def __add__ (self, other):
        x = self.x + other.x
        y = self.y + other.y
        return point2d(x, y)

    def __sub__ (self, other):
        x = self.x - other.x
        y = self.y - other.y
        return point2d(x, y)
    
    def __mul__ (self, other):
        x = self.x * other
        y = self.y * other
        return point2d(x, y)
    
    def __rmul__ (self, other):
        x = self.x * other
        y = self.y * other
        return point2d(x, y)
    
    def to_int (self):
        x = int(copy.copy(self.x))
        y = int(copy.copy(self.y))
        return point2d(x, y)

    def to_tuple (self):
        x = copy.deepcopy(self.x)
        y = copy.deepcopy(self.y)
        return (x, y)

    def print (self):
        print ("({0}, {1})".format(self.x, self.y))

関連記事
スポンサーサイト



コメント

非公開コメント