Rectangle clipping Algorithm _python3 final version

Source: Internet
Author: User

Environment Preparation:The Python version is 3.5.1. Use PIP command: Pip install pyopengl install PYOPENGL modulealso note that you need to copy OpenGL files (open File link or Baidu) to the system disk System32 (32-bit system) or SYSWOW64 (64-bit system) directory, or may or error. I met this problem at the school lab today, and the error message seems to be that the Glutinit function cannot be used. Description:The teacher's request is to realize the keyboard input and mouse click Two Interactive way, can't, had to encapsulate the function, convenient call. Code:
#encoding = ' Utf-8 ' #python3.5.1#author by Zhang Jinwei 2016/6/21# rectangle clipping algorithm from opengl.glut import *from opengl.gl import *import Sysblue= (0.0,0.0,1.0) green= (0.0,1.0,0.0) red= (1.0,0.0,0.0) color=bluewinx=400 #窗口宽度winy =400 #窗口高度l1 =[]; #存储坐标class Point:x=0 #x坐标 y=0 #y坐标 pl=0 #相对于矩形裁剪区域左边, the position of this point, 0 for the inside, 1 for the outer pt=0 #相对于矩形裁剪区域上边, the position of this point, 0 for the inside, 1 for the outer pr=0 #相对于矩形裁剪区域右边,        The position of this point, 0 for the inside, 1 for the outer pb=0 #相对于矩形裁剪区域下边, the position of this point, 0 for the inside, 1 for the outer def __init__ (SELF,X,Y,PL,PT,PR,PB): Self.x=x self.y=y SELF.PL=PL self.pt=pt self.pr=pr self.pb=pbi=point (0,0,0,0,0,0) #函数: Gets the position of the cropped area vertex coordinates relative to the clipping area four edges def J        Udgepositon (I,PRT,PLB): #判断左边 if (i.x<plb.x): i.pl=1 else:i.pl=0 #判断上边 if (I.Y&LT;PRT.Y): I.pt=0 else:i.pt=1 #判断右边 if (i.x<prt.x): i.pr=0 else:i.pr=1 #判断下边 if (i    . Y<plb.y): I.pb=1 else:i.pb=0def Leftclip (prt,plb,*l): l=l[0] l2=[] #左边裁剪 print (l[0].x) For I in Range (0,len (l)): if (i==Len (L)-1): if (l[i].pl==0): #都在内部, leave the second vertex if (l[0].pl==0): L2.ap                    Pend (L[0]) #一内一外, reserved intersection else:k= (L[I].Y-L[0].Y)/(l[i].x-l[0].x)                     b=l[i].y-k*l[i].x x=plb.x y=x*k+b temp=point (x,y,0,0,0,0)                    L2.append (temp) Else: #一外一内, keep the intersection with the inside if (l[0].pl==0):                    k= (L[I].Y-L[0].Y)/(l[i].x-l[0].x) b=l[i].y-k*l[i].x x=plb.x Y=x*k+b temp=point (x,y,0,0,0,0) l2.append (temp) l2.append (l[0                ]) #都在外部, discard all Else:pass else:if (l[i].pl==0):                #都在内部, retains the second vertex if (l[i+1].pl==0): L2.append (l[i+1]) #一内一外, preserving the intersection point         Else           k= (L[I].Y-L[I+1].Y)/(l[i].x-l[i+1].x) b=l[i].y-k*l[i].x x=plb.x                Y=x*k+b temp=point (x,y,0,0,0,0) l2.append (temp) Else:                    #一外一内, reserved intersection with inner if (l[i+1].pl==0): k= (L[I].Y-L[I+1].Y)/(l[i].x-l[i+1].x)                    b=l[i].y-k*l[i].x x=plb.x y=x*k+b temp=point (x,y,0,0,0,0)                     L2.append (temp) l2.append (l[i+1]) #都在外部, discard else: Pass return l2def Topclip (prt,plb,*l): L2=l[0] l=[] for I in range (0,len (L2)): if (i==le N (L2)-1): if (l2[i].pt==0): #都在内部, retains the second vertex if (l2[0].pt==0): L.ap                        Pend (L2[0]) #一内一外, retain the intersection else:if (l2[i].x==l2[0].x):              x=l2[i].x          Y=prt.y temp=point (x,y,0,0,0,0) l.append (temp)                        else:k= (L2[I].Y-L2[0].Y)/(l2[i].x-l2[0].x) b=l2[i].y-k*l2[i].x Y=prt.y if k!=0:x= (y-b)/k temp=p Oint (x,y,0,0,0,0) l.append (temp) Else: #一外一内, preserve the intersection with the inner if (l2[                        0].pt==0): if (l2[i].x==l2[0].x): x=l2[i].x y=prt.y                    Temp=point (x,y,0,0,0,0) l.append (temp) l.append (l2[0]) else:k= (L2[I].Y-L2[0].Y)/(l2[i].x-l2[0].x) B=l2[i].y-k*l2[i]                        . x y=prt.y if k!=0:x= (y-b)/k Temp=point (x,y,0,0,0, 0) L.append (temp) l.append (l2[0]) #都在外部, discard all Else:pass else:if (l2[i].pt==0): #都在内部, leave the second vertex if (l2[i +1].pt==0): L.append (l2[i+1]) #一内一外, preserving the intersection else:if ( l2[i].x==l2[i+1].x): x=l2[i].x y=prt.y temp=point (x, y , 0,0,0,0) l.append (temp) else:k= (L2[I].Y-L2[I+1].Y)/ (l2[i].x-l2[i+1].x) b=l2[i].y-k*l2[i].x Y=prt.y if k! =0:x= (y-b)/k temp=point (x,y,0,0,0,0) l.append (te MP) Else: #一外一内, preserving the intersection with the inner if (l2[i+1].pt==0): if (l2[i].x==l2[i+1].                     x):   x=l2[i].x y=prt.y temp=point (x,y,0,0,0,0) l.append (temp) l.append (l2[i+1]) else:k= (L2[I].Y-L2[I+1].Y)/(l2[                            i].x-l2[i+1].x) b=l2[i].y-k*l2[i].x Y=prt.y if k!=0:                        x= (y-b)/k temp=point (x,y,0,0,0,0) l.append (temp) L.append (l2[i+1]) #都在外部, discard all Else:pass return Ldef Rightclip (prt,plb,*l): L=l[0] l2=[] for I in range (0,len (l)): if (I==len (L)-1): if (l[i].pr= =0): #都在内部, retains the second vertex if (l[0].pr==0): L2.append (l[0]) #一内一外, reserved                    Intersection else:k= (L[I].Y-L[0].Y)/(l[i].x-l[0].x) b=l[i].y-k*l[i].x      X=prt.x              Y=x*k+b temp=point (x,y,0,0,0,0) l2.append (temp) Else:                    #一外一内, reserved intersection with inner if (l[0].pr==0): k= (L[I].Y-L[0].Y)/(l[i].x-l[0].x)                    b=l[i].y-k*l[i].x x=prt.x y=x*k+b temp=point (x,y,0,0,0,0)                    L2.append (temp) l2.append (l[0]) #都在外部, discard else:                    Pass Else:if (l[i].pr==0): #都在内部, keep the second vertex if (l[i+1].pr==0): L2.append (l[i+1]) #一内一外, retain the intersection else:k= (L[I].Y-L[I+1].Y)/(l[i                    ].x-l[i+1].x) b=l[i].y-k*l[i].x X=prt.x y=x*k+b Temp=point (x,y,0,0,0,0) l2.append (temp) Else: #一外一内, preserving the intersection with the inside I        F (l[i+1].pr==0):            k= (L[I].Y-L[I+1].Y)/(l[i].x-l[i+1].x) b=l[i].y-k*l[i].x x=prt.x Y=x*k+b temp=point (x,y,0,0,0,0) l2.append (temp) L2.app     End (L[i+1]) #都在外部, discard Else:pass return l2def bottomclip (prt,plb,*l): L2=L[0] l=[] for I in range (0,len (L2)): if (I==len (L2)-1): if (l2[i].pb==0): #都在内                     , preserving the second vertex if (l2[0].pb==0): L.append (l2[0]) #一内一外, preserving the intersection of else:                        if (l2[i].x==l2[0].x): x=l2[i].x y=plb.y Temp=point (x,y,0,0,0,0) l.append (temp) else:k= (L2                        [I].y-l2[0].y]/(l2[i].x-l2[0].x) b=l2[i].y-k*l2[i].x y=plb.y        If k!=0:                    x= (y-b)/k temp=point (x,y,0,0,0,0) l.append (temp)                        else: #一外一内, preserve the in-intersection if (l2[0].pb==0): if (l2[i].x==l2[0].x):                        x=l2[i].x y=plb.y Temp=point (x,y,0,0,0,0) L.append (temp) l.append (l2[0]) else:k= (L2[I].Y-L2[0].Y)/ (l2[i].x-l2[0].x) b=l2[i].y-k*l2[i].x y=plb.y if k!=0 : x= (Y-b)/k temp=point (x,y,0,0,0,0) l.append (temp ) L.append (l2[0]) #都在外部, discard all Else:pass els E:if (l2[i].pb==0): #都在内部, retain the second vertex if (l2[i+1].pb==0): L.append (l 2[I+1]) #One inside Out, reserved intersection else:if (l2[i].x==l2[i+1].x): x=l2[i].x Y=plb.y temp=point (x,y,0,0,0,0) l.append (temp) El                        se:k= (L2[I].Y-L2[I+1].Y)/(l2[i].x-l2[i+1].x) b=l2[i].y-k*l2[i].x Y=plb.y if k!=0:x= (y-b)/k Temp=poin T (x,y,0,0,0,0) l.append (temp) Else: #一外一内, preserve the intersection with the inner if (l2[i+1                        ].pb==0): if (l2[i].x==l2[i+1].x): x=l2[i].x y=plb.y                    Temp=point (x,y,0,0,0,0) l.append (temp) l.append (l2[i+1]) else:k= (L2[I].Y-L2[I+1].Y)/(l2[i].x-l2[i+1].x) b=l2[i].y-               k*l2[i].x         Y=plb.y if k!=0:x= (y-b)/k temp=point (x,                y,0,0,0,0) l.append (temp) l.append (l2[i+1]) #都在外部, discard all Else:pass return l# The upper-left corner of the window is (0,0), and Pyopengl paint takes the center of the window (0,0) and the maximum x and y coordinates are 100. #因此需要将鼠标获取的坐标转换成PyOpengl画图坐标.        def translate (point): A=WINX/2 B=WINY/2 if Point.x < A and Point.y < b:point.x= (point.x/a) *100-100 point.y=100-(point.y/b) *100 elif point.x > A and Point.y < b:point.x= ((POINT.X-A)/a) *100 PO int.y=100-(point.y/b) *100 elif Point.x < A and Point.y > b:point.x= (point.x/a) *100-100 point.y= (p    Oint.y-b)/b) *100* ( -1) else:point.x= ((point.x-a)/a) *100 point.y= ((point.y-b)/b) *100* ( -1) def display ():              Glclearcolor (0.0,0.0,0.0,0.0) #背景色 = black, Rgba is all 0 glclear (gl_color_buffer_bit) #清除颜色缓冲区 GLCOLOR3FV (COLOR) #设置下面绘制物体的颜色   #f表示参数类型为浮点类型, v indicates that the accepted parameter is in the array form #glRectf ( -10.0,-20.0,20.0,10.0) #画填充矩形 #绘制坐标系 glcolor3f (1.0, 1.0, 1.0) Glbegi N (gl_lines) glvertex2f (0.0, 0.0) glvertex2f (0.0, 95.0) glvertex2f (0.0, 95.0) glvertex2f (4.0, 92.0) glver      tex2f (0.0, 95.0) glvertex2f ( -4.0, 92.0) glvertex2f (0.0, 0.0) glvertex2f (0.0, -95.0) glvertex2f (0.0, 0.0)    GLVERTEX2F (95.0,0.0) glvertex2f (95.0,0.0) glvertex2f (92.0,4.0) glvertex2f (95.0,0.0) glvertex2f (92.0,-4.0)    glvertex2f (0.0, 0.0) glvertex2f ( -95.0,0.0) glend () glcolor3f (0.0,0.0,1.0) glbegin (gl_points);    Glvertex2i (0,0);    Glend (); Glflush () def reshape (winx,winy): Glviewport (0,0,winx,winy) #定义相对视窗大小 (Pixel range) Glmatrixmode (gl_projection) #选择投影                                   Matrix glloadidentity () #读取规范化的单位矩阵 Glortho ( -100.0,100.0, -100.0,100.0, 0.0,100.0) #定义空间坐标系范围 Glmatrixmode (Gl_modelview) #选择模型视图矩阵 glloadidentity () def key (Key,mousex,mousey): GlobAl Color if (key==b ' Q '): #按下q按键时 print (' exit ') Glutdestroywindow (Winid) #根据ID结束窗口 s    Ys.exit () elif (key==b ' e '): L1.clear () print (' Initialize interface ') Glutpostredisplay () #重绘, the display will be called Elif (key==b ' G '): #以绿线画裁剪后图形并输出裁剪后坐标 if l1[0].x<l1[1].x: #矩形裁剪区域: The coordinates of the lower left point Plb=point (l 1[0].x,l1[0].y,0,0,0,0) #矩形裁剪区域: Coordinates prt=point (l1[1].x,l1[1].y,0,0,0,0) else on upper right point: #矩 Shape clipping area: The lower-left point coordinates plb=point (l1[1].x,l1[1].y,0,0,0,0) #矩形裁剪区域: The coordinates of the upper-right point Prt=point (L1[0].X,L1[0].Y, 0,0,0,0) #输出 print (' lower left point of crop area: ') print (PLB.X,PLB.Y) print (' Crop area top right point coordinate: ') print (prt.x,prt        . Y) #被裁剪区域顶点坐标 l=l1[2:] #调用函数, fully initialize the trimmed area vertex coordinates for I in L1:judgepositon (I,PRT,PLB)        #测试输出被裁剪区域顶点坐标 Print ("init:") for I in L:print (I.X,I.Y,I.PL,I.PT,I.PR,I.PB) #左边裁剪 #调用左边裁减函数 L=leftclip (prt,plb,l) #初始化左裁剪后顶点坐标相对裁剪区域的位置 for I in L:judgepositon (I,PRT,PLB) #输出左裁剪后顶点坐标        Print ("Leftclip:") for I in L:print (I.X,I.Y,I.PL,I.PT,I.PR,I.PB) #上边裁剪 #调用上边裁剪函数 L=topclip (prt,plb,l) #初始化上裁剪后顶点坐标相对裁剪区域的位置 for I in L:judgepositon (I,PRT,PLB) #输出上 Cropped vertex coordinates print ("Topclip:") for I in L:print (I.X,I.Y,I.PL,I.PT,I.PR,I.PB) #右边裁剪 #调用右        Edge clipping function L=rightclip (prt,plb,l) #初始化右裁剪后顶点坐标相对裁剪区域的位置 for I in L:judgepositon (I,PRT,PLB)        #输出右裁剪后顶点坐标 print ("Rightclip:") for I in L:print (I.X,I.Y,I.PL,I.PT,I.PR,I.PB) #下边裁剪 #调用下边裁剪函数 L=bottomclip (prt,plb,l) #初始化下裁剪后顶点坐标相对裁剪区域的位置 for I in L:judgepositon (i,p        RT,PLB) #输出下裁剪后顶点坐标 print ("Bottomclip:") for I in L:print (I.X,I.Y,I.PL,I.PT,I.PR,I.PB) #以绿线画裁剪后区域 COlor=green GLCOLOR3FV (color) print (' clip polygon with Green Line ') for I in Range (0,len (l)): if (I==len (l)-1 ): Glbegin (gl_lines) #画线 glvertex2f (L[I].X,L[I].Y) glvertex2f (l[0].x,l[0].y                  ) Glend () Glflush () Else:glbegin (gl_lines) #画线            GLVERTEX2F (L[I].X,L[I].Y) glvertex2f (L[I+1].X,L[I+1].Y) glend () Glflush () Elif (key==b ' R '): #以红色画矩形裁剪窗口 color=red glcolor3fv (color) print (' Rectangle clipping window in red Line ') Glbegi N (gl_lines) #画线 glvertex2f (L1[0].X,L1[0].Y) glvertex2f (L1[0].X,L1[1].Y) glvertex2f (l1[0].x,         L1[1].Y) glvertex2f (L1[1].X,L1[1].Y) glvertex2f (L1[1].X,L1[1].Y) glvertex2f (L1[1].X,L1[0].Y)         GLVERTEX2F (L1[1].X,L1[0].Y) glvertex2f (L1[0].X,L1[0].Y) glend () Glflush () elif (key==b ' B '):      Color=blue  GLCOLOR3FV (color) print (' Crop polygon with Blue Line ') for I in range (2,len (L1)): if (I==len (L1)-1):                  Glbegin (gl_lines) #画线 glvertex2f (L1[I].X,L1[I].Y) glvertex2f (L1[2].X,L1[2].Y) Glend () Glflush () Else:glbegin (gl_lines) #画线 glVertex2 F (l1[i].x,l1[i].y) glvertex2f (L1[I+1].X,L1[I+1].Y) glend () Glflush () Eli        F (key==b ' K '): Print (' Please enter the bottom left point x coordinate: ') lx=float (input ()) print (' Please enter the lower left point y coordinate: ') ly=float (inputs ())        Print (' Please enter the bottom right point x coordinate: ') rx=float (input ()) print (' Please enter the lower right point y-coordinate: ') ry=float (input ()) L1.clear ()        Plb=point (lx,ly,0,0,0,0) prt=point (rx,ry,0,0,0,0) l1.append (PLB) l1.append (PRT) #矩形顶点坐标             For I in L1:print (I.X,I.Y) print ("Input clipped polygon vertex number:") N=int (Input ()) for I in Range (0,n): Print (' Please enter%d vertex x coordinate '% (i+1)) tempx=float (input ()) print (' Please enter%d point y coordinates '% (i+1)) tempy=float (input ()) Temp=point (tempx,tempy,0,0,0,0) l1.append (temp) print (' Press \ ' r\ ' key to draw the cropping area. Print (' Press \ ' b\ ' key to draw the cropped area ') print (' Press \ ' g\ ' key to draw the cropped area ') def Mouse (button,state,x,y): #鼠标左键按下 if button = = G        Lut_left_button and state = = GLUT_DOWN:GLCOLOR3FV (color) temp=point (x,y,0,0,0,0) Translate (temp) L1.append (temp) print (' Instructions: ') print (' 1. Mouse click: ') print (' Click on the mouse to determine the lower-left and lower-right coordinates of the rectangle clipping area, press \ ' R\ ' key to draw the clipping area ') print (' Click on the mouse to determine the coordinates of the cropped area, press \ ' b\ ' key to draw the cropped area ') print (' Press \ ' g\ ' key to draw the cropped area ') print (' Press \ ' e\ ' key Initialization interface ') print (' Press \ ' q\ ' key to exit the program ') print (' Press \ ' K\ ') Key to enter coordinates from the command line to crop ') Glutinit (SYS.ARGV) Glutinitdisplaymode (glut_single| Glut_rgba) glutinitwindowsize (winx,winy) glutinitwindowposition (100,100) Winid=glutcreatewindow (b "main") # The ID of the build window returns to Winidglutdisplayfunc (display) Glutreshapefunc (reshape) glutkeyboardfunc (key) Glutmousefunc (mouse) Glutmainloop ()


Rectangle clipping Algorithm _python3 final version

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.