diff --git a/cg-lab/animation/boat_paddle.py b/cg-lab/animation/boat_paddle.py new file mode 100644 index 0000000..2961091 --- /dev/null +++ b/cg-lab/animation/boat_paddle.py @@ -0,0 +1,92 @@ +from OpenGL.GL import * +from OpenGL.GLU import * +from OpenGL.GLUT import * +import sys +import math +WINDOWSIZE=500 +x=0 +y=0 +radius=70 +angle=0 +dir=0 + +def Init(): + glClearColor(1.0,1.0,1.0,1.0) + gluOrtho2D(-WINDOWSIZE,WINDOWSIZE,-WINDOWSIZE,WINDOWSIZE) + +def body(): + glColor3f(1.0,0.0,0.0) + glBegin(GL_POLYGON) + glVertex2f(x-100,y) + glVertex2f(x+100,y) + glVertex2f(x+90,y-40) + glVertex2f(x-90,y-40) + glEnd() + + glColor3f(0.0,0.0,0.0) + glBegin(GL_POLYGON) + glVertex(x+10,y+50) + glVertex(x-10,y+50) + glVertex(x-30,y) + glVertex(x+30,y) + glEnd() + glPointSize(10) + glBegin(GL_POINTS) + glVertex2f(x,y+80) + glEnd() + + glColor3f(0.1,0.7,1.0) + glBegin(GL_POLYGON) + glVertex2f(-500,-40) + glVertex2f(-500,-500) + glVertex2f(500,-500) + glVertex2f(500,-40) + glEnd() + +def hand(): + glColor3f(0.0,1.0,0.0) + glLineWidth(5) + glBegin(GL_LINES) + glVertex2f(x,y) + x1=(-70)*math.cos(math.radians(angle))+(-70)*math.sin(math.radians(angle))+x + y1=-(-70)*math.sin(math.radians(angle))+(-70)*math.cos(math.radians(angle))+y + glVertex2f(x1,y1) + glEnd() + +def drawBoat(): + glClear(GL_COLOR_BUFFER_BIT) + body() + hand() + glutSwapBuffers() + +def animate(temp): + global x,y,angle,dir + glutPostRedisplay() + glutTimerFunc(int(1000/60),animate,int(0)) + if angle>30: + dir=1 + if angle<-90: + dir=0 + + if(dir==1): + angle=angle-1 + else: + angle=angle+1 + x=x-1 + if x==(-WINDOWSIZE+100): + x=400 + + +def main(): + glutInit(sys.argv) + glutInitWindowSize(WINDOWSIZE,WINDOWSIZE) + glutInitWindowPosition(0,0) + glutInitDisplayMode(GLUT_RGBA) + glutCreateWindow("Boat") + glutDisplayFunc(drawBoat) + glutTimerFunc(0,animate,0) + glutIdleFunc(drawBoat) + Init() + glutMainLoop() + +main() diff --git a/cg-lab/animation/car-stop.py b/cg-lab/animation/car-stop.py new file mode 100644 index 0000000..d66c04b --- /dev/null +++ b/cg-lab/animation/car-stop.py @@ -0,0 +1,76 @@ +from OpenGL.GL import * +from OpenGL.GLU import * +from OpenGL.GLUT import * +import sys +sys.setrecursionlimit(100000) +WS = 900 +x,y=0,0 +ps = 5 +fillcolor = [1,0,0] + +tx,ty=0,0 + +def getpixel(x,y): + return glReadPixels(x,y,1,1,GL_RGB,GL_FLOAT,None)[0][0] + +def setpixel(x,y,fillcolor): + glColor3f(*fillcolor) + glPointSize(ps) + glBegin(GL_POINTS) + glVertex2f(x,y) + glEnd() + glFlush() + + + +def flood(x,y,fillcolor,oldcolor): + if all (getpixel(x,y)==oldcolor): + setpixel(x,y,fillcolor) + flood(x+ps,y,fillcolor,oldcolor) + flood(x,y+ps,fillcolor,oldcolor) + flood(x-ps,y,fillcolor,oldcolor) + flood(x,y-ps,fillcolor,oldcolor) + + + + +def draw(): + glClear(GL_COLOR_BUFFER_BIT) + print("draw func") + glColor3f(0,1,0) + glLineWidth(1) + glBegin(GL_POLYGON) + glVertex2f(100+tx,400+ty) + glVertex2f(300+tx,500+ty) + glVertex2f(550+tx,400+ty) + glVertex2f(550+tx,350+ty) + glVertex2f(150+tx,350+ty) + glEnd() + glFlush() + + + +def animate(temp): + global tx,ty + print("animate func") + glutPostRedisplay() + glutTimerFunc(1000,animate,0) + tx+=10 + ty+=0 + +def click(button,state,x,y): + if button == GLUT_LEFT_BUTTON and state == GLUT_DOWN: + flood(x,y,fillcolor,getpixel(x,y)) + +def main(): + glutInit(sys.argv) + glutInitWindowSize(WS,WS) + glutCreateWindow("car stop") + gluOrtho2D(0,WS,0,WS) + + glutDisplayFunc(draw) + glutMouseFunc(click) + glutTimerFunc(0,animate,0) + glutMainLoop() + +main() diff --git a/cg-lab/animation/car.py b/cg-lab/animation/car.py new file mode 100644 index 0000000..9d48927 --- /dev/null +++ b/cg-lab/animation/car.py @@ -0,0 +1,120 @@ +""" +CAR PROGRAM + +ABHINAND D MANOJ + +""" +from OpenGL.GL import * +from OpenGL.GLU import * +from OpenGL.GLUT import * +import math +import sys +import random +from pygame import mixer + +mixer.init() +plays=mixer.Sound('audio.mp3') + +x=0 +y=0 +t=0 +yr=100 +tr=1 + +def init(): + glClearColor(0,0,0,1) + gluOrtho2D(-500,500,-500,500) + +def drawcir(q): + glColor3f(0,1,1) + glLineWidth(2) + glBegin(GL_TRIANGLE_FAN) + glVertex2f(q,0) + for i in range(0,361,1): + glVertex2f(q+20*math.cos(math.pi*i/180),20*math.sin(math.pi*i/180)) + glEnd() + glColor3f(0,0,0) + glBegin(GL_LINES) + glVertex2f(q+20*math.cos(t)-(0)*math.sin(t),20*math.sin(t)+(0)*math.cos(t)) + glVertex2f(q-20*math.cos(t)-(0)*math.sin(t),-20*math.sin(t)+(0)*math.cos(t)) + glEnd() + glBegin(GL_LINES) + glVertex2f(q+0*math.cos(t)-(20)*math.sin(t),0+0*math.sin(t)+(20)*math.cos(t)) + glVertex2f(q+0*math.cos(t)-(-20)*math.sin(t),0+0*math.sin(t)+(-20)*math.cos(t)) + glEnd() + +def draw(): + global x,y + glClear(GL_COLOR_BUFFER_BIT) + glColor3f(1,0,0) + glBegin(GL_QUADS) + glVertex2f(x,y) + glVertex2f(x+200,y) + glVertex2f(x+160,y+40) + glVertex2f(x,y+40) + glEnd() + glBegin(GL_QUADS) + glVertex2f(x+10,y+40) + glVertex2f(x+10,y+80) + glVertex2f(x+155,y+80) + glVertex2f(x+155,y+40) + glEnd() + drawcir(x+25) + drawcir(x+155) + glutSwapBuffers() + +def animate(value): + global x,y,t,tr,yr + if tr==1: + if x<500: + x=x+1 + else: + x=-500 + if t>-math.radians(360): + t-=0.01 + else: + t=0.00 + elif tr==0: + if x>-500: + x=x-1 + else: + x=500 + if t-math.radians(360): + t-=0.01 + else: + t=0.00 + elif tr==0: + if x>-500: + x=x-1 + else: + x=500 + if t=360): + THETA[0]=0 + THETA[1]+=1 + else: + THETA[0]+=0.098 + if(THETA[1]>=360): + THETA[1]=0 + THETA[2]+=1 + if(THETA[2]>=360): + THETA[2]=0 + +def main(): + glutInit(sys.argv) + glutInitWindowPosition(0,0) + glutInitWindowSize(WINDOW_SIZE,WINDOW_SIZE) + glutInitDisplayMode(GLUT_RGB) + glutCreateWindow("Clock") + glutDisplayFunc(drawClock) + glutTimerFunc(0,animate,0) + init() + glutMainLoop() +main() diff --git a/cg-lab/animation/clock_animation.py b/cg-lab/animation/clock_animation.py new file mode 100644 index 0000000..c9ad925 --- /dev/null +++ b/cg-lab/animation/clock_animation.py @@ -0,0 +1,78 @@ +''' +Afeef Mohammed +pgrm Animation_clock +''' + +from OpenGL.GL import * +from OpenGL.GLU import * +from OpenGL.GLUT import * +import math +import sys + +h=0 +m=0 +s=0 + +def init(): + gluOrtho2D(-500,500,-500,500) + +def draw(): + glClear(GL_COLOR_BUFFER_BIT) + glColor3f(1,1,1) + glLineWidth(5) + glBegin(GL_POINTS) + theta=0.0 + while theta<=6.28: + x=float(200)*math.cos(theta) + y=float(200)*math.sin(theta) + glVertex2f(x,y) + theta+=0.001 + glEnd() + glColor3f(1,0,1) + glLineWidth(0.5) + glBegin(GL_LINES) + glVertex2f(0,0) + glVertex2f(-200*math.sin(s),200*math.cos(s)) + glEnd() + glColor3f(1,0,1) + glLineWidth(1) + glBegin(GL_LINES) + glVertex2f(0,0) + glVertex2f(-150*math.sin(m),150*math.cos(m)) + glEnd() + glColor3f(1,0,1) + glLineWidth(2) + glBegin(GL_LINES) + glVertex2f(0,0) + glVertex2f(-100*math.sin(h),100*math.cos(h)) + glEnd() + glutSwapBuffers() + + +def animate(temp): + global h,m,s + if s>-6.2832: + s-=0.01 + else: + s=0.00 + m-=0.01472 + if m<-6.2832: + m=0.00 + h-=0.01472 + if h<-6.2832: + h=0.00 + glutPostRedisplay() + glutTimerFunc(10,animate,10) + +def main(): + glutInit(sys.argv) + glutInitDisplayMode(GLUT_RGBA) + glutInitWindowSize(500,500) + glutInitWindowPosition(600,0) + glutCreateWindow("SAMPLE") + glutDisplayFunc(lambda : draw()) + glutTimerFunc(0,animate,0) + init() + glutMainLoop() +main() + diff --git a/cg-lab/animation/clock_animation_2.py b/cg-lab/animation/clock_animation_2.py new file mode 100644 index 0000000..517558f --- /dev/null +++ b/cg-lab/animation/clock_animation_2.py @@ -0,0 +1,89 @@ +''' +########################################### +########################################### +#### #### +#### NAME :ANGELO ANTU #### +#### CLASS : CSA #### +#### EXPERIMENT NO: 16 #### +#### EXPERIMENT NAME: CLOCK #### +#### #### +########################################### +########################################### + +''' + +from OpenGL.GL import * +from OpenGL.GLU import * +from OpenGL.GLUT import * +import math +import sys + +h=0 +m=0 +s=0 + +def init(): + gluOrtho2D(-500,500,-500,500) + +def draw(): + glClear(GL_COLOR_BUFFER_BIT) + glColor3f(1,1,0.1) + glLineWidth(5) + glBegin(GL_POINTS) + theta=0.0 + while theta<=6.28: + x=float(200)*math.cos(theta) + y=float(200)*math.sin(theta) + glVertex2f(x,y) + theta+=0.001 + glEnd() + glColor3f(0.8,0.8,1) + glLineWidth(5) + glBegin(GL_LINES) + glVertex2f(0,0) + glVertex2f(-200*math.sin(s),200*math.cos(s)) + glEnd() + glColor3f(0.7,0.7,1) + glLineWidth(5) + glBegin(GL_LINES) + glVertex2f(0,0) + glVertex2f(-150*math.sin(m),150*math.cos(m)) + glEnd() + glColor3f(0.5,0.5,1) + glLineWidth(4) + glBegin(GL_LINES) + glVertex2f(0,0) + glVertex2f(-100*math.sin(h),100*math.cos(h)) + glEnd() + glutSwapBuffers() + + +def animate(temp): + global h,m,s + if s>-6.2832: + s-=0.01 + else: + s=0.00 + m-=0.01472 + if m<-6.2832: + m=0.00 + h-=0.01472 + if h<-6.2832: + h=0.00 + glutPostRedisplay() + glutTimerFunc(10,animate,10) + +def main(): + glutInit(sys.argv) + glutInitDisplayMode(GLUT_RGBA) + glutInitWindowSize(500,500) + glutInitWindowPosition(600,0) + glutCreateWindow("SAMPLE") + glutDisplayFunc(lambda : draw()) + glutTimerFunc(0,animate,0) + init() + glutMainLoop() +main() + + + diff --git a/cg-lab/animation/cr3.py b/cg-lab/animation/cr3.py deleted file mode 100644 index ed336b2..0000000 --- a/cg-lab/animation/cr3.py +++ /dev/null @@ -1,43 +0,0 @@ -from OpenGL.GL import * -from OpenGL.GLUT import * -from OpenGL.GLU import * - -window = 0 # glut window number -width, height = 500, 500 # window size -field_width, field_height = 50, 50 # internal resolution - -def refresh2d_custom(width, height, internal_width, internal_height): - glViewport(0, 0, width, height) - glMatrixMode(GL_PROJECTION) - glLoadIdentity() - glOrtho(0.0, internal_width, 0.0, internal_height, 0.0, 1.0) - glMatrixMode (GL_MODELVIEW) - glLoadIdentity() - -def draw_rect(x, y, width, height): - glBegin(GL_QUADS) # start drawing a rectangle - glVertex2f(x, y) # bottom left point - glVertex2f(x + width, y) # bottom right point - glVertex2f(x + width, y + height) # top right point - glVertex2f(x, y + height) # top left point - glEnd() # done drawing a rectangle - -def draw(): # draw is called all the time - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) # clear the screen - glLoadIdentity() # reset position - refresh2d_custom(width, height, field_width, field_height) - - # TODO draw things - - glutSwapBuffers() # important for double buffering - - -# initialization -glutInit() # initialize glut -glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_ALPHA | GLUT_DEPTH) -glutInitWindowSize(width, height) # set window size -glutInitWindowPosition(0, 0) # set window position -window = glutCreateWindow("noobtuts.com") # create window with title -glutDisplayFunc(draw) # set draw function callback -glutIdleFunc(draw) # draw all the time -glutMainLoop() \ No newline at end of file diff --git a/cg-lab/animation/crow.py b/cg-lab/animation/crow.py deleted file mode 100644 index e412db4..0000000 --- a/cg-lab/animation/crow.py +++ /dev/null @@ -1,72 +0,0 @@ -from OpenGL.GL import * -from OpenGL.GLU import * -from OpenGL.GLUT import * -import math - -window_size = 800 -x_ref = 0 -y_ref = 0 - - -def clear_screen(): - glClearColor(0, 0, 0, 1) - gluOrtho2D(-window_size, window_size, -window_size, window_size) - # glClear(GL_COLOR_BUFFER_BIT) - - -def circle(x, y, r): - glColor3f(1, 1, 0) - glPointSize(1) - glBegin(GL_LINES) - for i in range(0, 360, 1): - glVertex2f(x, y) - glVertex2f(x + r * math.cos(math.radians(i)), y + r * math.sin(math.radians(i))) - glEnd() - - -def hand(): - global x_ref, y_ref - glLineWidth(5) - glClear(GL_COLOR_BUFFER_BIT) - glColor3f(1, 1, 0) - x = 200 + 200 * abs(math.cos(math.radians(x_ref))) - y = 300 * math.sin(math.radians(x_ref)) - - glBegin(GL_LINES) - glVertex2f(0, 0) - glVertex2f(x, y) - glEnd() - - glBegin(GL_LINES) - glVertex2f(0, 0) - glVertex2f(-x, y) - glEnd() - - circle(x, y, 20) - circle(-x, y, 20) - - -def man(): - hand() - - -def display(): - global x_ref, y_ref - x_ref += .05 - man() - - glFlush() - - -def main(): - glutInit() - glutInitWindowSize(window_size, window_size) - glutCreateWindow("Point") - glutDisplayFunc(display) - glutIdleFunc(display) - clear_screen() - glutMainLoop() - - -if __name__ == '__main__': - main() \ No newline at end of file diff --git a/cg-lab/animation/egg.py b/cg-lab/animation/egg.py new file mode 100644 index 0000000..8da07e8 --- /dev/null +++ b/cg-lab/animation/egg.py @@ -0,0 +1,227 @@ +from OpenGL.GL import * +from OpenGL.GLU import * +from OpenGL.GLUT import * +import sys,math,random +sys.setrecursionlimit(100000) + +WS = 900 # window size +timer = 1 +# egg +# crack +# fragments +# hatchling + +eggX,eggY = 300,300 +eggR = 100 +cY,cX = 0,0 + +frag = False + +x,y=0,0 +fragColor = [1,1,0.7] + +lineW = 1 +crackX,crackY = 0,0 + + +chickX,chickY = 0,0 + + +def egg(): + glColor3f(1,1,1) + glPointSize(2) + glLineWidth(2) + glBegin(GL_TRIANGLE_FAN) + glVertex2f(eggX,eggY) + for i in range(0,361,1): + glVertex2f(eggR*math.cos(3.14*i/180)+eggX, + eggR*math.sin(3.14*i/180)+eggY) + glEnd() + """ + glColor3f(1,1,0.7) + glPointSize(2) + glLineWidth(2) + glBegin(GL_TRIANGLE_FAN) + glVertex2f(eggX,eggY) + for i in range(0,361,1): + glVertex2f(98*math.cos(3.14*i/180)+eggX, + 98*math.sin(3.14*i/180)+eggY) + glEnd()""" + + +def fragments(fA1,fB1,fA2,fB2,fA3,fB3): + glColor3f(*fragColor) # note : eggish hue color + glPointSize(2) + glLineWidth(2) + glBegin(GL_TRIANGLES) + glVertex2f(fA1,fB1) + + glVertex2f(fA2,fB2) + glVertex2f(fA3,fB3) + glEnd() + +def crack(): #crA1,crB1,crA1,crB2 + glColor3f(0,0,0) + glPointSize(2) + glLineWidth(int(lineW)) + glBegin(GL_LINES) + #glVertex2f(crA1,crB1) + #glVertex2f(crA2,crB2) + glVertex2f(300,400) + glVertex2f(250,350) + + glVertex2f(250,350) + glVertex2f(300,300) + + + glVertex2f(300,300) + glVertex2f(250,250) + + glVertex2f(250,250) + glVertex2f(300,200) + + glEnd() + +def chick(): + #head circle + #body circle + #limbs lines + #beak triangle + + #body + bodyX = eggX+cX + bodyY = eggY-20+cY + glColor3f(1,1,0.6) + glPointSize(2) + glLineWidth(2) + glBegin(GL_TRIANGLE_FAN) + glVertex2f(bodyX,bodyY) + for i in range(0,361,1): + glVertex2f((50)*math.cos(3.14*i/180)+bodyX, + (50)*math.sin(3.14*i/180)+bodyY) + glEnd() + + # head + headX = eggX+cX + headY = eggY+30+cY + glColor3f(1,1,0.3) + glPointSize(2) + glLineWidth(2) + glBegin(GL_TRIANGLE_FAN) + glVertex2f(headX,headY) + for i in range(0,361,1): + glVertex2f((30)*math.cos(3.14*i/180)+headX, + (30)*math.sin(3.14*i/180)+headY) + glEnd() + + #limbs + + + # beak + + + + glColor3f(1,0,0) + glPointSize(2) + glLineWidth(2) + glBegin(GL_TRIANGLES) + glVertex2f(headX+30+cX,headY+10+cY) + glVertex2f(headX+30+cX,headY-10+cY) + glVertex2f(headX+40+cX,headY-5+cY) + glEnd() + + + +def getPixel(x,y): + clr = glReadPixels(x,y,1,1,GL_RGB,GL_FLOAT,None)[0][0] + print(f"get pixel color {clr}") + return clr + + +def setPixel(x,y,fillcolor): + glColor3f(*fillcolor) + glPointSize(2) + glLineWidth(2) + glBegin(GL_POINTS) + glVertex2f(x,y) + glEnd() + +def boundaryfill(x,y,fillcolor,boundarycolor): + print("inside bfil func") + currentcolor = getPixel(x,y) + print(f"curr_color {currentcolor}") + if all (currentcolor != fillcolor) : + print("inside if 1") + if all (currentcolor != boundarycolor): + print("inside if 2") + setPixel(x,y,fillcolor) + boundaryfill(x+2,y,fillcolor,boundarycolor) + boundaryfill(x,y+2,fillcolor,boundarycolor) + boundaryfill(x-2,y,fillcolor,boundarycolor) + boundaryfill(x,y-2,fillcolor,boundarycolor) + +def boundaryfillx(x,y,fillcolor,oldcolor): + print("inside bfil XXX func") + currentcolor = getPixel(x,y) + print(f"curr_color {currentcolor}") + if all (currentcolor == oldcolor) : + print("inside if 2") + setPixel(x,y,fillcolor) + boundaryfill(x+2,y,fillcolor,oldcolor) + boundaryfill(x,y+2,fillcolor,oldcolor) + boundaryfill(x-2,y,fillcolor,oldcolor) + boundaryfill(x,y-2,fillcolor,oldcolor) + + +def click(button,state,x,y): + if button==GLUT_LEFT_BUTTON and state == GLUT_DOWN: + boundaryfillx(300,300,[0.,0.,1.],[1.,1.,0.69803923]) + #boundaryfill(x,y,[0,0,1],[1,1,1]) + +def draw(): + glClear(GL_COLOR_BUFFER_BIT) + chick() + egg() + crack() + if frag: + fragments(100,100,150,150,200,100) + fragments(150,100,200,130,230,110) + fragments(90,90,150,100,200,50) + + + glutSwapBuffers() + + +def animate(temp): + global timer,lineW,cY,cX,frag + print(f"ref timer: {timer}") + timer+=1 + + if timer>3: + lineW+=10 + if timer>6 and timer<12: + cY+=50 + frag = True + + if timer>12 and timer<17: + cX+=100 + if timer>17 and timer<20: + cY-=50 + cX-=100 + + glutPostRedisplay() + glutTimerFunc(1000,animate,0) + + + +def main(): + glutInit(sys.argv) + glutInitWindowSize(WS,WS) + glutCreateWindow("eggilicious") + gluOrtho2D(0,WS,0,WS) + glutDisplayFunc(draw) + glutTimerFunc(0,animate,0) + glutMouseFunc(click) + glutMainLoop() + +main() diff --git a/cg-lab/animation/moving_square.py b/cg-lab/animation/moving_square.py new file mode 100644 index 0000000..c5f12d6 --- /dev/null +++ b/cg-lab/animation/moving_square.py @@ -0,0 +1,47 @@ +#ARJUN SANDHU + +import math +import time + +from OpenGL.GL import * +from OpenGL.GLU import * +from OpenGL.GLUT import * + +x=-200 +y=0 + +def init(): + glClearColor(0,0,0,1) + gluOrtho2D(-300,300,-300,300) +def draw(): + global x,y + glClear(GL_COLOR_BUFFER_BIT) + glColor3f(1,0,1) + glLineWidth(2) + glBegin(GL_QUADS) + glVertex2f(x,y) + glVertex2f(x+50,y) + glVertex2f(x+50,y+50) + glVertex2f(x,y+50) + glEnd() + glutSwapBuffers() +def animate(temp): + global x,y + if(x<500): + x+=10 + else: + x=-200 + glutPostRedisplay() + glutTimerFunc(1000,animate,0) + +def main(): + glutInit(sys.argv) + glutInitDisplayMode(GLUT_RGBA) + glutInitWindowSize(500,500) + glutInitWindowPosition(600,0) + glutCreateWindow('SAMPLE') + glutDisplayFunc(lambda:draw()) + glutTimerFunc(0,animate,0) + init() + glutMainLoop() +main() diff --git a/cg-lab/animation/movingcar.py b/cg-lab/animation/movingcar.py new file mode 100644 index 0000000..b028b80 --- /dev/null +++ b/cg-lab/animation/movingcar.py @@ -0,0 +1,120 @@ +#Dhanush P K +# + +from OpenGL.GL import * +from OpenGL.GLU import * +from OpenGL.GLUT import * +import sys +import math +from playsound import playsound + +GLOBAL_X=0 +GLOBAL_Y=0 +ROTATE_X=0 +ROTATE_Y=0 +WINDOW_SIZE=1000 +POINT_SIZE=10 +DIR=1 +THETA=0.0 +BOB_RADIUS=40 +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): + if s==0: + x=x-150 + y=y-100 + elif s==1: + x=x+150 + y=y-100 + glColor3f(0,0,0) + glBegin(GL_TRIANGLE_FAN) + glVertex2f(x,y) + for i in range(0,360,1): + glVertex2f(BOB_RADIUS*math.cos(math.pi*i/180)+x,BOB_RADIUS*math.sin(math.pi*i/180)+y) + glEnd() +def drawSpoke(x,y,s): + if s==0: + x=x-150 + y=y-100 + elif s==1: + x=x+150 + y=y-100 + glColor3f(1,1,1) + glLineWidth(5) + glBegin(GL_LINES) + glVertex2f(x,y) + glVertex2f(x+ROTATE_X,y+ROTATE_Y) + glEnd() + +def drawRectangle(x,y): + glBegin(GL_QUADS) + glVertex2f(x-250,y-100) + glVertex2f(x-250,y+50) + glVertex2f(x+250,y+50) + glVertex2f(x+250,y-100) + glEnd() +def drawCar(): + glClear(GL_COLOR_BUFFER_BIT) + glColor3f(1.0,0,0) + glLineWidth(POINT_SIZE) + drawRectangle(GLOBAL_X,GLOBAL_Y) + drawCircle(GLOBAL_X,GLOBAL_Y,0) + drawCircle(GLOBAL_X,GLOBAL_Y,1) + drawSpoke(GLOBAL_X,GLOBAL_Y,0) + drawSpoke(GLOBAL_X,GLOBAL_Y,1) + glFlush() + glutSwapBuffers() + +def keyboard(key,x,y): + key=key.decode() + i=0 + if key=='d': + i=i+1 + animate(i) + elif key=='a': + i=i-1 + animate(i) + + elif key=='s': + i=0 + animate(0) + elif key=='h': + animate(i) + playsound('/home/users/20221034/Desktop/Dhanush/bushorn.wav') + + + +def animate(speed): + global GLOBAL_X + global GLOBAL_Y + global DIR + global THETA + global ROTATE_Y + global ROTATE_X + + glutPostRedisplay() + if(speed==0): + return + glutTimerFunc(int(1000/60),animate,speed) + GLOBAL_X=GLOBAL_X+speed + ROTATE_X=(BOB_RADIUS)*math.cos(math.radians(THETA)) + ROTATE_Y=-(BOB_RADIUS)*math.sin(math.radians(THETA)) + if( THETA==360): + THETA=0 + THETA=THETA+speed + + + +def main(): + glutInit(sys.argv) + glutInitWindowPosition(0,0) + glutInitWindowSize(WINDOW_SIZE,WINDOW_SIZE) + glutInitDisplayMode(GLUT_RGB) + glutCreateWindow("movingcar") + glutDisplayFunc(drawCar) + glutIdleFunc(drawCar) + glutKeyboardFunc(keyboard) + init() + glutMainLoop() +main() diff --git a/cg-lab/animation/pendulum.py b/cg-lab/animation/pendulum.py new file mode 100644 index 0000000..36f67bc --- /dev/null +++ b/cg-lab/animation/pendulum.py @@ -0,0 +1,72 @@ +""" +PENDULUM + +ABHINAND D MANOJ + +""" +from OpenGL.GL import * +from OpenGL.GLUT import * +from OpenGL.GLU import * +import math + +# Global variables +width, height = 800, 600 +angle = 0.0 +direction = 1.0 + +def init(): + glClearColor(0.0, 0.0, 0.0, 1.0) + gluOrtho2D(-2.0, 2.0, -2.0, 2.0) + +def draw_pendulum(): + glClear(GL_COLOR_BUFFER_BIT) + glLoadIdentity() + glColor3f(1.0, 1.0, 1.0) + + # Draw the fixed point + glPointSize(5.0) + glBegin(GL_POINTS) + glVertex2f(0.0, 1.0) + glEnd() + + # Draw the pendulum rod + glLineWidth(2.0) + glBegin(GL_LINES) + glVertex2f(0.0, 1.0) + glVertex2f(math.sin(angle), 1.0 - math.cos(angle)) + glEnd() + + # Draw the pendulum bob + glTranslatef(math.sin(angle), 1.0 - math.cos(angle), 0) + glColor3f(0, 0, 1) + glutSolidSphere(0.1, 20, 20) + + glFlush() + +def update(value): + global angle, direction + angle += 0.01 * direction + + if angle >= (math.pi/2-math.pi/4): + direction = -1.0 + elif angle <= -(math.pi/2-math.pi/4): + direction = 1.0 + + glutPostRedisplay() + glutTimerFunc(10, update, 0) + +def main(): + glutInit() + glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB) + glutInitWindowSize(width, height) + glutCreateWindow("Pendulum Simulation") + + init() + + glutDisplayFunc(draw_pendulum) + glutTimerFunc(10, update, 0) + + glutMainLoop() + +if __name__ == '__main__': + main() diff --git a/cg-lab/animation/pendulum_animation.py b/cg-lab/animation/pendulum_animation.py new file mode 100644 index 0000000..974d859 --- /dev/null +++ b/cg-lab/animation/pendulum_animation.py @@ -0,0 +1,70 @@ +''' +Afeef Mohammed + +pgrm Animation_pendulum +''' + +from OpenGL.GL import * +from OpenGL.GLU import * +from OpenGL.GLUT import * +import math +import sys + +x=0 +y=0 +t=0.00 +a=1 + +def init(): + glClear(GL_COLOR_BUFFER_BIT) + gluOrtho2D(-500,500,-500,500) + +def draw(): + global x,y,t + glClear(GL_COLOR_BUFFER_BIT) + glColor3f(1,1,1) + glLineWidth(2) + glBegin(GL_LINES) + glVertex2f(0,0) + glVertex2f(0*math.cos(t)-(-200)*math.sin(t),0*math.sin(t)+(-200)*math.cos(t)) + glEnd() + glColor3f(1,1,1) + glLineWidth(2) + glBegin(GL_POINTS) + theta=0.0 + while theta<=6.28: + x=float(50)*math.cos(theta) + y=-250+float(50)*math.sin(theta) + glVertex2f(x*math.cos(t)-y*math.sin(t),x*math.sin(t)+y*math.cos(t)) + theta+=0.001 + glEnd() + glutSwapBuffers() + + +def animate(temp): + global t,a + if a==1: + if(t>(-1)): + t-=0.01 + else: + a=2 + if a==2: + if t<1: + t+=0.01 + else: + a=1 + glutPostRedisplay() + glutTimerFunc(50,animate,50) + +def main(): + glutInit(sys.argv) + glutInitDisplayMode(GLUT_RGBA) + glutInitWindowSize(500,500) + glutInitWindowPosition(600,0) + glutCreateWindow("SAMPLE") + glutDisplayFunc(lambda : draw()) + glutTimerFunc(0,animate,0) + init() + glutMainLoop() +main() + diff --git a/cg-lab/animation/pendulum_animation_3.py b/cg-lab/animation/pendulum_animation_3.py new file mode 100644 index 0000000..57b3998 --- /dev/null +++ b/cg-lab/animation/pendulum_animation_3.py @@ -0,0 +1,164 @@ +#Harikrishnan Aneesh +# +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 +DIR=1 + +PEND_LENGTH=float(int(input("Length of the Pendulum: "))) +BOB_RADIUS=float(int(input("Bob Radius: "))) +MAX_THETA=float(int(input("Maxim angle of swing: "))) +THETA=0.0 + +def init(): + glClearColor(1.0,1.0,1.0,1.0) + gluOrtho2D(-WINDOW_SIZE,WINDOW_SIZE,-WINDOW_SIZE,WINDOW_SIZE) + +def drawCircle(x,y): + i=0.0 + glBegin(GL_TRIANGLE_FAN) + glVertex2f(x,y) + for i in range(0,360,1): + glVertex2f(BOB_RADIUS*math.cos(math.pi*i/180.0)+x,BOB_RADIUS*math.sin(math.pi*i/180.0)+y) + glEnd() + +def drawPendulum(): + global MAX_THETA + global GLOBAL_X + global GLOBAL_Y + global THETA + global DIR + glClear(GL_COLOR_BUFFER_BIT) + glColor3f(0.0,1.0,1.0) + glLineWidth(5) + glBegin(GL_LINES) + glVertex2f(0,0) + glVertex2f(GLOBAL_X,GLOBAL_Y) + glEnd() + drawCircle(GLOBAL_X,GLOBAL_Y) + glutSwapBuffers() + +def animate(temp): + global MAX_THETA + global GLOBAL_X + global GLOBAL_Y + global THETA + global DIR + glutPostRedisplay() + glutTimerFunc(int(1200/FPS),animate,int(0)) + if DIR==1: + if(THETA=-MAX_THETA): + THETA=THETA-1.0 + else: + DIR=1 + GLOBAL_X=PEND_LENGTH*math.sin(math.radians(THETA)) + GLOBAL_Y=-PEND_LENGTH*math.cos(math.radians(THETA)) + + + +def main(): + glutInit(sys.argv) + glutInitWindowSize(WINDOW_SIZE,WINDOW_SIZE) + glutInitWindowPosition(0,0) + glutInitDisplayMode(GLUT_RGB) + glutCreateWindow("Pendulum") + glutDisplayFunc(drawPendulum) + glutTimerFunc(0,animate,0) + glutIdleFunc(drawPendulum) + init() + glutMainLoop() + +main() +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 +DIR=1 + +PEND_LENGTH=float(int(input("Length of the Pendulum: "))) +BOB_RADIUS=float(int(input("Bob Radius: "))) +MAX_THETA=float(int(input("Maxim angle of swing: "))) +THETA=0.0 + +def init(): + glClearColor(1.0,1.0,1.0,1.0) + gluOrtho2D(-WINDOW_SIZE,WINDOW_SIZE,-WINDOW_SIZE,WINDOW_SIZE) + +def drawCircle(x,y): + i=0.0 + glBegin(GL_TRIANGLE_FAN) + glVertex2f(x,y) + for i in range(0,360,1): + glVertex2f(BOB_RADIUS*math.cos(math.pi*i/180.0)+x,BOB_RADIUS*math.sin(math.pi*i/180.0)+y) + glEnd() + +def drawPendulum(): + global MAX_THETA + global GLOBAL_X + global GLOBAL_Y + global THETA + global DIR + glClear(GL_COLOR_BUFFER_BIT) + glColor3f(0.0,1.0,0.0) + glLineWidth(5) + glBegin(GL_LINES) + glVertex2f(0,0) + glVertex2f(GLOBAL_X,GLOBAL_Y) + glEnd() + drawCircle(GLOBAL_X,GLOBAL_Y) + glutSwapBuffers() + +def animate(temp): + global MAX_THETA + global GLOBAL_X + global GLOBAL_Y + global THETA + global DIR + glutPostRedisplay() + glutTimerFunc(int(1000/FPS),animate,int(0)) + if DIR==1: + if(THETA=-MAX_THETA): + THETA=THETA-1.0 + else: + DIR=1 + GLOBAL_X=PEND_LENGTH*math.sin(math.radians(THETA)) + GLOBAL_Y=-PEND_LENGTH*math.cos(math.radians(THETA)) + + + +def main(): + glutInit(sys.argv) + glutInitWindowSize(WINDOW_SIZE,WINDOW_SIZE) + glutInitWindowPosition(0,0) + glutInitDisplayMode(GLUT_RGB) + glutCreateWindow("Pendulum") + glutDisplayFunc(drawPendulum) + glutTimerFunc(0,animate,0) + glutIdleFunc(drawPendulum) + init() + glutMainLoop() + +main() diff --git a/cg-lab/animation/snake_apple.py b/cg-lab/animation/snake_apple.py new file mode 100644 index 0000000..5477f43 --- /dev/null +++ b/cg-lab/animation/snake_apple.py @@ -0,0 +1,197 @@ + #Rahul R + # Roll no 96 + + #Animation + +from OpenGL.GL import* +from OpenGL.GLU import* +from OpenGL.GLUT import* +import math , random + +WINDOWSIZE=1000 # window size +timer = 1 # reference timer + +appleX , appleY , appleR = 0,0,50 +eaten = False # F - not eaten, T - eaten + +SnakeQuadrant = 1 +appleQuadrant = 1 + + +snakeX,snakeY = -900,-900 # snake coordinates +tailX,tailY = 0,0 # tail coordinates +snakeSegCount = 1 # number of segments of snake +snakeSize = 50 # snake size +direction = 0 # snake direction + + +def grid(WINDOWSIZE=1000,offset=100): + glColor3f(0.5,0.5,0.5) + glLineWidth(1) + glBegin(GL_LINES) + + for i in range(0,WINDOWSIZE,offset): + glVertex2f(0+i,WINDOWSIZE) + glVertex2f(0+i,-WINDOWSIZE) + glVertex2f(0-i,WINDOWSIZE) + glVertex2f(0-i,-WINDOWSIZE) + glVertex2f(WINDOWSIZE,0+i) + glVertex2f(-WINDOWSIZE,0+i) + glVertex2f(WINDOWSIZE,0-i) + glVertex2f(-WINDOWSIZE,0-i) + + glEnd() + + +def apple_coordinates(): + global appleX,appleY,eaten + # get random apple coordinates + # getting a random x,y between -900 and +900 + appleX = random.randint(-9, 9) * 100 + appleY = random.randint(-9, 9) * 100 + print(f"random apple coords ({appleX},{appleY})") + eaten = False + +def apple_putter(): + #put apple in the coordinates + glColor3f(1,0,0) #red color + glLineWidth(1) + glBegin(GL_TRIANGLE_FAN) + glVertex2f(appleX,appleY) + for i in range(0,361,1): + glVertex2f(appleR*math.cos(math.pi*i/180)+appleX, + appleR*math.sin(math.pi*i/180)+appleY) + glEnd() + #print(f"put-ed apple at ({appleX},{appleY})") + + +def find_apple_quadrant(): + global appleX,appleY,snakeX,snakeY + global SnakeQuadrant,appleQuadrant + + if appleX>0: + if appleY>0: + appleQuadrant = 1 + elif appleY<0: + appleQuadrant = 4 + if appleX<0: + if appleY>0: + appleQuadrant = 2 + elif appleY<0: + appleQuadrant = 3 + + print(f"apple at quadrant ({appleQuadrant})") + print(f"\t\t snake direction ({direction})") + + + +def snake(): + global tailX,tailY + glColor3f(0.1,1,1) #magenta? violet? idk + glPointSize(snakeSize) + glBegin(GL_POINTS) + glVertex2f(snakeX,snakeY) + glEnd() + tailX=snakeX + tailY=snakeY + + for i in range(1,snakeSegCount): + glColor3f(0.1,1,0) #magenta? violet? idk + glPointSize(snakeSize) + glBegin(GL_POINTS) + #print(f"snake at ({snakeX},{snakeY})") + #glVertex2f(tailX-(i*110),tailY) + if direction == 1: + glVertex2f(tailX,tailY-(i*110)) + elif direction == 2: + glVertex2f(tailX,tailY+(i*110)) + elif direction == 3: + glVertex2f(tailX-(i*110),tailY) + elif direction == 4: + glVertex2f(tailX+(i*110),tailY) + glEnd() + + + + +def animate(temp): + + global timer + global appleX,appleY,appleR,eaten + global snakeX,snakeY,snakeSegCount,snakeSize + global tailX,tailY,oldX,oldY,direction + + + + glutPostRedisplay() + glutTimerFunc(int(10000/60),animate,int(0)) + + timer+=1 + print(timer) + + + + if eaten: + apple_coordinates() + find_apple_quadrant() + + if snakeXappleX: + oldX = snakeX + snakeX-=100 + direction = 4 + elif snakeX==appleX: + if snakeYappleY: + oldY = snakeY + snakeY-=100 + direction = 2 + + elif snakeY==appleY: + eaten = True + snakeSegCount+=1 + print(f"\t\tsnake segment count ({snakeSegCount})") + print(f"\t\tapple at quadrant ({appleQuadrant})") + print(f"direction : {direction}") + + + + + + +def drawFunction(): + glClear(GL_COLOR_BUFFER_BIT) + grid() + apple_putter() + snake() + + glutSwapBuffers() + + + +def main(): + glutInit(sys.argv) + glutInitWindowSize(WINDOWSIZE,WINDOWSIZE) + glutInitDisplayMode(GLUT_RGBA) + glutCreateWindow("Snake Animation") + + glutDisplayFunc(drawFunction) + glutTimerFunc(0,animate,0) + + glutIdleFunc(drawFunction) + + glClearColor(0.0,0.0,0.0,0.0) + gluOrtho2D(-WINDOWSIZE,WINDOWSIZE,-WINDOWSIZE,WINDOWSIZE) + + glutMainLoop() + +main() + + diff --git a/cg-lab/animation/snakelangelo.py b/cg-lab/animation/snakelangelo.py index 76a4ecd..faf80d0 100644 --- a/cg-lab/animation/snakelangelo.py +++ b/cg-lab/animation/snakelangelo.py @@ -200,7 +200,7 @@ def main(): glutInit(sys.argv) glutInitWindowSize(WINDOWSIZE,WINDOWSIZE) glutInitDisplayMode(GLUT_RGBA) - glutCreateWindow("Snake'l'Angleo - the game satan introduced to Adam and Eve") + glutCreateWindow("Hungry Snake") glutDisplayFunc(drawFunction) glutTimerFunc(0,animate,0) diff --git a/cg-lab/animation/snowman.py b/cg-lab/animation/snowman.py new file mode 100644 index 0000000..c5219d1 --- /dev/null +++ b/cg-lab/animation/snowman.py @@ -0,0 +1,132 @@ +import math +import time +from OpenGL.GL import* +from OpenGL.GLU import * +from OpenGL.GLUT import * +from random import randint +from pygame import mixer + +#mixer.init() +#plays=mixer.Sound('hello.wav') + + +snows = [] +for i in range(-300, 320, 10): + speed = randint(1,10) + snows.append([i,300+randint(1,10), speed]) +direction = 1 +theta = 0 +suntheta = 0 +star = 1 + + +def init(): + glClearColor(0.678,0.847,0.902,1) + gluOrtho2D(-300,300,-300,300) + + +def circle(x,y,r, color=[1,1,1]): + + glColor3f(color[0],color[1],color[2]) + glLineWidth(2) + glBegin(GL_TRIANGLE_FAN) + glVertex2f(x,y) + for i in range(0,361,1): + glVertex2f(x+ r*math.cos(math.pi*i/180),y+ r*math.sin(math.pi*i/180)) + glEnd() + +def poly(coordinate_list, color=[1,1,1]): + glColor3f(color[0],color[1],color[2]) + glLineWidth(2) + glBegin(GL_POLYGON) + for i in coordinate_list: + glVertex2fv(i) + glEnd() + +def line(coordinate_list, color=[1,1,1]): + glColor3f(color[0],color[1],color[2]) + glLineWidth(5) + glBegin(GL_LINES) + glVertex2fv(coordinate_list[0]) + glVertex2fv(coordinate_list[1]) + glEnd() + +def animate(temp): + global snows, theta, direction, suntheta, star + for x in snows: + x[1] = x[1] - x[2] + if x[1] < -300: + x[1] = 300 + x[2] = randint(1,10) + + + theta = theta+0.05*(direction) + if theta > 0.3: + plays.play() + direction = -direction + elif theta < -0.3: + direction = -direction + + suntheta = suntheta-0.01 + print(suntheta) + if suntheta < -0.7: + suntheta = 0.1 + star = -star + + glutPostRedisplay() + glutTimerFunc(100,animate,0) + +def rotate(x, y, theta, xc=75, yc=65): + return [(x - xc)*math.cos(theta) - (y-yc)*math.sin(theta) + xc, (x - xc)*math.sin(theta) + (y-yc)*math.cos(theta) + yc] + +def Man(): + xc = 75 + yc = 65 + global theta, suntheta,star + + + + glClear(GL_COLOR_BUFFER_BIT) + circle(0,0,100) + circle(0,120,60) + circle(-25,135,5, [0,0,0]) + circle(+25,135,5, [0,0,0]) + poly([(0,115),(0,105),(20,110)], [1,0.647,0]) + circle(0,110,5, [1,0.647,0]) + + line([rotate(75,65,theta),rotate(130,95,theta)],[0.345,0.223,0.153]) + line([rotate(130,95,theta),rotate(150,70,theta)],[0.345,0.223,0.153]) + line([rotate(130,95,theta),rotate(140,110,theta)],[0.345,0.223,0.153]) + + line([rotate(-75,65,theta,xc=-75,yc=65),rotate(-130,95,theta,xc=-75,yc=65)],[0.345,0.223,0.153]) + line([rotate(-130,95,theta,xc=-75,yc=65),rotate(-150,70,theta,xc=-75,yc=65)],[0.345,0.223,0.153]) + line([rotate(-130,95,theta,xc=-75,yc=65),rotate(-140,110,theta,xc=-75,yc=65)],[0.345,0.223,0.153]) + + x = rotate(-300,200,suntheta, xc=0, yc= -700) + if star==1: + circle(x[0],x[1],50, [0.082,0.133,0.219]) + circle(x[0]+20,x[1],45, [0.678,0.847,0.902]) + else: + circle(x[0],x[1],50, [1,0.765,0.043]) + + for x in snows: + circle(x[0],x[1],2) + + poly([(-300, -90), (300,-90), (300,-300), (-300,-300)], [1,1,1]) + + glFlush() + + +def main(): + glutInit(sys.argv) + glutInitDisplayMode(GLUT_RGBA) + glutInitWindowSize(500,500) + glutInitWindowPosition(600,0) + glutCreateWindow("SAMPLE") + glutDisplayFunc(lambda:Man()) + glutTimerFunc(0,animate,0) + + init() + glutMainLoop() + +main() diff --git a/cg-lab/filling/non_recb.py b/cg-lab/filling/non_recb.py new file mode 100644 index 0000000..2869c15 --- /dev/null +++ b/cg-lab/filling/non_recb.py @@ -0,0 +1,75 @@ +#lekshmeeka p a +from OpenGL.GL import * +from OpenGL.GLU import * +from OpenGL.GLUT import * +from queue import LifoQueue + +ws = 800 +point_size = 2 +filclr = [1, 0, 0] + +def get_pixel(x, y): + data = glReadPixels(x, ws - y, 1, 1, GL_RGB, GL_FLOAT) + return [round(x, 1) for x in data[0][0]] + +def set_pixel(x, y, fill_color=(0, 0, 0)): + glColor3f(*fill_color) + glPointSize(point_size) + glBegin(GL_POINTS) + glVertex2f(x, y) + glEnd() + glFlush() + +def plot_rect(): + glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) + gluOrtho2D(0, ws, ws, 0) + glColor3f(1, 0, 0) + glLineWidth(point_size) + + sqrsize = 200 + xc = ws / 2 + yc = ws / 2 + hf = sqrsize / 2 + + glBegin(GL_POLYGON) + glVertex2f(xc - hf, yc - hf) + glVertex2f(xc + hf, yc - hf) + glVertex2f(xc + hf, yc + hf) + glVertex2f(xc - hf, yc + hf) + glEnd() + glFlush() + +def boundary_fill(x, y, fill_color, boundary_color): + if fill_color == boundary_color: + return + + stack = LifoQueue() + stack.put((x, y)) + + while not stack.empty(): + current_x, current_y = stack.get() + color = get_pixel(current_x, current_y) + + if color != fill_color and color != boundary_color: + set_pixel(current_x, current_y, fill_color) + + if current_y - point_size >= 0: + stack.put((current_x, current_y - point_size)) + if current_x + point_size < ws: + stack.put((current_x + point_size, current_y)) + if current_y + point_size < ws: + stack.put((current_x, current_y + point_size)) + if current_x - point_size >= 0: + stack.put((current_x - point_size, current_y)) + +def mouse_click(button, state, x, y): + if button == GLUT_LEFT_BUTTON and state == GLUT_DOWN: + boundary_fill(x, y, [0, 0, 1], filclr) + +glutInit() +glutInitWindowSize(ws, ws) +glutCreateWindow("boundary-fill") +glutDisplayFunc(plot_rect) +glutMouseFunc(mouse_click) +glutMainLoop() + diff --git a/cg-lab/filling/temp.py b/cg-lab/filling/non_recf.py similarity index 54% rename from cg-lab/filling/temp.py rename to cg-lab/filling/non_recf.py index f3fbca5..9bc52dc 100644 --- a/cg-lab/filling/temp.py +++ b/cg-lab/filling/non_recf.py @@ -1,20 +1,20 @@ +#lekshmeeka p a +# from OpenGL.GL import * -from OpenGL.GLU import * # Utility Library -from OpenGL.GLUT import * # Utility Toolkit - -import sys -sys.setrecursionlimit(100000) - -x,y = 0,0 # mouse coordinates -ws= 500 +from OpenGL.GLU import * +from OpenGL.GLUT import * +from queue import Queue +import numpy as np + +x = 0 +y = 0 +ws = 500 point_size = 2 - def get_pixel(x, y): - pixel = glReadPixels(x, ws -y, 1, 1, GL_RGB, GL_FLOAT) + pixel = glReadPixels(x, ws - y, 1, 1, GL_RGB, GL_FLOAT) return pixel[0][0] - def set_pixel(x, y, fill_color=(0, 0, 0)): glColor3f(*fill_color) glPointSize(point_size) @@ -23,7 +23,6 @@ def set_pixel(x, y, fill_color=(0, 0, 0)): glEnd() glFlush() - def rectangle(vertices, color): glColor3f(color[0], color[1], color[2]) glBegin(GL_POLYGON) @@ -31,13 +30,12 @@ def rectangle(vertices, color): glVertex2f(*vertex) glEnd() - def plot_rect(): glClear(GL_COLOR_BUFFER_BIT) gluOrtho2D(0, ws, ws, 0) - - square_center = [ws / 2, ws / 2] - square_size = 200 + + square_center = [ws / 2, ws / 2] + square_size = 200 square_vertices = [ [square_center[0] - square_size / 2, square_center[1] - square_size / 2], @@ -45,35 +43,35 @@ def plot_rect(): [square_center[0] + square_size / 2, square_center[1] + square_size / 2], [square_center[0] + square_size / 2, square_center[1] - square_size / 2] ] - - rectangle(square_vertices, [1, 1, 0]) - - glFlush() + rectangle(square_vertices, [1, 0, 0]) + glFlush() def flood_fill(x, y, new_color, old_color): - color = get_pixel(x, y) - if all(color == old_color): - set_pixel(x, y, new_color) - flood_fill(x + point_size, y, new_color, old_color) - flood_fill(x, y + point_size, new_color, old_color) - flood_fill(x - point_size, y, new_color, old_color) - flood_fill(x, y - point_size, new_color, old_color) + if np.all(new_color == old_color) or not np.all(get_pixel(x, y) == old_color): + return + queue = Queue() + queue.put((x, y)) + + while not queue.empty(): + current_x, current_y = queue.get() + if np.all(get_pixel(current_x, current_y) == old_color): + set_pixel(current_x, current_y, new_color) + queue.put((current_x + point_size, current_y)) + queue.put((current_x, current_y + point_size)) + queue.put((current_x - point_size, current_y)) + queue.put((current_x, current_y - point_size)) def mouse_click(button, state, x, y): if button == GLUT_LEFT_BUTTON and state == GLUT_DOWN: - flood_fill(x, y, [0, 1, 1], get_pixel(x, y)) - - - + flood_fill(x, y, [0, 0, 1], get_pixel(x, y)) glutInit() glutInitWindowSize(ws, ws) -glutCreateWindow("fahad fasil") +glutCreateWindow("flood fill") glutDisplayFunc(plot_rect) glutMouseFunc(mouse_click) glutMainLoop() - diff --git a/cg-lab/programs/01_quad.py b/cg-lab/programs/01_quad.py index 01bd0d0..4decc6d 100644 --- a/cg-lab/programs/01_quad.py +++ b/cg-lab/programs/01_quad.py @@ -1,17 +1,43 @@ #1. Quadrilateral printing import math -import time +import time,sys from OpenGL.GL import * from OpenGL.GLU import * from OpenGL.GLUT import * -X = 0 -Y = 0 -side = int(input("side length : ")) -def init(): - glClearColor(0,0,0,1) - gluOrtho2D(-300,300,-300,300) +sys.setrecursionlimit(10000) + +WS = 900 + +X = 200 +Y = 200 +side = 300 +x,y=0,0 +p = 2 + +def getpixel(x,y): + return glReadPixels(x,y,1,1,GL_RGB,GL_FLOAT,None)[0][0] + +def setpixel(x,y,fillcolor): + glColor3f(*fillcolor) + glPointSize(p) + glBegin(GL_POINTS) + glVertex2f(x,y) + glEnd() + glFlush() + +def floodfill(x,y,fillcolor,oldcolor): + current_color = getpixel(x,y) + if all (current_color == oldcolor): + setpixel(x,y,fillcolor) + floodfill(x-p,y,fillcolor,oldcolor) + floodfill(x,y-p,fillcolor,oldcolor) + floodfill(x,y+p,fillcolor,oldcolor) + floodfill(x+p,y,fillcolor,oldcolor) + + + def draw(): glClear(GL_COLOR_BUFFER_BIT) glColor3f(1,1,1) @@ -22,21 +48,21 @@ def draw(): glVertex2f(X+side,Y+side) glVertex2f(X,Y+side) glEnd() + floodfill(300,300,[1,0,1],[1,1,1]) glutSwapBuffers() def main(): glutInit(sys.argv) glutInitDisplayMode(GLUT_RGBA) - glutInitWindowSize(500,500) + glutInitWindowSize(WS,WS) glutInitWindowPosition(1000,0) glutCreateWindow("SAMPLE") - glutDisplayFunc(lambda: draw()) + glutDisplayFunc(draw) - init() + glClearColor(0,0,0,1) + gluOrtho2D(0,WS,0,WS) glutMainLoop() -if __name__ == "__main__": - main() - +main() diff --git a/cg-lab/programs/04_moving_quad.py b/cg-lab/programs/04_moving_quad.py index 2876876..64cdbfc 100644 --- a/cg-lab/programs/04_moving_quad.py +++ b/cg-lab/programs/04_moving_quad.py @@ -9,19 +9,42 @@ from OpenGL.GLUT import * X = -200 Y = -200 +WINDOW_SIZE = 300 + + +def grid(WINDOWSIZE=1000,offset=50): + glColor3f(0.5,0.5,0.5) + glLineWidth(1) + glBegin(GL_LINES) + + for i in range(0,WINDOWSIZE,offset): + glVertex2f(0+i,WINDOWSIZE) + glVertex2f(0+i,-WINDOWSIZE) + glVertex2f(0-i,WINDOWSIZE) + glVertex2f(0-i,-WINDOWSIZE) + glVertex2f(WINDOWSIZE,0+i) + glVertex2f(-WINDOWSIZE,0+i) + glVertex2f(WINDOWSIZE,0-i) + glVertex2f(-WINDOWSIZE,0-i) + + glEnd() def draw(): global X,Y glClear(GL_COLOR_BUFFER_BIT) + + grid() glColor3f(1,0,1) glLineWidth(2) glBegin(GL_QUADS) glVertex2f(X,Y) - glVertex2f(X+50,Y) - glVertex2f(X+50,Y+50) - glVertex2f(X,Y+50) + glVertex2f(X+150,Y) + glVertex2f(X+150,Y+150) + glVertex2f(X,Y+150) glEnd() glutSwapBuffers() + + def animate(temp): global X,Y if(X < 500): diff --git a/cg-lab/programs/lclip.py b/cg-lab/programs/lclip.py new file mode 100644 index 0000000..3b4fa56 --- /dev/null +++ b/cg-lab/programs/lclip.py @@ -0,0 +1,163 @@ +# NANDULAL KRISHNA +# Line CLIPPING +# 20221097 + +from OpenGL.GL import * +from OpenGL.GLU import * +from OpenGL.GLUT import * +import time + +WS = 500 +border = 100 +point_size = 5 + + +def init(): + glClearColor(0.0, 0.0, 0.0, 1.0) + + +def get_pixel(x, y): + pixel = glReadPixels(x, WS - y, 1, 1, GL_RGB, GL_FLOAT) + return pixel[0][0] + + +def set_pixel(x, y, filled_color): + glColor3f(filled_color[0], filled_color[1], filled_color[2]) + glPointSize(point_size) + glBegin(GL_POINTS) + glVertex2f(x, WS - y) + glEnd() + glFlush() + + +def point_code(x, y): + return [1 if y > WS - border else 0, 1 if y < border else 0, 1 if x > WS - border else 0, + 1 if x < border else 0] + + +def perform_and(p1, p2): + return [c1 and c2 for c1, c2 in zip(point_code(*p1), point_code(*p2))] + + +def perform_or(p1, p2): + return [c1 or c2 for c1, c2 in zip(point_code(*p1), point_code(*p2))] + + +def check_line_visibility(p1, p2): + if perform_or(p1, p2) == [0, 0, 0, 0]: + return 1, "visible" + elif perform_and(p1, p2) != [0, 0, 0, 0]: + print(perform_and(p1, p2)) + return 2, "invisible" + else: + return 3, "clipped" + + +def plot_line(p1, p2, color=(1, 0, 1)): + glColor3f(*color) + glBegin(GL_LINES) + glVertex2f(*p1) + glVertex2f(*p2) + glEnd() + glFlush() + + +def plot_clipping_window(): + glColor3f(1.0, 0.5, 0.0) + glLineWidth(2) + glBegin(GL_LINE_LOOP) + glVertex2f(border, border) + glVertex2f(border, WS - border) + glVertex2f(WS - border, WS - border) + glVertex2f(WS - border, border) + glEnd() + + +def get_clipped_point(point, m): + code = point_code(*point) + print(point, code) + + if code[-1] == 1: + """left portion""" + if code[0] == 1: + point[1] = WS - border + elif code[1] == 1: + point[1] = border + else: + point[1] = point[1] + m * (border - point[0]) + point[0] = border + elif code[-2] == 1: + """right portion""" + if code[0] == 1: + point[1] = WS - border + elif code[1] == 1: + point[1] = border + else: + point[1] = point[1] + m * (WS - border - point[0]) + point[0] = WS - border + elif code[0] == 1: + """top portion""" + if code[-1] == 1: + point[0] = border + elif code[-2] == 1: + point[0] = WS - border + else: + point[0] = point[0] + (WS - border - point[1]) / m + point[1] = WS - border + elif code[1] == 1: + """bottom portion""" + if code[-1] == 1: + point[0] = border + elif code[-2] == 1: + point[0] = WS - border + else: + point[0] = point[0] + (border - point[1]) / m + point[1] = border + + +def slop(p1, p2): + return (p2[1] - p1[1]) / (p2[0] - p1[0]) + + +def get_clipped_points(p1, p2): + visibility, _ = check_line_visibility(p1, p2) + if visibility == 3: + m = slop(p1, p2) + print("m before", m) + get_clipped_point(p1, m) + get_clipped_point(p2, m) + print("m after", slop(p1, p2)) + print(p1, p2) + return p1, p2 + + +def plot_clipped_line(p1, p2): + plot_line(*get_clipped_points(p1, p2), color=(1, 1, 0)) + + +def display(): + gluOrtho2D(0, WS, 0, WS) + glClear(GL_COLOR_BUFFER_BIT) + plot_clipping_window() + p1 = [WS / 10, WS - 150] + p2 = [WS - 150, WS / 10] + print("point to be plotted", p1, p2) + plot_line(p1, p2) + glutSwapBuffers() + time.sleep(2) # Delay for 2 seconds + glClear(GL_COLOR_BUFFER_BIT) + plot_clipping_window() + plot_clipped_line(p1, p2) + glutSwapBuffers() + + +def main(): + glutInit() + glutInitWindowSize(WS, WS) + glutCreateWindow("LC") + glutDisplayFunc(display) + glutMainLoop() + + +main() + diff --git a/cg-lab/programs/pclip.py b/cg-lab/programs/pclip.py new file mode 100644 index 0000000..2a661c2 --- /dev/null +++ b/cg-lab/programs/pclip.py @@ -0,0 +1,140 @@ + +# NANDULAL KRISHNA +# POLYGON CLIPPING +# 20221097 + +from OpenGL.GL import * +from OpenGL.GLUT import * +from OpenGL.GLU import * +import time + +MAX_POINTS = 20 + +def x_intersect(x1, y1, x2, y2, x3, y3, x4, y4): + num = (x1 * y2 - y1 * x2) * (x3 - x4) - (x1 - x2) * (x3 * y4 - y3 * x4) + den = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4) + return int(num / den) + +def y_intersect(x1, y1, x2, y2, x3, y3, x4, y4): + num = (x1 * y2 - y1 * x2) * (y3 - y4) - (y1 - y2) * (x3 * y4 - y3 * x4) + den = (x1 - x2) * (y3 - y4) - (y1 - y2) * (x3 - x4) + return int(num / den) + +def clip(poly_points, clipper_points): + new_points = [] + new_poly_size = 0 + + for i in range(len(poly_points)): + k = (i + 1) % len(poly_points) + ix, iy = poly_points[i] + kx, ky = poly_points[k] + + i_pos = (clipper_points[1][0] - clipper_points[0][0]) * (iy - clipper_points[0][1]) - ( + clipper_points[1][1] - clipper_points[0][1]) * (ix - clipper_points[0][0]) + + k_pos = (clipper_points[1][0] - clipper_points[0][0]) * (ky - clipper_points[0][1]) - ( + clipper_points[1][1] - clipper_points[0][1]) * (kx - clipper_points[0][0]) + + if i_pos < 0 and k_pos < 0: + new_points.append([kx, ky]) + new_poly_size += 1 + elif i_pos >= 0 and k_pos < 0: + new_points.append([x_intersect(clipper_points[0][0], clipper_points[0][1], clipper_points[1][0], + clipper_points[1][1], ix, iy, kx, ky), + y_intersect(clipper_points[0][0], clipper_points[0][1], clipper_points[1][0], + clipper_points[1][1], ix, iy, kx, ky)]) + new_poly_size += 1 + new_points.append([kx, ky]) + new_poly_size += 1 + elif i_pos < 0 and k_pos >= 0: + new_points.append([x_intersect(clipper_points[0][0], clipper_points[0][1], clipper_points[1][0], + clipper_points[1][1], ix, iy, kx, ky), + y_intersect(clipper_points[0][0], clipper_points[0][1], clipper_points[1][0], + clipper_points[1][1], ix, iy, kx, ky)]) + new_poly_size += 1 + + return new_points + +def suthHodgClip(poly_points, clipper_points): + global clip_done + if not clip_done: + glColor3f(1.0, 1.0, 1.0) + glBegin(GL_LINE_LOOP) + for point in clipper_points: + glVertex2f(point[0], point[1]) + glEnd() + + glColor3f(1.0, 1.0, 1.0) + glBegin(GL_LINE_LOOP) + for point in poly_points: + glVertex2f(point[0], point[1]) + glEnd() + + glColor3f(1.0, 1.0, 1.0) + glBegin(GL_LINE_LOOP) + for i in range(len(clipper_points)): + k = (i + 1) % len(clipper_points) + x1, y1 = clipper_points[i] + x2, y2 = clipper_points[k] + glVertex2f(x1, y1) + glVertex2f(x2, y2) + glEnd() + + for i in range(len(clipper_points)): + k = (i + 1) % len(clipper_points) + poly_points = clip(poly_points, [clipper_points[i], clipper_points[k]]) + + glColor3f(0.0, 1.0, 0.0) + glBegin(GL_LINE_LOOP) + for point in poly_points: + glVertex2f(point[0], point[1]) + glEnd() + + glutSwapBuffers() + time.sleep(2) + clip_done = True + + + glClear(GL_COLOR_BUFFER_BIT) + glColor3f(0.0, 1.0, 0.0) + glBegin(GL_LINE_LOOP) + for point in poly_points: + glVertex2f(point[0], point[1]) + glEnd() + + glutSwapBuffers() + +def display(): + glClear(GL_COLOR_BUFFER_BIT) + + + poly_size = 4 + poly_points = [[100,100], [400, 100], [400, 400],[100,400]] + + + clipper_size = 4 + clipper_points = [[50, 250], [250, 450], [450,250], [250,50]] + + suthHodgClip(poly_points, clipper_points) + +def reshape(width, height): + glViewport(0, 0, width, height) + glMatrixMode(GL_PROJECTION) + glLoadIdentity() + gluOrtho2D(0,500,0,500) + glMatrixMode(GL_MODELVIEW) + +def main(): + glutInit(sys.argv) + glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB) + glutInitWindowSize(500, 500) + glutCreateWindow(b'PC') + glutReshapeFunc(reshape) + glutDisplayFunc(display) + glClearColor(0.0, 0.0, 0.0, 0.0) + glutMainLoop() + +if __name__ == "__main__": + clip_done = False + main() + diff --git a/mp-lab/8086/03_concat.asm b/mp-lab/8086/03_concat.asm index de2f366..d2b6945 100644 --- a/mp-lab/8086/03_concat.asm +++ b/mp-lab/8086/03_concat.asm @@ -29,7 +29,8 @@ start: lea dx,m1 mov ah,09h int 21h - + + lea si,str1 loop0:mov ah,01h int 21h cmp al,13 diff --git a/mp-lab/8086/t.asm b/mp-lab/8086/t.asm new file mode 100644 index 0000000..292acec --- /dev/null +++ b/mp-lab/8086/t.asm @@ -0,0 +1,59 @@ +data segment + arr1 dw 10 dup(?) + arr2 dw 1,2,3,4,5,6,7,8,9,10 + newline db 13,10,"$" + wel db 13,10,"sagar , what a sudden surprise!!$" + +data ends + +code segment + assume cs:code,ds:data +start: + mov ax,data + mov ds,ax + + lea dx,wel + mov ah,09h + int 21h + ; trivial shiz ends here + + lea si,arr2 + mov cx,10 + mov bx,0 + + display_sum: + mov ax,[si] + add bx,ax + + lea dx,newline + mov ah,09h + int 21h + + ; Convert the array element to ASCII before printing + add ax, '0' + mov dl, al + mov ah, 02h + int 21h + + lea dx,newline + mov ah,09h + int 21h + + add si,2 ; Move to the next element (2 bytes per element) + loop display_sum ; loop based on cx + + lea dx,newline + mov ah,09h + int 21h + + ; Display the sum + mov dx,bx + mov ah,09H + int 21H + + ; exiting + mov ah,4ch + int 21h + +code ends +end start diff --git a/mp-lab/8086/te.asm b/mp-lab/8086/te.asm new file mode 100644 index 0000000..bfb3737 --- /dev/null +++ b/mp-lab/8086/te.asm @@ -0,0 +1,40 @@ +data segment + array db 01h,02h,03h,04h,05h + newline db 13,10,"$" + wel db 13,10,"sagar , what a sudden suppaise!!$" +data ends +code segment + assume cs:code,ds:data +start: + mov ax,data + mov ds,ax + + lea dx,wel + mov ah,09h + int 21h + ; trivial shiz end here + + mov cx,0005h;counter + mov ax,0000h + mov si,0001h + ;lea si,array + + sum:add al,array[si] + + mov dl,al + mov ah,09h + int 21h + + inc si + loop sum + hlt + + + + + mov ah,4ch + int 21h + + +code ends +end start diff --git a/mp-lab/8086/temp.asm b/mp-lab/8086/temp.asm index 4bbdf84..e69de29 100644 --- a/mp-lab/8086/temp.asm +++ b/mp-lab/8086/temp.asm @@ -1,49 +0,0 @@ -.model small -.data - stringarray1 db 30 dup("$") - message1 db 13,10, "this is a god$" - message2 db 13,10, "you have done wrong$" -.code -.stack -start: - mov ax,@data - mov ds,ax - - lea dx,message1 - mov ah,09h - int 21h - - lea dx,message2 - mov ah,09h - int 21h - - call input_string - call output_string - - call exit - - - -exit proc - mov ah,4ch - int 21h -exit endp - -input_string proc - loopin: lea ah,01h - int 21h - cmp al,13 - je skip - mov [si],al - inc si - jmp loopin - skip: call output_string - -input_string endp - -output_string proc - -output_string endp - - -end start \ No newline at end of file