You are on page 1of 9

#####################################################

##########################################
#ASSIGNMENT 1:scan conversion of circle
#NAME:- YADWINDER KAUR
#ROLL NO:- 21071273
#####################################################
#####################################

Purpose: To implement scan conversion of ellipse with given


centre and axes using following
algorithms
1. Direct
2. Polar
3. Polar incremental
4. Mid-point
Input:Centre (ℎ, K) and axes a and b
#direct
from graphics import*
import math
win=GraphWin("Assignment 3",700,500)
win.setBackground("white")
def plotEllipsepixel(h,k,x,y,color):
win.plotPixel(h+x,k+y,"red")
win.plotPixel(h-x,k+y,"black")
win.plotPixel(h-x,k-y,"blue")
win.plotPixel(h+x,k-y,"brown")
maxx=win.getWidth()
maxy=win.getHeight()
midx=maxx/2
midy=maxy/2
l1=Line(Point(midx,0),Point(midx,maxy))
l1.setFill("black")
l1.draw(win)
l2=Line(Point(0,midy),Point(maxx,midy))
l2.setFill("black")
l2.draw(win)
t1=Text(Point(30,midy-10),"-x axes")
t1.setFill("black")
t1.draw(win)
t2=Text(Point(midx+300,midy-10),"x axes")
t2.setFill("black")
t2.draw(win)
t3=Text(Point(midx+30,midy+230),"-y axes")
t3.setFill("black")
t3.draw(win)
t4=Text(Point(midx+30,10),"y axes")
t4.setFill("black")
t4.draw(win)
t4=Text(Point(midx-4,midy+4),"0")
t4.setFill("black")
t4.draw(win)
a=200
b=150
h=midx
k=midy
x=0
y=b
while(b*b*x<=a*a*y):
x=x+1
y=b*math.sqrt(1-((x*x)/(a*a)))
plotEllipsepixel(h,k,x,round(y),"black")
y=round(y)
while(y>=0):
y=y-1
x=a*math.sqrt(1-(y*y)/(b*b))
plotEllipsepixel(h,k,round(x),y,"black")
time.sleep(0.005)
win.getMouse()
win.close()
#mid point
from graphics import*
import math
win=GraphWin("Assignment 3",700,500)
win.setBackground("white")
def plotEllipsepixel(h,k,x,y,color):
win.plotPixel(h+x,k+y,"red")
win.plotPixel(h-x,k+y,"black")
win.plotPixel(h-x,k-y,"blue")
win.plotPixel(h+x,k-y,"brown")
maxx=win.getWidth()
maxy=win.getHeight()
midx=maxx/2
midy=maxy/2
l1=Line(Point(midx,0),Point(midx,maxy))
l1.setFill("black")
l1.draw(win)
l2=Line(Point(0,midy),Point(maxx,midy))
l2.setFill("black")
l2.draw(win)
t1=Text(Point(30,midy-10),"-x axes")
t1.setFill("black")
t1.draw(win)
t2=Text(Point(midx+300,midy-10),"x axes")
t2.setFill("black")
t2.draw(win)
t3=Text(Point(midx+30,midy+230),"-y axes")
t3.setFill("black")
t3.draw(win)
t4=Text(Point(midx+30,10),"y axes")
t4.setFill("black")
t4.draw(win)
t4=Text(Point(midx-4,midy+4),"0")
t4.setFill("black")
t4.draw(win)
a=200
h=0
k=0
a=200
b=150
x=0
y=b
h=midx+h
k=midy-k
p=(b*b)-(a*a*b)+((a*a)/4)
plotEllipsepixel(h,k,x,y,"black")
while(b*b*x<=a*a*y):
if(p<0):
p=p+(b*b)*(2*x+3)
else:
p=p+(b*b)*(2*x+3)+(a*a)*(-2*y+3)
y=y-1
x=x+1
plotEllipsepixel(h,k,x,y,"black")
p=(b*b)*(x+0.5)*(x+0.5)+(a*a)*(y-1)-a*a*b*b
p=round(p)
while(y>=0):
if(p<0):
p=p+(b*b)*(2*x+2)+(a*a)*(-2*y-3)
x=x+1
else:
p=p+(a*a)*(-2*y+3)
y=y+1
plotEllipsepixel(h,k,round(x),round(y),"grey")
time.sleep(0.005)
win.getMouse()
win.close()
#polar method
from graphics import*
import math
win=GraphWin("Assignment 3",700,500)
win.setBackground("white")
def plotEllipsepixel(h,k,x,y,color):
win.plotPixel(h+x,k+y,"red")
win.plotPixel(h-x,k+y,"black")
win.plotPixel(h-x,k-y,"blue")
win.plotPixel(h+x,k-y,"brown")
maxx=win.getWidth()
maxy=win.getHeight()
midx=maxx/2
midy=maxy/2
l1=Line(Point(midx,0),Point(midx,maxy))
l1.setFill("black")
l1.draw(win)
l2=Line(Point(0,midy),Point(maxx,midy))
l2.setFill("black")
l2.draw(win)
t1=Text(Point(30,midy-10),"-x axes")
t1.setFill("black")
t1.draw(win)
t2=Text(Point(midx+300,midy-10),"x axes")
t2.setFill("black")
t2.draw(win)
t3=Text(Point(midx+30,midy+230),"-y axes")
t3.setFill("black")
t3.draw(win)
t4=Text(Point(midx+30,10),"y axes")
t4.setFill("black")
t4.draw(win)
t4=Text(Point(midx-4,midy+4),"0")
t4.setFill("black")
t4.draw(win)
a=200
h=0
k=0
a=200
b=150
x=a
y=0
theta=0
h=midx+h
k=midy+k
if(a<b):
dtheta=1/a
else:
dtheta=1/b
plotEllipsepixel(h,k,x,y,"black")
while(y<=b):
theta=theta+dtheta
x=a*math.cos(theta)
y=b*math.sin(theta)
plotEllipsepixel(h,k,round(x),round(y),"black")
time.sleep(0.005)
win.getMouse()
win.close()
#polar increment
from graphics import*
import math
win=GraphWin("Assignment 3",700,500)
win.setBackground("white")
def plotEllipsepixel(h,k,x,y,color):
win.plotPixel(h+x,k+y,"red")
win.plotPixel(h-x,k+y,"black")
win.plotPixel(h-x,k-y,"blue")
win.plotPixel(h+x,k-y,"brown")
maxx=win.getWidth()
maxy=win.getHeight()
midx=maxx/2
midy=maxy/2
l1=Line(Point(midx,0),Point(midx,maxy))
l1.setFill("black")
l1.draw(win)
l2=Line(Point(0,midy),Point(maxx,midy))
l2.setFill("black")
l2.draw(win)
t1=Text(Point(30,midy-10),"-x axes")
t1.setFill("black")
t1.draw(win)
t2=Text(Point(midx+300,midy-10),"x axes")
t2.setFill("black")
t2.draw(win)
t3=Text(Point(midx+30,midy+230),"-y axes")
t3.setFill("black")
t3.draw(win)
t4=Text(Point(midx+30,10),"y axes")
t4.setFill("black")
t4.draw(win)
t4=Text(Point(midx-4,midy+4),"0")
t4.setFill("black")
t4.draw(win)
a=200
h=0
k=0
a=200
b=150
x=a
y=0
theta=0
h=midx+h
k=midy+k
if(a<b):
dtheta=1/a
else:
dtheta=1/b
c=math.cos(dtheta)
s=math.sin(dtheta)
sab=(a/b)*s
sba=(b/a)*s
plotEllipsepixel(h,k,x,y,"black")
while(y<=b):
xtemp=x
x=x*c-sab*y
y=sba*xtemp+c*y
plotEllipsepixel(h,k,round(x),round(y),"black")
time.sleep(0.005)
win.getMouse()
win.close()

You might also like