Skip to content

Instantly share code, notes, and snippets.

@0xKD
Last active August 26, 2022 09:29
Show Gist options
  • Select an option

  • Save 0xKD/4433688 to your computer and use it in GitHub Desktop.

Select an option

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)
@jpsluka

jpsluka commented Aug 7, 2020

Copy link
Copy Markdown

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)

@0xKD

0xKD commented Aug 17, 2020

Copy link
Copy Markdown
Author

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
Copy Markdown

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

@0xKD

0xKD commented Mar 4, 2022

Copy link
Copy Markdown
Author

'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