python使用opengl绘制竹叶的十字型

论坛 期权论坛 脚本     
匿名网站用户   2020-12-20 02:46   390   0

我们在很多地方看到的瓷砖的表面都印有竹叶的十字型,我们用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()
执行结果如下:

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:1136255
帖子:227251
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP