如何获取绘制直线的 xy 坐标?
How do I get the xy coordinates for drawing a line?
我想得到你为了画对角线应该去的坐标。
我想象这样的事情:
def drawLine(x,y,x1,y1):
return #all the coordinates that you need to go to in order to draw from x y to x1 y1
def drawLine(x,y,x1,y1):
if (x>x1):
temp = x
x = x1
x1 = temp
elif (y>y1):
temp = y
y = y1
y1 = temp
listx = list(i for i in range (x,x1+1))
listy = list(j for j in range (y,y1+1))
m = (y1-y)/(x1-x)
c = y - (m*x)
for i in listx:
for j in listy:
if((i*m)+c == j):
print("(",str(i),",",str(j),")")
drawLine(0,0,3,3)
这个程序适用于整数坐标。这是一个坐标几何理论的实现,其中m代表梯度,c代表截距。
float 或 double 在数学上具有无限坐标。
def drawLines(x1, y1, x2, y2):
r = 10 # resolution
diff_x, diff_y = abs(x2-x1),abs(y2-y1)
return [(diff_x*i/r + x1 if x2>x1 else x1 - diff_x*i/r , diff_y*i/r + y1 if y2>y1 else y2-diff_y*i/r) for i in range(r+1)]
此代码应该有效。例如;
print(drawLines(3,1,1,3)) # Returns a list of points
# [(3.0, 1.0), (2.8, 1.2), (2.6, 1.4), (2.4, 1.6), (2.2, 1.8), (2.0, 2.0), (1.8, 2.2), (1.6, 2.4), (1.4, 2.6), (1.2, 2.8), (1.0, 3.0)]
我想得到你为了画对角线应该去的坐标。 我想象这样的事情:
def drawLine(x,y,x1,y1):
return #all the coordinates that you need to go to in order to draw from x y to x1 y1
def drawLine(x,y,x1,y1):
if (x>x1):
temp = x
x = x1
x1 = temp
elif (y>y1):
temp = y
y = y1
y1 = temp
listx = list(i for i in range (x,x1+1))
listy = list(j for j in range (y,y1+1))
m = (y1-y)/(x1-x)
c = y - (m*x)
for i in listx:
for j in listy:
if((i*m)+c == j):
print("(",str(i),",",str(j),")")
drawLine(0,0,3,3)
这个程序适用于整数坐标。这是一个坐标几何理论的实现,其中m代表梯度,c代表截距。 float 或 double 在数学上具有无限坐标。
def drawLines(x1, y1, x2, y2):
r = 10 # resolution
diff_x, diff_y = abs(x2-x1),abs(y2-y1)
return [(diff_x*i/r + x1 if x2>x1 else x1 - diff_x*i/r , diff_y*i/r + y1 if y2>y1 else y2-diff_y*i/r) for i in range(r+1)]
此代码应该有效。例如;
print(drawLines(3,1,1,3)) # Returns a list of points
# [(3.0, 1.0), (2.8, 1.2), (2.6, 1.4), (2.4, 1.6), (2.2, 1.8), (2.0, 2.0), (1.8, 2.2), (1.6, 2.4), (1.4, 2.6), (1.2, 2.8), (1.0, 3.0)]