- 两种五角星画法 homework of Computer Graphic
- 花花的三角形, 点和线的像素宽度, 写字
- 同时出现两个窗口 描边
- 交互式画图
- 国民党旗 共青团徽 自定义边数的花
五角星 可以和下面的五角星对比一下
- 参考模板讲解
// #pragma comment (lib, "opengl32.lib")
// #pragma comment (lib, "glu32.lib")
// #pragma comment (lib, "glut32.lib")//Windows VC6.0环用
#include <GL/glut.h>
#include <stdio.h>
// #include <windows.h> // Under Windows to Use Sleep(50)
#include <unistd.h> // 在gcc编译器中,使用的头文件因gcc版本的不同而不同
#include <stdlib.h>
#include <time.h>
#include <iostream>
#include <math.h>
using namespace std;
const double PI = acos(-1.0);
float pCenter[] = {200.0, 200.0}; //五角星中心点坐标
float Length = 100;
float angle = 0; //五角星便宜正方向的角度
float R, G, B;
void myDraw(GLfloat Pos[2], GLfloat Length, GLfloat theta) //Pos[2]=五角星坐标,Length=五角星大圆半径
{
glBegin(GL_LINE_LOOP);
//float Short = Length * (2 - 2 * cos(0.2 * PI));
//Wiki: 五角星 红色/蓝色 = 蓝色/绿色 可以找到大小圆半径的关系
float Short = Length * sin(0.1 * PI) / sin(0.7 * PI);
float beta = 18 + theta; //五角星NE的长半径和x轴的夹角
float p1[2];
glColor3f(R, G, B);
for (int i = 0; i < 5; i++) //从最右边的那个长边顶点开始画
{
p1[0] = Length * cos(i * 2 * 0.2 * PI + beta * 3.1415926 / 180.0) + Pos[0]; //x坐标 18 * 3.1415926 = 56.55
p1[1] = Length * sin(i * 2 * 0.2 * PI + beta * 3.1415926 / 180.0) + Pos[1]; //y坐标
glVertex2fv(p1);
p1[0] = Short * cos((i * 2 + 1) * 0.2 * PI + beta * 3.1415926 / 180.0) + Pos[0];
p1[1] = Short * sin((i * 2 + 1) * 0.2 * PI + beta * 3.1415926 / 180.0) + Pos[1];
glVertex2fv(p1);
}
glEnd();
}
void display()
{
glClearColor(0, 1.0, 0, 0);
glClear(GL_COLOR_BUFFER_BIT);
// Sleep(50); // Windows <windows.h> unsigned Sleep(unsigned millon_seconds);
// sleep(1); // <unistd.h> unsigned sleep(unsigned seconds);
usleep(100000); // <unistd.h> void usleep(int micro_seconds);
srand(time(NULL));
for(int k = 0; k < 10; k++)
{
float i;
i = rand() % 400 + 1; /* i是[1,200]区间内的一个整数 */
pCenter[0] = (float)i;
//srand(time(NULL)); //这里不能写,否则会生成相同的
i = rand() % 200 + 1;
pCenter[1] = (float)i;
//srand(time(NULL));
i = rand() % 200;
i = rand() % 200 + 1;
R = i / 200;
//srand(time(NULL));
i = rand() % 200 + 1;
G = i / 200;
//srand(time(NULL));
i = rand() % 200 + 1;
B = i / 200;
//srand(time(NULL));
i = rand() % 200 + 1;
angle += i;
Length = (float)i;
myDraw(pCenter, Length, angle);
//printf("%f\n", i);
}
glFlush();
}
void Reshape(int w, int h) {
display();
}
void randomChange() {
display();
}
int main(int argc, char* argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_DEPTH | GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize(400, 400);
glutInitWindowPosition(300, 300);
glutCreateWindow("coder352");
gluOrtho2D(0, 400, 0, 400);
glutDisplayFunc(display);
//glutReshapeFunc(Reshape);
glutIdleFunc(randomChange);
glutMainLoop();
return 0;
}
glBegin画各种图形 圆 五角星 菱形 三角形
- 参考模板讲解
#include<GL/glut.h>
#include<math.h>
const GLfloat R = 0.8f;
const GLfloat PI = 3.1415926536f;
const int n = 600;
void basicshape2()
{
int i;
glClearColor(1.0, 1.0, 0.0, 0.8);
glClear(GL_COLOR_BUFFER_BIT);
/* ---------------------- ----------------------*/
glLineWidth(3);
glBegin(GL_LINE_LOOP); //以闭合折线方式画圆
glColor3f(1.0f, 0.0f, 0.0f);
for (i = 0; i < n; ++i)
glVertex2f(R * cos(2 * PI / n * i), R * sin(2 * PI / n * i));
glEnd();
/* ---------------------- ----------------------*/
glBegin(GL_LINE_LOOP); //以闭合折线方式画五角星
glColor3f(0.0f, 1.0f, 0.0f);
GLfloat bx = R * cos(18 * PI / 180);
GLfloat by = R * sin(18 * PI / 180);
GLfloat cx = R * sin(36 * PI / 180);
GLfloat cy = R * cos(36 * PI / 180);
glVertex2f(-bx, by);
glVertex2f(bx, by);
glVertex2f(-cx, -cy);
glVertex2f(0.0f, R);
glVertex2f(cx, -cy);
glEnd();
/* ---------------------- ----------------------*/
glBegin(GL_POLYGON); //以填充方式画菱形,多边形,五角星也是可以的啦~
glVertex2f(-0.8f, 0.95f);
glVertex2f(-0.7f, 0.85f);
glVertex2f(-0.8f, 0.75f);
glVertex2f(-0.9f, 0.85f);
glEnd();
glBegin(GL_POLYGON); //对称一下
glVertex2f(0.8f, 0.95f);
glVertex2f(0.7f, 0.85f);
glVertex2f(0.8f, 0.75f);
glVertex2f(0.9f, 0.85f);
glEnd();
//这里有两个菱形,可以用GL_QUADS一次搞定,独立的~
/* ---------------------- ----------------------*/
glColor3f(0.0f, 0.0f, 1.0f);
glBegin(GL_TRIANGLES); //两个三角形
glVertex2f(-1.0f, -1.0f);
glVertex2f(-0.95f, -0.7f);
glVertex2f(-0.8f, -0.7f);
glVertex2f(-1.0f, -1.0f);
glVertex2f(-0.8f, -0.75f);
glVertex2f(-0.8f, -0.95f);
glEnd();
/* ---------------------- ----------------------*/
glFlush(); //最后flush一下
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowPosition(150, 150);
glutInitWindowSize(500, 500);
glutCreateWindow("Baisc Shape2");
glutDisplayFunc(basicshape2);
glutMainLoop();
return 0;
}
小图形
- 花花的三角形, 点和线的像素宽度, 写字
#include<GL/glut.h>
#pragma comment(linker,"/subsystem:\"windows\" /entry:\"mainCRTStartup\"")
void mydisplay(void)
{
glClearColor(0.0, 1.0, 0.0, 0.0); //设置背景为绿色
glClear(GL_COLOR_BUFFER_BIT);
//在左上角画一个黄色的矩形
glColor3f(1.0f, 1.0f, 0.0f);
glRectf(-0.9f, 0.0f, 0.0f, 0.9f); //分别是左下角和右上角两点坐标
//在右上角画一个花花的三角形
glBegin(GL_TRIANGLES);
glColor3f(1.0f, 0.0f, 0.0f); glVertex2f(0.9f, 0.0f);
glColor3f(0.0f, 1.0f, 0.0f); glVertex2f(0.5f, 0.6f);
glColor3f(0.0f, 0.0f, 1.0f); glVertex2f(0.1f, 0.0f);
glEnd();
//画两个点
glPointSize(5); //表示点的像素
glBegin(GL_POINTS);
glColor3f(0.0f, 0.0f, 0.0f); glVertex2f(0.05f, 0.0f);
glColor3f(1.0f, 0.0f, 0.0f); glVertex2f(0.05f, -0.5f);
glEnd();
//写一个我的"可"字
glLineWidth(5); //表示线的宽度
glBegin(GL_LINES);
glColor3f(1.0f, 1.0f, 0.0f); glVertex2f(0.5f, -0.5f);
glColor3f(1.0f, 1.0f, 0.0f); glVertex2f(0.8f, -0.5f);
glColor3f(1.0f, 1.0f, 0.0f); glVertex2f(0.75f, -0.5f);
glColor3f(1.0f, 1.0f, 0.0f); glVertex2f(0.75f, -0.8f);
glEnd();
glColor3f(1.0f, 1.0f, 0.0f);
glRectf(0.55f, -0.7f, 0.7f, -0.55f);
glFlush();
}
int main(int argc, char *argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowPosition(100, 100);
glutInitWindowSize(600, 600);
glutCreateWindow("Basic Shape");
glutDisplayFunc(mydisplay);
glutMainLoop();
return 0;
}
升级2 更加对称的图形
- 同时出现两个窗口 描边
#include<GL/glut.h>
#include<math.h>
#pragma comment(linker,"/subsystem:\"windows\" /entry:\"mainCRTStartup\"")
const float R1 = 0.8f;
const float R2 = 0.4f;
const float PI = 3.1415926;
void myDisplay_1()
{
glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
//*****下面开始画矩形和三角形
glColor3f(1.0f, 0.0f, 0.0f);
glBegin(GL_POLYGON);
glVertex2f(0.4f, 0);
glVertex2f(0, 0.4f);
glVertex2f(-0.4f, 0);
glVertex2f(0, -0.4f);
glEnd();
glBegin(GL_TRIANGLES); //四个三角形
glColor3f(1.0f, 1.0f, 0.0f);
glVertex2f(0.4f, -0.4f);
glVertex2f(0.8f, 0);
glVertex2f(0.4f, 0.4f);
glColor3f(0.0f, 0.0f, 1.0f);
glVertex2f(0.4f, 0.4f);
glVertex2f(-0.4f, 0.4f);
glVertex2f(0, 0.8f);
glColor3f(1.0f, 0.0f, 1.0f);
glVertex2f(-0.4f, 0.4f);
glVertex2f(-0.4f, -0.4f);
glVertex2f(-0.8f, 0);
glColor3f(0.0f, 1.0f, 0.0f);
glVertex2f(-0.4f, -0.4f);
glVertex2f(0.4f, -0.4f);
glVertex2f(0, -0.8f);
glEnd();
//******描边
glColor3f(0.0f, 0.0f, 0.0f);
glBegin(GL_LINE_LOOP);
glVertex2f(0.4f, 0); //矩形的边
glVertex2f(0, 0.4f);
glVertex2f(-0.4f, 0);
glVertex2f(0, -0.4f);
glEnd();
glBegin(GL_LINE_LOOP);//三角形的边,可当作两个矩形
glVertex2f(0.8f, 0);
glVertex2f(0.4f, -0.4f);
glVertex2f(0, -0.8f);
glVertex2f(-0.4f, -0.4f);
glVertex2f(-0.8f, 0);
glVertex2f(-0.4f, 0.4f);
glVertex2f(0, 0.8f);
glVertex2f(0.4f, 0.4f);
glVertex2f(0.4f, -0.4f);
glVertex2f(-0.4f, -0.4f);
glVertex2f(-0.4f, 0.4f);
glVertex2f(0.4f, 0.4f);
glEnd();
glFlush();
}
void myDisplay_2()
{
glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
for (int i = 0; i <= 270; i += 90) {
glColor3f(1.0f, 0.0f, 0.0f);
glBegin(GL_POLYGON);
glVertex2f(R1 * cos(i * PI / 180), R1 * sin(i * PI / 180));
glVertex2f(R2 * cos((i + 25)*PI / 180), R2 * sin((i + 25)*PI / 180));
glVertex2f(0, 0);
glVertex2f(R2 * cos((i - 25)*PI / 180), R2 * sin((i - 25)*PI / 180));
glEnd();
glColor3f(0.0f, 0.0f, 0.0f);
glBegin(GL_LINE_LOOP);
glVertex2f(R1 * cos(i * PI / 180), R1 * sin(i * PI / 180));
glVertex2f(R2 * cos((i + 25)*PI / 180), R2 * sin((i + 25)*PI / 180));
glVertex2f(0, 0);
glVertex2f(R2 * cos((i - 25)*PI / 180), R2 * sin((i - 25)*PI / 180));
glEnd();
}
for (int i = 45; i <= 315; i += 90) {
glColor3f(0.0f, 1.0f, 0.0f);
glBegin(GL_POLYGON);
glVertex2f(R1 * cos(i * PI / 180), R1 * sin(i * PI / 180));
glVertex2f(R2 * cos((i + 20)*PI / 180), R2 * sin((i + 20)*PI / 180));
glVertex2f(0, 0);
glVertex2f(R2 * cos((i - 20)*PI / 180), R2 * sin((i - 20)*PI / 180));
glEnd();
glColor3f(0.0f, 0.0f, 0.0f);
glBegin(GL_LINE_LOOP);
glVertex2f(R1 * cos(i * PI / 180), R1 * sin(i * PI / 180));
glVertex2f(R2 * cos((i + 20)*PI / 180), R2 * sin((i + 20)*PI / 180));
glVertex2f(0, 0);
glVertex2f(R2 * cos((i - 20)*PI / 180), R2 * sin((i - 20)*PI / 180));
glEnd();
}
glFlush();
}
int main(int argc, char * argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowPosition(100, 100);
glutInitWindowSize(500, 500);
glutCreateWindow("sheng2_1");
glutDisplayFunc(myDisplay_1);
glutInitWindowPosition(700, 100);
glutInitWindowSize(500, 500);
glutCreateWindow("sheng2_2");
glutDisplayFunc(myDisplay_2);
glutMainLoop();
return 0;
}
交互式画图
#include<GL/glut.h>
#include<stdio.h>
int col;
int p;
void mydisplay()
{
glShadeModel(GL_FLAT);
glClearColor(0.0f, 1.0f, 1.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glColor3f(0.5f, 0.5f, 0.5f);
glRectf(-0.9, -0.9f, -0.7f, -0.7f);
switch (col)
{
case 1: glColor3f(1.0f, 0.0f, 0.0f); break;
case 2: glColor3f(0.0f, 1.0f, 0.0f); break;
case 3: glColor3f(0.0f, 0.0f, 1.0f); break;
case 4: glColor3f(1.0f, 1.0f, 0.0f); break;
case 5: glColor3f(1.0f, 0.0f, 1.0f); break;
case 6: glColor3f(0.0f, 1.0f, 1.0f); break;
default: printf("没有此种颜色!\n");
}
glLineWidth(p);
glBegin(GL_LINES);
glVertex2i(-1.0f, -1.0f);
glVertex2i(0, 0);
glEnd();
glLineWidth(1);
glColor3f(0.0f, 0.0f, 1.0f);
glBegin(GL_LINES);
glVertex2f(-0.6f, -1.0f);
glVertex2f(-0.7f, 0.2f);
glEnd();
glPointSize(10);
glColor3f(1.0f, 1.0f, 0.0f);
glBegin(GL_POINTS);
glVertex2f(-1.0f, -1.0f);
glEnd();
glFlush();
}
int main(int argc, char * argv[])
{
glutInit(&argc, argv);
printf("请选择线的颜色\n");
printf("1.红色 2.绿色 3.蓝色 4.黄色 5.梅红色 6.青色\n");
scanf("%d", &col);
printf("请输入线的宽度:\n");
scanf("%d", &p);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowPosition(800, 100);
glutInitWindowSize(400, 400);
glutCreateWindow("jiben2");
glutDisplayFunc(mydisplay);
glutMainLoop();
return 0;
}
国民党旗 共青团徽 自定义边数的花
- 有BUG,待完善
#include<stdio.h>
#include<GL/glut.h>
#include<math.h>
//#pragma comment(linker,"/subsystem:\"windows\" /entry:\"mainCRTStartup\"")
const double PI = 3.14159265358979f;
const float R = 0.8f;
const float R2 = 0.48f;
const float R3 = 0.45f;
const float R4 = 0.42f;
const float r2 = 0.7f;
//const float r3 = r2*sin(36*PI/180)*2*0.382;//0.618黄金分割
const float r3 = r2 * sin(36 * PI / 180) / sin(72 * PI / 180) * 0.618;
int vn;
//国民党党旗
void GMD()
{
glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POLYGON); //画背景的蓝色圆
glColor3f(0.0f, 0.0f, 1.0f);
for (float i = 0; i <= 360; i += 0.1)
glVertex2f(R * cos(i * PI / 180), R * sin(i * PI / 180));
glEnd();
//画12个顶点的太阳
float p[30][2];
int j = 0;
for (int i = 0; i <= 360; i += 30) {
p[j][0] = R * cos(i * PI / 180);
p[j][1] = R * sin(i * PI / 180);
j = j + 2;
}
j = 1;
for (int i = 15; i <= 360; i += 30) {
p[j][0] = R2 * cos(i * PI / 180);
p[j][1] = R2 * sin(i * PI / 180);
j = j + 2;
}
glLineWidth(2);
glColor3f(1.0f, 1.0f, 1.0f);
for (j = 1; j <= 23; j++) { //填充
glBegin(GL_POLYGON);
glVertex2f(0, 0);
glVertex2f(p[abs(j + 1) % 24][0], p[abs(j + 1) % 24][1]);
glVertex2f(p[j][0], p[j][1]);
glVertex2f(p[abs(j - 1) % 24][0], p[abs(j - 1) % 24][1]);
glEnd();
}
//画中间的两个圆
glBegin(GL_POLYGON);
glColor3f(0.0f, 0.0f, 1.0f);
for (int i = 0; i <= 360; i += 0.1)
glVertex2f(R3 * cos(i * PI / 180), R3 * sin(i * PI / 180));
glEnd();
glBegin(GL_POLYGON);
glColor3f(1.0f, 1.0f, 1.0f);
for (int i = 0; i <= 360; i += 0.1)
glVertex2f(R4 * cos(i * PI / 180), R4 * sin(i * PI / 180));
glEnd();
glFlush();
}
//******团徽
void GQT()
{
glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
glBegin(GL_POLYGON); //画黄色背景圆
glColor3f(1.0f, 1.0f, 0.0f);
for (float i = 0; i <= 360; i += 0.1)
glVertex2f(R * cos(i * PI / 180), R * sin(i * PI / 180));
glEnd();
glBegin(GL_POLYGON); //画红色背景圆
glColor3f(1.0f, 0.0f, 0.0f);
for (int i = 0; i <= 360; i += 0.1)
glVertex2f(r2 * cos(i * PI / 180), r2 * sin(i * PI / 180));
glEnd();
float p[10][2];
int j = 0;
for (int i = 90; i <= 270; i += 72) {
p[j][0] = r2 * cos(i * PI / 180);
p[j++][1] = r2 * sin(i * PI / 180);
p[j][0] = r3 * cos((i + 36) * PI / 180);
p[j][1] = r3 * sin((i + 36) * PI / 180);
j++;
}
j = j--;
printf("%d", j);
p[6][0] = -p[4][0];
p[6][1] = p[4][1];
p[7][0] = -p[3][0];
p[7][1] = p[3][1];
p[8][0] = -p[2][0];
p[8][1] = p[2][1];
p[9][0] = -p[1][0];
p[9][1] = p[1][1];
glColor3f(1.0f, 1.0f, 0.0f);
glBegin(GL_POLYGON);
for (j = 9; j >= 0; j--)
glVertex2f(p[j][0], p[j][1]);
glEnd();
glLineWidth(2);
//描边
glColor3f(0.0f, 0.0f, 0.0f);
glBegin(GL_LINE_LOOP);
for (j = 0; j <= 9; j++)
glVertex2f(p[j][0], p[j][1]);
glEnd();
//从原点连线
glColor3f(0.0f, 0.0f, 0.0f);
for (j = 0; j <= 9; j++) {
glBegin(GL_LINES);
glVertex2f(0, 0);
glVertex2f(p[j][0], p[j][1]);
glEnd();
}
glFlush();
}
//****花型图案
void Flower()
{
glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
glClear(GL_COLOR_BUFFER_BIT);
double p[201][2];
int j = 0;
float add = 180 / (vn / 2 + 0.5);
float add2 = add / 2;
for (float i = 90; i <= 270; i += add) {
p[j][0] = R * cos(i * PI / 180);
p[j++][1] = R * sin(i * PI / 180);
p[j][0] = R4 * cos((i + add2) * PI / 180);
p[j++][1] = R4 * sin((i + add2) * PI / 180);
p[j][0] = R4 * cos((i + add2) * PI / 180);
p[j--][1] = R4 * sin((i + add2) * PI / 180);
}
for (int k = j;; k++)
{
if (j < 0) break;
p[k][0] = -p[j][0];
p[k][1] = p[j][1];
j--;
} //至此,点全部计算存储完毕~,注意从90度的点开始存储,然后对称右半部分
k--;
//画最外边的多边形
glColor3f(1.0f, 0.0f, 1.0f);
glBegin(GL_POLYGON);
for (j = 0; j <= k; j = j + 2)
glVertex2f(p[j][0], p[j][1]);
glEnd();
//描多边形的边
glColor3f(0.0f, 0.0f, 0.0f);
glBegin(GL_LINE_STRIP);
for (j = 0; j <= k; j = j + 2)
glVertex2f(p[j][0], p[j][1]);
glEnd();
//画角
glColor3f(1.0f, 1.0f, 1.0f);
for (j = 0; j <= k; j = j + 2) {
glBegin(GL_POLYGON);
glVertex2f(0, 0);
glVertex2f(p[abs(j + 1) % k][0], p[abs(j + 1) % k][1]);
glVertex2f(p[j][0], p[j][1]);
glVertex2f(p[abs(j - 1) % k][0], p[abs(j - 1) % k][1]);
glEnd();
}
//为里面的几个尖角描出边界
glLineWidth(1);
glColor3f(0.0f, 0.0f, 0.0f);
glBegin(GL_LINE_STRIP);
for (j = 0; j <= k; j++)
glVertex2f(p[j][0], p[j][1]);
glEnd();
//从原点向各顶点连线
for (j = 0; j < k; j++)
{
if (j % 2) glColor3f(1.0f, 0.0f, 0.0f);
else glColor3f(0.0f, 0.0f, 0.0f);
glBegin(GL_LINES);
glVertex2f(0, 0);
glVertex2f(p[j][0], p[j][1]);
glEnd();
}
glFlush();
}
int main(int argc, char * argv[])
{
glutInit(&argc, argv);
glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
glutInitWindowPosition(0, 100);
glutInitWindowSize(500, 500);
glutCreateWindow("sheng2_1");
glutDisplayFunc(GMD);
glutInitWindowPosition(900, 100);
glutInitWindowSize(500, 500);
glutCreateWindow("sheng2_2");
glutDisplayFunc(GQT);
printf("输入顶点数可选择花型图案的顶点数!(5~100)\n\n");
scanf("%d", &vn);
glutInitWindowPosition(450, 100);
glutInitWindowSize(500, 500);
glutCreateWindow("sheng2_1");
glutDisplayFunc(Flower);
glutMainLoop();
return 0;
}
/*
Rshape是不能用在这里的,它起到刷新的作用
用算法来实现的必须要用,因为每一帧都要刷新
用库函数画的就不必了
*/
时间: 2024-10-11 23:29:25