[英]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)]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.