Demo entry 6351297

1

   

Submitted by anonymous on Mar 20, 2017 at 09:45
Language: C. Code size: 3.1 kB.

//  main.cpp
//  OpenGL
//
//  Created by 陈天乐 on 17/3/17.
//  Copyright (c) 2017年 陈天乐. All rights reserved.

//使用的是mac版OpenGL,所以包含的文件不太一样(之后会提交VSProject文件的)
#include <iostream>
#include <OpenGl/gl.h>
#include <GLUT/GLUT.h>
#include<cmath>

#define width 30 //画面(or国旗)的宽度
#define height 20//画面(or国旗)的高度
#define PI 3.141592

double ratio = sin(PI/10)/sin(7*PI/10);//五角星小圆半径和大圆半径的比值

//绘制五角星的函数
//(x,y)星星圆心坐标,r大圆半径,(x0,y0)是指向点的坐标
void drawStar(double x, double y, double r, double x0, double y0)
{
    glColor3f(1, 1, 0);
    double angle;
    if((x-x0)!=0 && (y-y0)!= 0)
        angle = PI + atan((y-y0)/(x-x0));
    else
        angle = PI / 2;
    //获得第一个角在以圆心为原点的坐标系中的角度
    
    for(int i = 0; i < 5; i++){
        glBegin(GL_QUADS);
        glVertex3f(x + r*ratio*cos(angle - PI/5), y + r*ratio*sin(angle - PI/5), 0);
        //根据角的角度实时计算坐标
        glVertex3f(x + r*cos(angle), y + r*sin(angle), 0);
        glVertex3f(x + r*ratio*cos(angle + PI/5), y + r*ratio*sin(angle + PI/5), 0);
        glVertex3f(x, y, 0);
        glEnd();
        angle = angle + 2*PI/5;
    }
}

//绘制国旗的函数
void displayFlag()
{
    glClear(GL_COLOR_BUFFER_BIT);//清空颜色缓存
    glColor3f(1, 0, 0);
    glBegin(GL_QUADS);
    //画连续的四边形,连续定四个点(三维、float型)
    glVertex3f(0, 20, 0);
    glVertex3f(30, 20, 0);
    glVertex3f(30, 0, 0);
    glVertex3f(0, 0, 0);
    glEnd();
    //调用函数绘制五个星星
    drawStar(5, 15, 3, 5, 15);
    drawStar(10, 11, 1, 5, 15);
    drawStar(12, 13, 1, 5, 15);
    drawStar(12, 16, 1, 5, 15);
    drawStar(10, 18, 1, 5, 15);
    
    glutSwapBuffers();
}




//绘制六芒星的函数,中心点(15,10),
void displayColorful()
{
    //将手动计算的得到的坐标存储在数组中
    double xy1[6][2] = {{15,15},{19,13},{19,7},{15,5},{11,7},{11,13}};
    double xy2[6][2] = {{13.7,13},{16.3,13},{17.7,10},{16.3,7},{13.7,7},{12.3,10}};
    //设置不同颜色
    double color[6][3] = {{1,0,0},{1,1,0},{0,0,1},{0,1,0},{0,1,1},{1,0,1}};
    
    glClear(GL_COLOR_BUFFER_BIT);
    //画连续的四边形,连续定四个点(三维、float型)
    for(int i = 0; i < 6; i++){
        glBegin(GL_QUADS);
        //经过观察,可以看出在六芒星角顶点处颜色更改,分割后的四边形,边界处颜色一致
        //设定颜色,使四边形边界处和上个四边形顶点处一致
        //glColor3f(color[(i+5)%6][0], color[(i+5)%6][1], color[(i+5)%6][2]);
        glColor3f(1,((i+5)%6)*1.0/6,0);
        glVertex3f(xy2[i][0], xy2[i][1], 0);
        //设定颜色,更改颜色
        //glColor3f(color[i][0], color[i][1], color[i][2]);
        glColor3f(1,i*1.0/6,0);
        glVertex3f(xy1[i][0], xy1[i][1], 0);
        glVertex3f(xy2[(i+1) % 6][0], xy2[(i+1)%6][1], 0);
        //设定为白色。让中心可以有白色的感觉
        glColor3f(1, 1, 1);
        glVertex3f(15, 10, 0);
        glEnd();
    }
   
    glutSwapBuffers();
}

int main (int argc,  char *argv[])
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_RGB|GLUT_DOUBLE);
    glutInitWindowPosition(10, 10);
    glutInitWindowSize(600, 400);//国旗是3:2,故调整窗口
    glutCreateWindow("Ken GLUT App");
    
    gluOrtho2D(0, 30, 0, 20);//更换成比较顺手的坐标系
    
    glutDisplayFunc(displayColorful);
    
    glutMainLoop();

    
    return 0;
}

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).