- matplotlib

DDA Line Drawing Algorithm


Implementation of DDA Algorithm using pyscript and matplotlib

Input the X-coordinates and Y-coordinates and it will generate a matplotlib plot using the DDA line drawing algotithm.
Don't put equal values for the coordinates because the difference will be 0 which will make the denominator/numerator 0 so no plot will be displayed.










# Python Code Goes Here ... import matplotlib.pyplot as plt fig, ax = plt.subplots() def DDALine(x1, y1, x2, y2, color): dx = x2 - x1 dy = y2 - y1 # calculate steps required for generating pixels steps = abs(dx) if abs(dx) > abs(dy) else abs(dy) #calculate increment in x & y for each steps Xinc = float(dx / steps) Yinc = float(dy / steps) for i in range(0, int(steps + 1)): # Draw pixels plt.plot(int(x1), int(y1), color) x1 += Xinc y1 += Yinc plt.plot() def main(*args, **kwargs): xa = Element('x1').element xavalue = int(xa.value) ya = Element('y1').element yavalue = int(ya.value) xb = Element('x2').element xbvalue = int(xb.value) yb = Element('y2').element ybvalue = int(yb.value) x = xavalue y = yavalue xEnd = xbvalue yEnd =ybvalue color = "r." DDALine(x, y, xEnd, yEnd, color) fig result_place = Element('lineplot') result_place.write(fig)