Drawing diagonal lines through the image

I am trying to draw parallel lines diagonally from the upper right corner to the lower left corner of the image. I want it to look like this (beautiful picture drawing)

diag paint pic

def diagTopLBottomR(): pic=makePicture(pickAFile()) w=getWidth(pic) h=getHeight(pic) x1=0 y1=0 x2=0 y2=0 i=0 while i<11: x1=10*i y2=10*i i+=1 for y in range (y1,y2): x = (y-y1)*(x2-x1)/(y2-y1) +x1 px=getPixel(pic,x,y) color=makeColor(0,0,0) setColor(px, color) x3=0 y3=h x4=w y4=0 j=0 while j<10: x3=10*j y4=10*j j+=1 for y in range (y3,y4): x = (y-y3)*(x4-x3)/(y4-y3) +x3 px=getPixel(pic,x,y) color=makeColor(0,0,0) setColor(px, color) return(pic) 

You will notice that x3 will either be the maximum value, causing an exception to the range, or the range y will start from a higher value, i.e. (y3> y4), and will not work the other way around, or when I reduce it. This is like a paradox.

The first cycle works, no matter what, I cannot get the second cycle to work. This is what I get.

diag lines

Any ideas? Thanks.


Edit

I played with ranges and did not get results for the second cycle, as shown above, from an exception outside the range.

I tried:

  x3=0 y3=h x4=w y4=0 j=0 while j<10: x3=10*j y4=10*j j+=1 for x in range (x3,x4): y = (x-x3)*(y4-y3)/(x4-x3) +y3 

Steal the unicorns from here .

+6
source share
4 answers

In the first part, y1 set to 0 and y2 increases from 0 in the cycle, so y1 < y2 . This is normal because you use

 for y in range (y1,y2) 

In the second part, y3 sets h (in your case, in your opinion, 128), and y4 increases from 0 in the loop, so y3 > y4 . This is NOT normal because you are using

 for y in range (y3,y4) 

You can try to take a step back by specifying range() third parameter specifying a step size of -1. Or you can switch y3 and y4 (be careful what this does with the rest of your code).

+7
source

range() assumes that the first parameter is smaller than the second parameter, and it goes in ascending order. You have:

 for y in range (y3,y4): 

where y3=h and y4=0 (on the first pass). Since y3> y4, this loop does nothing. You can use either:

 for y in range(y4,y3): 

or

 for y in range(y3,y4,-1): 
+4
source

In the second cycle, y3 greater than (or equal to) than y4 . So try range (y4,y3) .

+4
source

I solved this by making px=getPixel(pic,x,y-1) and using the suggested answers about decreasing the y range.

 def diagTopLBottomR(): pic=makePicture(pickAFile()) w=getWidth(pic) h=getHeight(pic) x1=0 y1=0 x2=0 y2=0 i=0 while i<10: x1=10*i y2=10*i i+=1 for y in range (y1,y2): x = (y-y1)*(x2-x1)/(y2-y1) +x1 px=getPixel(pic,x,y) color=makeColor(0,0,0) setColor(px, color) x3=0 y3=h x4=w y4=0 j=0 while j<10: x3=10*j y4=10*j j+=1 for y in range (y3,y4,-1):#change here x = abs((y-y3)*(x4-x3)/(y4-y3) +x3) px=getPixel(pic,x,y-1)#change here color=makeColor(0,0,0) setColor(px, color) return(pic) 

pic

+2
source

Source: https://habr.com/ru/post/951052/


All Articles