如何储存一根线?
①记录下函数f(x)=ax+d的各个参数
②检测线线相交时用a1-a2是否等于零判别,然后求x=(b2-b1)/(a1-a2),完事套公式求y
③绘制使用dda
class map:
class Line(Line_show):
def __init__(self,P1,P2):
self.x = P1
self.y = P2
self.Ix = (P2[0] - P1[0])
self.Iy = (P2[1] - P1[1])
self.k = self.Iy/self.Ix
self.d = P1[1] - self.k*P1[0]
def Line_and_Line(self,Line):
Delta = self.k - Line.k
if Delta:
x = (Line.b - self.b)/Delta
y = self.k*x + self.b
return {whether:1,P:[x,y]}
else:
return {whether:0,P:None}
def Line_and_spot(self,P):
return {whether: 1 if self.k*P[0] + self.b ==P[1] else 0}
class Line_show:
def main(self,map,Line):
if Ix:
if Iy:
self.__DDA(map,Line)
else:
y[0] = y[0] - 1
for i in range(x[0], y[0] + 1):
map.data[x[1]][i] = 1
else:
x[0] = x[0] - 1
for i in range(x[1], y[1] + 1):
map.data[i][x[0]] = 1
def __DDA(map,Line):
k = Line.k
b = Line.x[1] - k * Line.x[0]
Iy = Line.x[1]
for Ix in range(Line.x[0], Line.y[0]):
map.data[Ix][int(abs(Iy))] = 1
Iy = Iy + k



