Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
14 views

CG Lab Code

Uploaded by

niraajstha
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

CG Lab Code

Uploaded by

niraajstha
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 6

#DDA Line

import pygame
import sys

pygame.init()

#width, height = 800, 900


screen = pygame.display.set_mode((800,700))
pygame.display.set_caption("DDA line drawing Algorithm")

White=(255,255,255)
BLACK=(0,0,0)

def draw_line_dda(x1,y1,x2,y2):
dx=x2-x1
dy=y2=y1
steps=max(abs(dx),abs(dy))
x_increment=dx/ steps
y_increment=dy/ steps
x=x1
y=y1
for i in range(steps):
screen.set_at((round(x),round(y)),BLACK)
x+=x_increment
y+=y_increment

def main():
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
sys.exit()

screen.fill(White)

draw_line_dda(20,20,100,100)

pygame.display.flip()

if __name__=="__main__":
main()

#Bresenham Line

import pygame
import sys

pygame.init()

#width, height = 800, 900


screen = pygame.display.set_mode((800,600))
pygame.display.set_caption("Bresenham line drawing Algorithm")
White=(255,255,255)
BLACK=(0,0,0)

def draw_line_dda(x1,y1,x2,y2):
dx=x2-x1
dy=y2-y1
if (x2>x1):
ls=1
else:
ls=-1
if (y2>y1):
ly=1
else:
ly=-1
x ,y =x1, y1
if(dx>dy):
p=2*dy-dx
while(x!=x2):
if(p<0):
x=x+ls
p=p+2*dy
else:
p=p+2*dx-2*dy
x=x+ls
y=y+ly
screen.set_at((x,y),"black")

else:
p=2*dx-dy
while(y!=y2):
if(p<0):
y=y+ly
p=p+2*dx
else:
y=y+ly
x=x+ls
p=p+2*dx-2*dy
screen.set_at((x,y),"black")

def main():
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
sys.exit()

screen.fill("white")

draw_line_dda(100,100,200,200)

pygame.display.flip()

if __name__=="__main__":
main()
#Circle

import pygame
import sys

pygame.init()

#width, height = 800, 900


screen = pygame.display.set_mode((800,600))
pygame.display.set_caption("Midpoint circle drawing Algorithm")

black= (0, 0, 0)
white= (255, 255, 255)

def draw_circle_midpoint(x1,y1,r):

x=0
y=r
p=1-r

while(x<=y):
screen.set_at((x1+y,y1+x),white)
screen.set_at((x1+x,y1+y),white)
screen.set_at((x1-x,y1+y),white)
screen.set_at((x1+y,y1-x),white)
screen.set_at((x1-x,y1-y),white)
screen.set_at((x1-y,y1-x),white)
screen.set_at((x1+x,y1-y),white)
screen.set_at((x1-y,y1+x),white)

x=x+1
if(p<0):
p=p+2*x+1
else:
y=y-1
p=p+2*x-2*y+1

def main():
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
sys.exit()

screen.fill(black)

draw_circle_midpoint(200,200,100)

pygame.display.flip()

if __name__=="__main__":
main()
#Ellipse

import pygame
import sys

pygame.init()

#width, height = 800, 900


screen = pygame.display.set_mode((700,500))
pygame.display.set_caption("Midpoint Ellipse drawing Algorithm")

black= (0, 0, 0)
white= (255, 255, 255)

def draw_ellipse_midpoint(xc,yc,rx,ry):

x=0
y=ry
p1=pow(ry,2)-pow(rx,2)*ry+1/4*pow(rx,2)

while(2*pow(ry,2)*x<=2*pow(rx,2)*y):
screen.set_at((xc+x,yc+y),white)
screen.set_at((xc-x,yc+y),white)
screen.set_at((xc-x,yc-y),white)
screen.set_at((xc+x,yc-y),white)

if(p1<0):
x=x+1
y=y
p1=p1+2*pow(ry,2)*x+pow(ry,2)
else:
x=x+1
y=y-1
p1=p1+2*pow(ry,2)*x-2*pow(rx,2)*y+pow(ry,2)

p2=round(pow(ry,2)*pow((x+1/2),2)+pow(rx,2)*pow((y-1),2)-pow(rx,2)*pow(ry,2))

while(y>0):

screen.set_at((xc+x,yc+y),white)
screen.set_at((xc+x,yc-y),white)
screen.set_at((xc-x,yc-y),white)
screen.set_at((xc-x,yc+y),white)
if(p2>0):
x=x
y=y-1
p2=p2-2*pow(rx,2)*y+pow(rx,2)
else:
x=x+1
y=y-1
p2=p2+2*pow(ry,2)*x-2*pow(rx,2)*y+pow(rx,2)

def main():
while True:
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
sys.exit()

screen.fill(black)

draw_ellipse_midpoint(200,200,100,50)

pygame.display.flip()

if __name__=="__main__":
main()

#2D

import pygame
import sys
import math

pygame.init()
screen = pygame.display.set_mode((800,600))
pygame.display.set_caption("2D Transformation")

WHITE = (255,255,255)
BLACK = (0,0,0)

#Translation
def translation(x1,y1,x2,y2):
tx=50
ty=100
x1=x1+tx
y1=y1+ty
x2=x2+tx
y2=y2+ty
pygame.draw.line(screen,WHITE,(x1,y1),(x2,y2),2)

#Scaling
def scaling(x1,y1,x2,y2):
sx=2
sy=2
x1=x1*sx
y1=y1*sy
x2=x2*sx
y2=y2*sy
pygame.draw.line(screen,"RED",(x1,y1),(x2,y2),2)

#Rotation
def rotation(x1,y1,x2,y2):
theta = (22/(7*180))*20
x2 = x2*(math.cos(theta)) - y2*(math.sin(theta))
y2 = x2*(math.sin(theta)) + y2*(math.cos(theta))
pygame.draw.line(screen,"BLUE",(x1,y1),(x2,y2),2)

def main():
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
pygame.quit()
sys.exit()

screen.fill(BLACK)
pygame.draw.line(screen,WHITE,(200,200),(300,300),2)
translation(200,200,300,300)
scaling(175,175,250,250)
rotation(200,200,300,300)
pygame.display.flip()

if __name__ == "__main__":
main()

You might also like