Skip to content

Instantly share code, notes, and snippets.

@0xKD
Last active August 26, 2022 09:29
Show Gist options
  • Save 0xKD/4433688 to your computer and use it in GitHub Desktop.
Save 0xKD/4433688 to your computer and use it in GitHub Desktop.
DDA in Python
def ROUND(a):
return int(a + 0.5)
def drawDDA(x1,y1,x2,y2):
x,y = x1,y1
length = abs((x2-x1) if abs(x2-x1) > abs(y2-y1) else (y2-y1))
dx = (x2-x1)/float(length)
dy = (y2-y1)/float(length)
print 'x = %s, y = %s' % (((ROUND(x),ROUND(y))))
for i in range(length):
x += dx
y += dy
print 'x = %s, y = %s' % (((ROUND(x),ROUND(y))))
drawDDA(2,5,10,20)
@0xKD
Copy link
Author

0xKD commented Aug 17, 2020

length = abs(x2-x1) if abs(x2-x1) > abs(y2-y1) else abs(y2-y1)

As it is written drawDDA(1,5,10,20) doesn't return the same set (or even count of) values as drawDDA(10,20,1,5)

Should work now

@asim-vaibhav-435
Copy link

'x = %s, y = %s' % (((ROUND(x),ROUND(y)))) this statement should be in parenthesis, else it shows error in VScode.

@0xKD
Copy link
Author

0xKD commented Mar 4, 2022

'x = %s, y = %s' % (((ROUND(x),ROUND(y)))) this statement should be in parenthesis, else it shows error in VScode.

This is python2 code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment