Python:在画布上的两个点之间画线

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

我无法从2点创建一条直线。据我了解,点坐标是由原点计算的。原点位于画布的中间。我只完成了画布的绘制。有没有人有办法解决吗?测试:can = Canvas(31,11,“ +”)l =线(0、5、0,-5)can.draw(l,“ L”)can.display()一定是:+ + + + + + + + + + + + + + L + + + + + + + + + + + + + + + + + + ++ + + + + + + + + + + + + + L + + + + + + + + + + + + + + + + + + ++ + + + + + + + + + + + + + L + + + + + + + + + + + + + + + + + + ++ + + + + + + + + + + + + + L + + + + + + + + + + + + + + + + + + ++ + + + + + + + + + + + + + L + + + + + + + + + + + + + + + + + + ++ + + + + + + + + + + + + + L + + + + + + + + + + + + + + + + + + ++ + + + + + + + + + + + + + L + + + + + + + + + + + + + + + + + + ++ + + + + + + + + + + + + + L + + + + + + + + + + + + + + + + + + ++ + + + + + + + + + + + + + L + + + + + + + + + + + + + + + + + + ++ + + + + + + + + + + + + + L + + + + + + + + + + + + + + + + + + ++ + + + + + + + + + + + + + L + + + + + + + + + + + + + + +

class Canvas():
    def __init__(self, width, height, background = " "):
        """Constructor for the Canvas class
        """
        self.width = width
        self.height = height
        self.background = background
        self.canvas = []
        for i in (range(self.height)):
            a = [self.background for j in range(self.width)]
            self.canvas.append(a)

    def display(self):
        """
        Printing method for the canvas. When triggered outputs the canvas as characters.
        Example outputs are given in the test cases. White spaces may be used as you prefer,
        but you must have *at least* one whitespace between every character. 
        """
        for i in reversed(range(self.height)):
            for j in (range(self.width)):
                print(self.canvas[i][j],end=' ')
            print("")
    def draw(self, toDraw, color="A"):
        """
        When given a Line draw all points on the Line. We are currently working with an approximation.
        Any point with an euclidian distance to the line of less than or equal 0.7 is drawn.
        Make sure to clip your lines using the Cohen-Sutherland algortihm.
        After clipping, floor the coordinates of the endpoints

        """
class Line():
    def __init__(self, x1, y1, x2, y2):
        """Constructor for Line
        The first point is (x1, y1), the second point is (x2, y2).
        All coordinates are given as cartesian coordinates.
        """
        self.x1 = x1
        self.x2 = x2
        self.y1 = y1
        self.y2 = y2

python canvas line point
1个回答
0
投票

看这个:Check is a point (x,y) is between two points drawn on a straight line

检查是否在线上,但不会显示任何结果。计算斜边的比特温和斜边以及将点和线的顶点连接的2条线的总和。

d = lambda p1,p2: ((p1[0]-p2[0])**2 + (p1[1]-p2[1])**2)**.5 #Distance beetwin 2 poins
on = lambda pf,pt,point: d(pf,pt) - (d(pf,point)+d(pt,point)) #difference between hypotenus and 2 lines ...

def draw(points, w=10, h=10):
    '''
    This is not the optimized one, it just check all points 
    '''
    l = [[(1 if (x,y) in points else 0) for y in range(10)] for x in range(10)] #1=point
    print('\n'.join([' '.join([('.' if x else '0') for x in y]) for y in l]))

r = 0.1 #This will set the presition of your line
points = [(x,y) for x in range(10) for y in range(10) if abs(on(p1,p2,(x,y)))<0.1]

r = 0.01时

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 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 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

r = 0.1时

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 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
0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0
© www.soinside.com 2019 - 2024. All rights reserved.