You need to use the following formula to find the line between two points.
(y-y0)/(y1-y0)=(x-x0)/(x1-x0)
In my code I have used x1,y1
and x2,y2
representative of the first and second points the user inputs.
Manipulate the above equation to solve for x as follows:
def drawAnyLine(p):
w= getWidth(p)
h= getHeight(p)
newPic= makeEmptyPicture(w,h)
x1=requestIntegerInRange("Enter x1 between 1 and " , 1,w)
y1=requestIntegerInRange("Enter y1 between 1 and " , 1,h)
x2=requestIntegerInRange ("Enter x2 between 1 and ", 1, w)
y2=requestIntegerInRange("Enter y2 between 1 and ", 1, h)
for y in range (y1,y2):
for x in range (x1,x2):
x = (y-y1)*(x2-x1)/(y2-y1) +x1
pxl = getPixel(p, x, y)
newPxl= getPixel(newPic,x,y)
color = getColor(pxl)
setColor ( newPxl, color)
return (newPic)