pyopengl
from OpenGL.GL import *
from OpenGL.GLUT import *
from OpenGL.GLU import *
from math import pi
import numpy as np
import time
a=-2*pi
b=2*pi
c=0
def init():
glClearColor(0,0,0,1)
gluOrtho2D(a,b,a,b)
def drawfunc():
glClear(GL_COLOR_BUFFER_BIT)
#建立坐标系
glColor3f(0,1,0)
glBegin(GL_LINES)
glVertex2f(a,0)
glVertex2f(b,0)
glVertex2f(0,a)
glVertex2f(0,b)
glEnd()
#正弦函数
glColor3f(0,1,1)
glBegin(GL_LINE_STRIP)
for x in np.arange(a,b,0.1):
y=np.sin(x-c)*1
glVertex2f(x,y)
glEnd()
glColor3f(1,0,1)
glBegin(GL_LINE_STRIP)
for x in np.arange(a,b,0.1):
y=np.cos(x+c)*1
glVertex2f(x,y)
glEnd()
glColor3f(1,0,0)
glPolygonMode(GL_FRONT,GL_LINE)
glPolygonMode(GL_FRONT,GL_LINE)
glBegin(GL_QUADS)
glVertex2f(b*0.5,b*0.6)
glVertex2f(b*0.8,b*0.6)
glVertex2f(b*0.8,b*0.8)
glVertex2f(b*0.5,b*0.8)
glEnd()
glFlush()
def update():
global c
if c <= b:
c = c + b/100
else:
c=b/50
time.sleep(0.02)
glutPostRedisplay()
def main():
glutInit()
glutInitDisplayMode(GLUT_RGBA|GLUT_SINGLE)
glutInitWindowSize(500,500)
glutCreateWindow('window CE')
glutDisplayFunc(drawfunc)
init()
glutIdleFunc(update)
glutMainLoop()
main()
运行结果: