Search Here

Python program to draw Straight Line using DDA algorithm

# Line using DDA Algorithm

import pygame
from pygame import gfxdraw
import time

# pygame.init()
screen = pygame.display.set_mode((400,400))
screen.fill((255,255,255))
# pygame.display.flip()
black=(0,0,0)

def ROUND(n):
    return int(n+0.5)

def DDA_LINE(x1,y1,x2,y2):
    dx=abs(x1-x2)
    dy=abs(y1-y2)

    if(dx>dy):
        steps=dx
    else:
        steps=dy
        
    x_inc = dx/steps
    y_inc = dy/steps
    x=x1
    y=y1
    gfxdraw.pixel(screen,ROUND(x),ROUND(y),black)
    
    for k in range(0,steps):
        x += x_inc  
        y += y_inc
        gfxdraw.pixel(screen,ROUND(x),ROUND(y),black)
        
    pygame.display.flip()
    time.sleep(15)
    
    
DDA_LINE(10,10,60,200)

No comments:

Post a Comment