我们在很多地方看到的瓷砖的表面都印有竹叶的十字型,我们用opengl来绘制一个试一试。
#coding=utf-8 from OpenGL.GL import * from OpenGL.GLU import * from OpenGL.GLUT import * import math import sys def init(): glClearColor(1.0, 1.0, 1.0, 1.0) gluOrtho2D(-5.0, 5.0, -5.0, 5.0) #竹叶的十字型,注意是巧用y=x**2和y=math.sqrt(x)两个函数 def plotfunc(): glClear(GL_COLOR_BUFFER_BIT) glColor3f(0.0, 0.0, 0.0) glPointSize(1.0) glBegin(GL_LINES) glVertex2f(-5.0, 0.0) glVertex2f(5.0, 0.0) glVertex2f(0.0, 5.0) glVertex2f(0.0, -5.0) glEnd() x = 0.0 while x <= 1.0: y = x**2 glBegin(GL_POINTS) glVertex2f(x, y) glVertex2f(-x, y) glVertex2f(x, -y) glVertex2f(-x, -y) glEnd() x += 0.01 x = 0.0 while x <= 1.0: y = math.sqrt(x) glBegin(GL_POINTS) glVertex2f(x, y) glVertex2f(-x, y) glVertex2f(x, -y) glVertex2f(-x, -y) glEnd() x += 0.01 glFlush() def main(): glutInit(sys.argv) glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB) glutInitWindowSize(500, 500) glutInitWindowPosition(100, 100) glutCreateWindow("Function plotter") glutDisplayFunc(plotfunc) init() glutMainLoop() main()执行结果如下: