社区所有版块导航
Python
python开源   Django   Python   DjangoApp   pycharm  
DATA
docker   Elasticsearch  
aigc
aigc   chatgpt  
WEB开发
linux   MongoDB   Redis   DATABASE   NGINX   其他Web框架   web工具   zookeeper   tornado   NoSql   Bootstrap   js   peewee   Git   bottle   IE   MQ   Jquery  
机器学习
机器学习算法  
Python88.com
反馈   公告   社区推广  
产品
短视频  
印度
印度  
Py学习  »  Python

python:关于画布绘图程序嵌入的问题

Santosh • 7 年前 • 711 次点击  

我编写了代码,好奇心很好地翻译了一些Java代码到Python。问题陈述如下:

Command Description C w h Create a new canvas of width w and height h. L x1 y1 x2 y2 Create a new line of 'x' from (x1,y1) to (x2,y2). Only support horizontal or vertical lines. R x1 y1 x2 y2 Create a new rectangle, (x1,y1) is upper left corner & (x2,y2) is lower right corner. B x y c Fill the entire area around (x,y) with "colour" c. Same as that of the "bucket fill" tool in paint programs. Q Quit.

下面是我的代码,它工作正常。如果这是一个更好的解决方案或者可以以更好的方式实现(使用子类可能是?)

下面是我的代码,它工作正常。如果这是一个更好的解决方案或者可以以更好的方式实现(使用子类可能是?)

class CanvasApp(object):

def __init__(self):
    self.canvasArray= []

def init(self, width, height):
    self.width = width + 2
    self.height = height + 2
    self.canvasArray = [['' for x in range(self.width+2)] for y in range(self.height+2)]

    self.drawLine(0, 0, self.width-1, 0, '-');
    self.drawLine(0, self.height-1, self.width-1, self.height-1, '-');
    self.drawLine(0, 1, 0, self.height-2, '|');
    self.drawLine(self.width-1, 1, self.width-1, self.height-2, '|');
    return self.doAction()

def checkCanvasExists(self):
    if not self.canvasArray:
        raise Exception("Canvas doesn't exist. Please create a canvas first.")    

def doAction(self):

    stringArr = []

    for i in range(0, self.height):
        for j in range(0, self.width):
            if self.canvasArray[i][j] == '':
                res = ' '
            else:
                res = self.canvasArray[i][j]
            stringArr.append(res);

        stringArr.append("\n");

    return ''.join(stringArr).strip();

def drawLine(self, x1, y1, x2, y2, char='x'):
    self.checkCanvasExists()
    x1, x2, y1, y2 = int(x1), int(x2), int(y1), int(y2)

    for i in range(y1, y2+1):
        for j in range(x1, x2+1):
            self.canvasArray[i][j] = char;
    return self.doAction()

def drawRectangle(self, x1, y1, x2, y2, char='x'):
    self.checkCanvasExists()
    self.drawLine(x1, y1, x2, y1, char)
    self.drawLine(x1, y1, x1, y2, char)
    self.drawLine(x2, y1, x2, y2, char)
    self.drawLine(x1, y2, x2, y2, char)
    return self.doAction()

def fillColor(self, x, y, char='x'):
    self.checkCanvasExists()
    x, y = int(x), int(y)
    if(self.canvasArray[y][x] != ''):
        return;

    if(x > 0 or x < self.height or y > 0 or y  < self.width):
        if(self.canvasArray[y][x] == ''):
            self.canvasArray[y][x] = char
            self.fillColor(x+1,y, char)
            self.fillColor(x-1,y, char)
            self.fillColor(x,y-1, char)
            self.fillColor(x,y+1, char)
    return self.doAction()


def draw(self, command):
    args = command.split(' ')
    firstArg = args[0]
    if firstArg == 'B':
        remArgs=[int(arg) for arg in args[1:-1]]
        remArgs.append(args[-1])
    else:
        remArgs=[int(arg) for arg in args[1:]]

    switch= {'C': self.init,
             'L' : self.drawLine,
             'R' : self.drawRectangle,
             'B' : self.fillColor
            }
    try:
        func = switch.get(firstArg, lambda *args: 'Invalid Argument. Commands should start with C, L, R or B')
        res = func(*remArgs)
        print res
    except Exception as e:
        print str(e) + '. Please try again'


def start(self):
    while True:
        command = raw_input("Enter the command:")
        if command == 'Q':
            break
        self.draw(command)

def main():
    canvasApp = CanvasApp()
    canvasApp.start()
Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/44354