【发布时间】:2020-06-08 14:02:09
【问题描述】:
我无法从 2 个点创建一条直线。据我了解,点坐标是由原点计算的。原点位于画布的中间。我只画了画布。有人有解决办法吗?
测试:
can = Canvas(31, 11, "+")
l = Line(0, 5, 0, -5)
can.draw(l, "L")
can.display()
一定是:
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
class Canvas():
def __init__(self, width, height, background = " "):
"""Constructor for the Canvas class
"""
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.
"""
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
【问题讨论】: