绘制一条从A到B的线,但总是垂直

时间:2016-03-31 02:51:17

标签: python python-3.x

新手程序员在这里试图根据2坐标(x0,y0)和(x1,y1)的输入绘制一条线。我无法弄清楚如何计算线的角度,因此结果始终是垂直线。我想我在计算角度的方式上有问题,或者在for循环中输入角度。

任何人都知道我的代码可能出现了什么问题?

from images import Image

def drawLine(image, x0, y0, x1, y1, red, green, blue):
    image = Image(image)
    color = (red, green, blue)
    a = (y0 - y1)/(x0 - x1)
    xAxis = x1 - x0
    yCount = y0
    for x in range(xAxis):
        image.setPixel(x0,yCount,color)
        yCount = int(yCount + a)
    return image.draw()

def main():
    drawLine("Cat2.gif",25,30,40,60,255,0,0)

main()  

以下是我不断得到的结果:1

3 个答案:

答案 0 :(得分:1)

更改行

image.setPixel(x0,yCount,color)

image.setPixel(x,yCount,color)

x0 + x,我无法说出来。

答案 1 :(得分:0)

我相信这是因为您每次都在image.setPixel(x0,yCount,color)使用相同的x值 我不确定你是否打算放x0,但每次for循环运行时该变量都不会改变。

答案 2 :(得分:0)

将你的for循环修改为这样:

for x in np.arange(x0,x1,0.1):
   image.setPixel(x,(a*x+y0),color)

没有尝试过运行它但是应该可以运行!