from OpenGL.
GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
import sys
def plotpoints(x,y):
glBegin(GL_POINTS)
glVertex2f(x,y)
glEnd()
glFlush()
x1=float(input("enter x1:"))
y1=float(input("enter y1:"))
x2=float(input("enter x2:"))
y2=float(input("enter y2:"))
tx=float(input("tx coordinate"))
ty=float(input("ty coordinate"))
def line(x1,y1,x2,y2,rgb):
glColor3f(rgb[0],rgb[1],rgb[2])
glBegin(GL_LINES)
glVertex2f(x1,y1)
glVertex2f(x2,y2)
glEnd()
glFlush()
def translation_line(x1,y1,x2,y2,tx,ty):
rgb=(1.0,0.0,0.0)
line(x1,y1,x2,y2,rgb)
X1=x1+tx
Y1=y1+ty
X2=x2+tx
Y2=y2+ty
rgb=(0.0,1.0,0.0)
line(X1,Y1,X2,Y2,rgb)
def main():
translation_line(x1,y1,x2,y2,tx,ty)
glutInit(sys.argv)
glutInitDisplayMode(GLUT_RGB)
glutInitWindowSize(500,500)
glutInitWindowPosition(500,500)
glutCreateWindow("2D Translation")
glClearColor(1,1,1,1)
glOrtho(-500,500,-500,500, -500, 500)
glClear(GL_COLOR_BUFFER_BIT)
glColor3f(0,0,1)
glPointSize(10)
glutDisplayFunc(main)
glutMainLoop()
-----------------------------------------------------------------------------------------
import sys
import math
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
def plotPoints(x,y):
glBegin(GL_POINTS)
glVertex2f(x,y)
glEnd()
glFlush()
x1=int(input("Enter x1:"))
y1=int(input("Enter y1:"))
x2=int(input("Enter x2:"))
y2=int(input("Enter y2:"))
def mainfn():
ch=int(input("Enter which operation performing \n1.reference point \n2.About
orgin"))
if ch==1:
xr=int(input("Enter the x reference:"))
yr=int(input("Enter the y reference:"))
r=int(input("Enter the rotation angle:"))
bresenhams(x1,y1,x2,y2)
rotationref(x1,y1,x2,y2,xr,yr,r)
elif ch==2:
r=int(input("Enter the rotation angle:"))
bresenhams(x1,y1,x2,y2)
rotation(x1,y1,x2,y2,r)
def bresenhams(x1, y1, x2, y2):
dx = x2 - x1
dy = y2 - y1
abs_dx = abs(dx)
abs_dy = abs(dy)
if dx >= 0:
sx = 1
else:
sx = -1
if dy >= 0:
sy = 1
else:
sy = -1
if abs_dx >= abs_dy:
p = 2 * abs_dy - abs_dx
x = x1
y = y1
while x != x2:
plotPoints(x, y)
x += sx
if p < 0:
p += 2 * abs_dy
else:
p += 2 * (abs_dy - abs_dx)
y += sy
else:
p = 2 * abs_dx - abs_dy
x = x1
y = y1
while y != y2:
plotPoints(x, y)
y += sy
if p < 0:
p += 2 * abs_dx
else:
p += 2 * (abs_dx - abs_dy)
x += sx
def rotation(x1,y1,x2,y2,r):
r=(r*3.14)/180
xs=int(x1*math.cos(r)-y1*math.sin(r))
ys=int(x1*math.sin(r)+y1*math.cos(r))
xe=int(x2*math.cos(r)-y2*math.sin(r))
ye=int(x2*math.sin(r)+y2*math.cos(r))
glColor3f(0,1,0)
bresenhams(xs, ys, xe, ye)
def rotationref(x1,y1,x2,y2,xr,yr,r):
r=(r*3.14)/180
xs=int((x1-xr)*math.cos(r)-(y1-yr)*math.sin(r)+xr)
ys=int((x1-xr)*math.sin(r)+(y1-yr)*math.cos(r)+yr)
xe=int((x2-xr)*math.cos(r)-(y2-yr)*math.sin(r)+xr)
ye=int((x2-xr)*math.sin(r)+(y2-yr)*math.cos(r)+yr)
glColor3f(0,1,0)
bresenhams(xs, ys, xe, ye)
glutInit(sys.argv)
glutInitDisplayMode(GLUT_RGB)
glutInitWindowSize(500, 500)
glutInitWindowPosition(500,500)
glutCreateWindow('Transformation: rotation');
glClearColor(1, 1, 1, 1)
gluOrtho2D(-500, 500, -500, 500)
glClear(GL_COLOR_BUFFER_BIT)
glColor3f(0,0,0)
glPointSize(3)
glutDisplayFunc(mainfn)
glutMainLoop()
-------------------------------------------------------------------------------------------------
import sys
import math
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
def plotPoints(x1,y1,x2,y2,rgb):
glColor3f(rgb[0],rgb[1],rgb[2])
glBegin(GL_LINES)
glVertex2f(x1,y1)
glVertex2f(x2,y2)
glEnd()
glFlush()
def init():
glClearColor(1,1,1,1)
glPointSize(3)
glMatrixMode(GL_PROJECTION)
gluOrtho2D(-300,300,-300,300)
def clearscreen():
glClearColor(0.0,0.0,0.0,0.0)
gluOrtho2D(-500,500,-500,500)
glClear(GL_COLOR_BUFFER_BIT)
glPointSize(5.0)
def scaling_line(x1,y1,x2,y2,sx,sy,xr=0,yr=0):
rgb=(1.0,0.0,0.0)
plotPoints(x1,y1,x2,y2,rgb)
X1=(x1-xr)*sx+xr
X2=(x2-xr)*sx+xr
Y1=(y1-yr)*sy+yr
Y2=sx*(y2-yr)+yr
rgb=(0.0,1.0,0.0)
plotPoints(X1,Y1,X2,Y2,rgb)
def main():
glutInit(sys.argv)
glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE)
glutInitWindowPosition(100,100)
glutInitWindowSize(500,500)
x1=int(input("Enter x1: "))
y1=int(input("Enter y1: "))
x2=int(input("Enter x2: "))
y2=int(input("Enter y2: "))
sx=float(input("scaling factor of x"))
sy=float(input("scaling factor of y"))
ch = int(input("1. wrt origin\n2. wrt reference point"))
if(ch==1):
glutCreateWindow("2D scaliing")
glutDisplayFunc(lambda:scaling_line(x1,y1,x2,y2,sx,sy))
glutIdleFunc(lambda:scaling_line(x1,y1,x2,y2,sx,sy))
else:
xr=int(input("enter x coordinate of reference point"))
yr=int(input("enter y coordinate of reference point"))
glutCreateWindow("2D scaliing about reference")
glutDisplayFunc(lambda:scaling_line(x1,y1,x2,y2,sx,sy,xr,yr))
glutIdleFunc(lambda:scaling_line(x1,y1,x2,y2,sx,sy,xr,yr))
clearscreen()
glutMainLoop()
init()
glutMainLoop()
main()
-------------------------------------------------------------------------------------------------------
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
import sys
def plotpoint(x,y):
glBegin(GL_POINTS)
glVertex2f(x,y)
glEnd()
glFlush()
x1=int(input("x1="))
y1=int(input("y1="))
x2=int(input("x2="))
y2=int(input("y2="))
def dda(x1,y1,x2,y2):
dx=x2-x1
dy=y2-y1
steps=max(abs(dx),abs(dy))
xinc=dx/float(steps)
yinc=dy/float(steps)
x=x1
y=y1
for i in range(int(steps)):
plotpoint(round(x),round(y))
x+=xinc
y+=yinc
def main():
ch=int(input("1. along X axis 2. along Y axis 3.along x=y line 4.along y=-x line
5.along Origin "))
if ch==1:
dda(x1,y1,x2,y2)
glColor3f(1,0,1)
dda(x1,-y1,x2,-y2)
elif ch==2:
dda(x1,y1,x2,y2)
glColor3f(1,0,1)
dda(-x1,y1,-x2,y2)
elif ch==3:
dda(x1,y1,x2,y2)
glColor3f(1,0,1)
dda(y1,x1,y2,x2)
elif ch==4:
dda(x1,y1,x2,y2)
glColor3f(1,0,1)
dda(-y1,-x1,-y2,-x2)
elif ch==5:
dda(x1,y1,x2,y2)
glColor3f(1,0,1)
dda(-x1,-y1,-x2,-y2)
else:
print("Invalid")
glutInit(sys.argv)
glutInitDisplayMode(GLUT_RGB)
glutInitWindowPosition(0,0)
glutInitWindowSize(500,500)
glutCreateWindow('Reflection')
glClearColor(0,0,0,1)
glClear(GL_COLOR_BUFFER_BIT)
glColor3f(1,0,0)
gluOrtho2D(-300,300,-300,300)
glPointSize(3)
glutDisplayFunc(main)
glutMainLoop()
#correct in lab (ujjawal)
------------------------------------------------------------------------------------------------------------
-
////// ball
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
import sys
import math
WINDOW_SIZE=500
SCALE=100
xc=0
yc=80
r=8
c=8
def init_display():
glClear(GL_COLOR_BUFFER_BIT)
glColor3f(1,0,0)
glPointSize(5)
def polar_circle():
glBegin(GL_POINTS)
theta=0.0
while theta<=6.28:
x=(float(r)*math.cos(theta))+xc
y=(float(r)*math.sin(theta))+yc
glVertex2f(x/SCALE,y/SCALE)
theta+=0.001
glEnd()
glFlush()
ty = yc
tx = xc
count = 0
def wrap():
glClear(GL_COLOR_BUFFER_BIT)
polar_circle()
def update(n):
global xc,yc,r,ty,tx,count,c
dec = 10
if count==0:
yc -= 1.5
if yc<(-98):
count = 1
c-=1
if count==1:
yc += 1.5
if yc>ty-dec and count==1:
count = 0
ty = ty-dec
if c==0:
yc=-100
count=-1
glutTimerFunc(int(1000/60),update,0)
glutPostRedisplay()
def main():
glutInit(sys.argv)
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB)
glutInitWindowSize(WINDOW_SIZE,WINDOW_SIZE)
glutInitWindowPosition(50,50)
glutCreateWindow(b"Animation 1")
init_display()
glutDisplayFunc(wrap)
glutTimerFunc(0,update,0)
glutMainLoop()
main()
------------------------------------------------------------------------------------------------------------
-----
//////fan
from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *
import math
angle = 0
def draw_circle(radius, num_segments):
glBegin(GL_TRIANGLE_FAN)
glColor3f(0.0, 0.0,1.0)
glVertex2f(0, 0)
for i in range(num_segments + 1):
theta = 2.0 * math.pi * i / num_segments
x = radius * math.cos(theta)
y = radius * math.sin(theta)
glVertex2f(x, y)
glEnd()
def fan_blade():
glBegin(GL_TRIANGLES)
glColor3f(1, 0, 0)
glVertex2f(0, 0)
glVertex2f(0.1, 0.5)
glVertex2f(-0.1, 0.5)
glEnd()
def draw_fan():
global angle
glPushMatrix()
glRotatef(angle, 0, 0, 1)
for i in range(4):
glPushMatrix()
glRotatef(i * 90, 0, 0, 1)
fan_blade()
glPopMatrix()
glPopMatrix()
draw_circle(0.1, 100)
def display():
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
draw_fan()
glutSwapBuffers()
def timer(value):
global angle
angle += 2
if angle >= 360:
angle = 0
glutPostRedisplay()
glutTimerFunc(30, timer, 0)
def init():
glClearColor(0.0, 0.0, 0.0, 1.0)
glColor3f(1.0, 1.0, 1.0)
glPointSize(5)
glLineWidth(2)
glMatrixMode(GL_PROJECTION)
glLoadIdentity()
gluOrtho2D(-1, 1, -1, 1)
def main():
glutInit(sys.argv)
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH)
glutInitWindowSize(800, 600)
glutCreateWindow('Rotating Fan with Circle')
init()
glutDisplayFunc(display)
glutTimerFunc(0, timer, 0)
glutMainLoop()
main()
# correct (ujjawal)
-------------------------------------------------------------------------------------------------------
// car
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
import sys
import math
WINDOW_SIZE=500
GLOBAL_X=0.0
GLOBAL_Y=0.0
FPS=60
def init():
glClearColor(1.0,1.0,1.0,1.0)
gluOrtho2D(-WINDOW_SIZE,WINDOW_SIZE,-
WINDOW_SIZE,WINDOW_SIZE)
def drawCircle(x,y,s):
i=0.0
if s==0:
y=y-100-35
x=x-50
else:
y=y-100-35
x=x+50
glBegin(GL_TRIANGLE_FAN)
glVertex2f(x,y)
for i in range(0,361,1):
glVertex2f(35*math.cos(math.pi*i/180.0)+x,35*math.sin(math.pi*i/180.0)+y)
glEnd()
def drawRectangle(x,y):
glBegin(GL_QUADS)
glVertex2f(x-100,y+50)
glVertex2f(x+100,y+50)
glVertex2f(x+100,y-100)
glVertex2f(x-100,y-100)
glEnd()
def drawCar():
global GLOBAL_X
global GLOBAL_Y
global DIR
glClear(GL_COLOR_BUFFER_BIT)
glColor3f(0.0,1.0,0.0)
drawRectangle(GLOBAL_X,GLOBAL_Y)
drawCircle(GLOBAL_X,GLOBAL_Y,0)
drawCircle(GLOBAL_X,GLOBAL_Y,1)
glutSwapBuffers()
def animate(temp):
global WINDOW_SIZE
global GLOBAL_X
global GLOBAL_Y
glutPostRedisplay()
glutTimerFunc(int(1000/FPS),animate,int(0))
if(GLOBAL_X+100<WINDOW_SIZE):
GLOBAL_X=GLOBAL_X+1
else:
GLOBAL_X=-400
def main():
glutInit(sys.argv)
glutInitWindowSize(WINDOW_SIZE,WINDOW_SIZE)
glutInitWindowPosition(0,0)
glutInitDisplayMode(GLUT_RGB)
glutCreateWindow("Car")
glutDisplayFunc(drawCar)
glutTimerFunc(0,animate,0)
glutIdleFunc(drawCar)
init()
glutMainLoop()
main()
----------------------------------------------------------------------------------------------------
////// dda
from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *
def init():
glClearColor(10,10,10,1.0)
gluOrtho2D(-300,300,-300,300)
def plotLine(x1,y1,x2,y2):
deltaX=x2-x1
deltaY=y2-y1
steps=0
if(abs(deltaX)>abs(deltaY)):
steps=abs(deltaX)
else:
steps=abs(deltaY)
Xincrement=deltaX/steps
Yincrement=deltaY/steps
glClear(GL_COLOR_BUFFER_BIT)
glColor3f(1.0,0.0,1)
glPointSize(10.0)
glBegin(GL_POINTS)
for step in range(1,steps+1):
glVertex2f(round(x1),round(y1))
x1=x1+Xincrement
y1=y1+Yincrement
glEnd()
glFlush()
def main():
print("Enter coordinates: ")
x1=int(input("Enter x1: "))
y1=int(input("Enter y1: "))
x2=int(input("Enter x2: "))
y2=int(input("Enter y2: "))
glutInit(sys.argv)
glutInitDisplayMode(GLUT_RGB)
glutInitWindowSize(500,500)
glutInitWindowPosition(0,0)
glutCreateWindow("Plot Line using DDA")
glutDisplayFunc(lambda:plotLine(x1,y1,x2,y2))
glutIdleFunc(lambda:plotLine(x1,y1,x2,y2))
init()
glutMainLoop()
main()
------------------------------------------------------------------------------------
/////// bresen
import sys
from OpenGL.GL import*
from OpenGL.GLU import*
from OpenGL.GLUT import*
def plotPoints(x,y):
glBegin(GL_POINTS)
glVertex2f(x,y)
glEnd()
glFlush()
x1=int(input("Enter x1 "))
y1=int(input("Enter y1 "))
x2=int(input("Enter x2 "))
y2=int(input("Enter y2 "))
def wrapper():
bresenhams(x1,y1,x2,y2)
def bresenhams(x1,y1,x2,y2):
dx=x2-x1
dy=y2-y1
p= 2*dy - dx
x=x1
y=y1
while( x<=x2):
plotPoints(x,y)
x+=1
if p<0:
p+=2*dy
else:
p+= 2*dy - 2*dx
if y2>=y1:
y+=1
else:
y-=1
glutInit(sys.argv)
glutInitDisplayMode(GLUT_RGB)
glutInitWindowSize(800,800)
glutInitWindowPosition(800,300)
glutCreateWindow('Bresenhams')
glClearColor(1,1,1,1)
gluOrtho2D(-500,500,-500,500)
glClear(GL_COLOR_BUFFER_BIT)
glColor3f(0,0,0)
glPointSize(3)
glutDisplayFunc(wrapper)
glutMainLoop()
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
import sys
import math
WINDOW_SIZE = 500
SCALE = 100
xc = yc = 0
r=1
def init_display():
glClear(GL_COLOR_BUFFER_BIT)
glColor3f(1,0,0)
glPointSize(5)
def midpoint_circle():
glBegin(GL_POINTS)
global xc,yc,r
p = 1-r
plot_symmetric_points(x,y)
while x<y:
x += 1
if p<0:
p += 2*x + 1
else:
y-=1
p += 2*(x-y)+1
plot_symmetric_points(x,y)
glEnd()
glFlush()
def polar_circle():
glBegin(GL_POINTS)
theta = 0.0
while theta <= 6.28:
x = float(r)*math.cos(theta)
y = float(r)*math.sin(theta)
glVertex2f(x/SCALE,y/SCALE)
theta += 0.001
glEnd()
glFlush()
def nonpolar_circle():
global xc,yc,r
glBegin(GL_POINTS)
x,y = xc,r
plot_symmetric_points(x-xc,y)
while x<(xc+r):
x = x+1
y = math.sqrt(float(r*r)-((x-xc)*(x-xc)))
plot_symmetric_points(x-xc,y)
glEnd()
glFlush()
def no_circle():
pass
def plot_symmetric_points(x,y):
global xc,yc
glVertex2f((xc+x)/SCALE,(yc+y)/SCALE)
glVertex2f((xc+x)/SCALE,(yc-y)/SCALE)
glVertex2f((xc-x)/SCALE,(yc+y)/SCALE)
glVertex2f((xc-x)/SCALE,(yc-y)/SCALE)
glVertex2f((xc+y)/SCALE,(yc+x)/SCALE)
glVertex2f((xc+y)/SCALE,(yc-x)/SCALE)
glVertex2f((xc-y)/SCALE,(yc+x)/SCALE)
glVertex2f((xc-y)/SCALE,(yc-x)/SCALE)
def main():
glutInit(sys.argv)
glutInitDisplayMode(GLUT_SINGLE)
glutInitWindowSize(250,250)
glutInitWindowPosition(50,50)
global xc,yc,r
xc = int(input("Enter the x Co-ordinate!"))
yc = int(input("Enter the y Co-ordinate!"))
r = int(input("Enter the radius for the circle!"))
choice = int(input("1 - Midpoint Algorithm\n2 - Polar Algorithm\n 3 - Non-Polar
Algorithm")
glutCreateWindow("Circle Drawing Algorithm)
init_display()
if choice==1:
glutDisplayFunc(midpoint_circle)
elif choice==1:
glutDisplayFunc(polar_circle)
elif choice==2:
glutDisplayFunc(nonpolar_circle)
else:
glutDisplayFunc(no_circle)
////////////////////////////////////////////////////////////////////////////////////////////////
from OpenGL.GL import *
from OpenGL.GLU import *
from OpenGL.GLUT import *
import sys
import math
WINDOW_SIZE = 500
SCALE = 100
xc=yc=0
rx=ry=1
def polar_ellipse():
glClear(GL_COLOR_BUFFER_BIT)
glColor3f(1,0,0)
glPointSize(5)
glBegin(GL_POINTS)
global xc,yc,rx,ry
theta=0.0
while theta<=1.57:
x=float(rx)*math.cos(theta)
y=float(ry)*math.sin(theta)
plot_symmetric_points(x,y)
theta+=0.001
glEnd()
glFlush()
def nonpolar_ellipse():
glClear(GL_COLOR_BUFFER_BIT)
glColor3f(1,0,0)
glPointSize(5)
glBegin(GL_POINTS)
global xc,yc,rx,ry
x=0
while x<=rx:
y=ry*(math.sqrt(1-(((x*x)/(rx*rx)))))
plot_symmetric_points(x,y)
x+=0.01
glEnd()
glFlush()
def plot_symmetric_points(x,y):
global xc,yc
glVertex2f((xc+x)/SCALE,(yc+y)/SCALE)
glVertex2f((xc+x)/SCALE,(yc-y)/SCALE)
glVertex2f((xc-x)/SCALE,(yc+y)/SCALE)
glVertex2f((xc-x)/SCALE,(yc-y)/SCALE)
def no_plot():
pass
def main():
glutInit(sys.argv)
glutInitDisplayMode(GLUT_RGB)
glutInitWindowSize(500,500)
glutInitWindowPosition(0,0)
global xc,yc,rx,ry
xc=int(input("Enter x coordinate of the centre: "))
yc=int(input("Enter y coordinate of the centre: "))
rx=int(input("Enter the length of semi major axis: "))
ry=int(input("Enter the length of the semi minor: "))
choice=int(input("Enter the option\n1)Polar Ellipse Algorithm\n2)Non Polar
Ellipse Algorithm\n"))
if choice==1:
glutCreateWindow("Polar Ellipse Drawing Algorithm")
glutDisplayFunc(polar_ellipse)
elif choice==2:
glutCreateWindow("Non polar ellipse drawing algorithm")
glutDisplayFunc(nonpolar_ellipse)
else:
print("Invalid option")
glutCreateWindow("Invalid Option")
glutDisplayFunc(no_plot)
glutMainLoop()
main()