实验2 直线生成算法实现

#include<Gl/glut.h>

void LineDDA(int x0, int y0, int x1, int y1 /* int color*/){
    int x, dy, dx, y;
    float m;
    dx = x1 - x0;
    dy = y1 - y0;
    m = dy / dx;
    y = y0;

    glColor3f(1.0f, 1.0f, 0.0f);
    glPointSize(1);
    for(x = x0; x <= x1; x++){
        glBegin(GL_POINTS);
        glVertex2i(x, (int)(y + 0.5));
        glEnd();
        y += m;
    }
}

void myDisplay(void){
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1.0f, 0.0f, 0.0f);
    glRectf(25.0, 25.0, 75.0, 75.0);

    glPointSize(5);
    glBegin(GL_POINT);
    glColor3f(0.0f, 1.0f, 0.0f); glVertex2f(0.0f, 0.0f);
    glEnd();

    LineDDA(0, 0, 200, 300);

    glBegin(GL_LINES);
    glColor3f(1.0f, 0.0f, 0.0f); glVertex2f(100.0f, 0.0f);
    glColor3f(0.0f, 1.0f, 0.0f); glVertex2f(180.0f, 240.0f);
    glEnd();

    glFlush();
}
void Init(){
    glClearColor(0.0, 0.0, 0.0, 0.0);
    glShadeModel(GL_FLAT);
}
void Reshape(int w, int h){
    glViewport(0, 0, (GLsizei)w, (GLsizei) h);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0, (GLdouble)w, 0.0, (GLdouble)h);
}

int main(int argc, char *argv[]){
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(400, 400);
    glutCreateWindow("Hello World!");
    Init();
    glutDisplayFunc(myDisplay);
    glutReshapeFunc(Reshape);
    glutMainLoop();
    return 0;
}

 

 

#include "pch.h"
#include<Gl/glut.h>
#include<iostream>
#include<cmath>
using namespace std;

int colorR, colorG, colorB;
int linewide;

void colormake(int x) {
	if (x == 1) { colorR = 1; colorG = 0; colorB = 0; }
	else if (x == 2) { colorR = 0; colorG = 1; colorB = 0; }
	else if (x == 3) { colorR = 0; colorG = 0; colorB = 1; }
	else if (x == 4) { colorR = 1; colorG = 1; colorB = 0; }
	else if (x == 5) { colorR = 0; colorG = 1; colorB = 1; }
	else if (x == 6) { colorR = 1; colorG = 0; colorB = 1; }
	else if (x == 7) { colorR = 1; colorG = 1; colorB = 1; }
}
void Print() {
	cout << "画线颜色按以下约定:\n";
	cout << "  红    绿     兰     青    黄   白   玫瑰" << endl;
	cout << "1(100);2(010);3(001);4(110);5(011);6(101);7(111);" << endl;
	int color;
	cout << "请输入画线颜色(1 ~ 7):" << endl;
	cin >> color;
	colormake(color);
	cout << "请输入画线宽度:" << endl;
	cin >> linewide;
}
void LineDDA(int x0, int y0, int x1, int y1 /* int color*/) {
	int x, dy, dx, y;
	float m;
	dx = x1 - x0;
	dy = y1 - y0;
	m = dy / dx;
	y = y0;

	glColor3f(colorR, colorG, colorB);
	glPointSize(linewide);
	for (x = x0; x <= x1; x++) {
		glBegin(GL_POINTS);
		glVertex2i(x, (int)(y + 0.5));
		glEnd();
		y += m;
	}
}

void myDisplay(void) {
	glClear(GL_COLOR_BUFFER_BIT);
	glColor3f(1.0f, 0.0f, 0.0f);
	glRectf(25.0, 25.0, 75.0, 75.0);

	glPointSize(5);
	glBegin(GL_POINT);
	glColor3f(0.0f, 1.0f, 0.0f);
	glVertex2f(0.0f, 0.0f);
	glEnd();

	LineDDA(0, 0, 200, 300);

	glBegin(GL_LINES);
	glColor3f(1.0f, 0.0f, 0.0f); glVertex2f(100.0f, 0.0f);
	glColor3f(0.0f, 1.0f, 0.0f); glVertex2f(180.0f, 240.0f);
	glEnd();

	glFlush();
}
void Init() {
	glClearColor(0.0, 0.0, 0.0, 0.0);
	glShadeModel(GL_FLAT);
}
void Reshape(int w, int h) {
	glViewport(0, 0, (GLsizei)w, (GLsizei)h);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluOrtho2D(0.0, (GLdouble)w, 0.0, (GLdouble)h);
}

int main(int argc, char *argv[]) {
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
	glutInitWindowPosition(100, 100);
	glutInitWindowSize(400, 400);
	glutCreateWindow("Hello World!");
	Print();
	Init();
	glutDisplayFunc(myDisplay);
	glutReshapeFunc(Reshape);
	glutMainLoop();
	return 0;
}
#include"pch.h"
#define GLUT_DISABLE_ATEXIT_HACK
#include<Gl/glut.h>
#include<iostream>
#include<cmath>
using namespace std;
//多边形的边数
const GLfloat R = 0.3f;
const int n = 12;
const GLfloat R2 = 0.5f;
const GLfloat pi = acos(-1.0);
int x;

//绘国民党徽
void myDisplay_GM() {
	glClearColor(1.0, 1.0, 1.0, 1.0);
	glClear(GL_COLOR_BUFFER_BIT);
	//画圆
	glBegin(GL_POLYGON);
	glColor3f(0.0f, 0.0f, 0.6f);
	for (int i = 0; i < 100; i++) {
		glVertex2f(R2 * cos(2 * pi / 100 * i), R2 * sin(2 * pi / 100 * i));
	}
	glEnd();
	//画周围的三角形
	glBegin(GL_TRIANGLES);
	glColor3f(1.0f, 1.0f, 1.0f);
	for (int i = 0; i < n; i++) {
		glVertex2f(R * cos(2 * pi / n * (i + 1) - (pi / 12)), R * sin(2 * pi / n * (i + 1) - (pi / 12)));
		glVertex2f(R * cos(2 * pi / n * i - (pi / 12)), R * sin(2 * pi / n * i - (pi / 12)));
		glVertex2f(R2 * cos(2 * pi / n * i), R2 * sin(2 * pi / n * i));
	}
	glEnd();
	//内接的蓝色圆
	glBegin(GL_POLYGON);
	glColor3f(0.0f, 0.0f, 0.6f);
	for (int i = 0; i < 100; i++)
	{
		glVertex2f(R*cos(2 * pi / 100 * i), R*sin(2 * pi / 100 * i));
	}
	glEnd();
	//内接小圆
	glBegin(GL_POLYGON);
	glColor3f(1.0f, 1.0f, 1.0f);
	for (int i = 0; i < 100; i++) {
		glVertex2f(0.9 * R * cos(2 * pi / 100 * i), 0.9 * R * sin(2 * pi / 100 * i));
	}
	glEnd();

	glFlush();
}
//绘共青团徽
void myDisplay_GQ() {
	x = 5;
	glClearColor(1.0, 1.0, 1.0, 1.0);
	glClear(GL_COLOR_BUFFER_BIT);

	//画圆
	glBegin(GL_POLYGON);
	glColor3f(1.0f, 1.0f, 0.0f);
	for (int i = 0; i < 100; i++) {
		glVertex2f(0.8 * cos(2 * pi / 100 * i), 0.8 * sin(2 * pi / 100 * i));
	}
	glEnd();
	//小红色圆
	glBegin(GL_POLYGON);
	glColor3f(1.0f, 0.0f, 0.0f);
	for (int i = 0; i < 100; i++) {
		glVertex2f(0.7 * cos(2 * pi / 100 * i), 0.7 * sin(2 * pi / 100 * i));
	}
	glEnd();
	//黄心
	glBegin(GL_POLYGON);
	glColor3f(1.0f, 1.0f, 0.0f);
	for (int i = 0; i < x; i++) {
		glVertex2f(0.3 * cos(2 * pi / x * i + (pi / 2 - 2 * pi / x) + pi / x), 0.3 * sin(2 * pi / x * i + (pi / 2 - 2 * pi / x) + pi / x));
	}
	glEnd();
	//黄角
	glBegin(GL_TRIANGLES);
	glColor3f(1.0f, 1.0f, 0.0f);
	for (int i = 0; i < x; i++) {
		glVertex2f(0.7 * cos(2 * pi / x * i + (pi / 2 - 2 * pi / x)), 0.7 * sin(2 * pi / x * i + (pi / 2 - 2 * pi / x)));
		glVertex2f(0.3 * cos(2 * pi / x * i + (pi / 2 - 2 * pi / x) + pi / x), 0.3 * sin(2 * pi / x * i + (pi / 2 - 2 * pi / x) + pi / x));
		glVertex2f(0.3 * cos(2 * pi / x * (i - 1) + (pi / 2 - 2 * pi / x) + pi / x), 0.3 * sin(2 * pi / x * (i - 1) + (pi / 2 - 2 * pi / x) + pi / x));
	}
	glEnd();
	//补充黑线
	glBegin(GL_LINES);
	glColor3f(0.0f, 0.0f, 0.0f);
	for (int i = 0; i < x; i++) {
		glVertex2f(0.7 * cos(2 * pi / x * i + (pi / 2 - 2 * pi / x)), 0.7 * sin(2 * pi / x * i + (pi / 2 - 2 * pi / x)));
		glVertex2f(0.0f, 0.0f);

		glVertex2f(0.3 * cos(2 * pi / x * i + (pi / 2 - 2 * pi / x) + pi / x), 0.3 * sin(2 * pi / x * i + (pi / 2 - 2 * pi / x) + pi / x));
		glVertex2f(0.0f, 0.0f);

		glVertex2f(0.3 * cos(2 * pi / x * (i - 1) + (pi / 2 - 2 * pi / x) + pi / x), 0.3 * sin(2 * pi / x * (i - 1) + (pi / 2 - 2 * pi / x) + pi / x));
		glVertex2f(0.0f, 0.0f);

		glVertex2f(0.7 * cos(2 * pi / x * i + (pi / 2 - 2 * pi / x)), 0.7 * sin(2 * pi / x * i + (pi / 2 - 2 * pi / x)));
		glVertex2f(0.3 * cos(2 * pi / x * i + (pi / 2 - 2 * pi / x) + pi / x), 0.3 * sin(2 * pi / x * i + (pi / 2 - 2 * pi / x) + pi / x));

		glVertex2f(0.7 * cos(2 * pi / x * i + (pi / 2 - 2 * pi / x)), 0.7 * sin(2 * pi / x * i + (pi / 2 - 2 * pi / x)));
		glVertex2f(0.3 * cos(2 * pi / x * (i - 1) + (pi / 2 - 2 * pi / x) + pi / x), 0.3 * sin(2 * pi / x * (i - 1) + (pi / 2 - 2 * pi / x) + pi / x));
	}
	glEnd();

	glFlush();
}
//绘牵牛花
void myDisplay_FLOWER()
{

	glClearColor(1.0, 1.0, 1.0, 1.0);
	glClear(GL_COLOR_BUFFER_BIT);
	//红花
	glBegin(GL_POLYGON);
	glColor3f(1.0f, 0.0f, 1.0f);
	for (int i = 0; i < x; i++) {
		glVertex2f(R2 * cos(2 * pi / x * i + (pi / 2 - 2 * pi / x)), R2 * sin(2 * pi / x * i + (pi / 2 - 2 * pi / x)));
	}
	glEnd();
	//白蕊
	glBegin(GL_POLYGON);
	glColor3f(1.0f, 1.0f, 1.0f);
	for (int i = 0; i < x; i++) {
		glVertex2f(0.4 * R2 * cos(2 * pi / x * i + (pi / 2 - 2 * pi / x) + pi / x), 0.4 * R2 * sin(2 * pi / x * i + (pi / 2 - 2 * pi / x) + pi / x));
	}
	glEnd();
	//白角
	glBegin(GL_TRIANGLES);
	glColor3f(1.0f, 1.0f, 1.0f);
	for (int i = 0; i < x; i++) {
		glVertex2f(R2 * cos(2 * pi / x * i + (pi / 2 - 2 * pi / x)), R2 * sin(2 * pi / x * i + (pi / 2 - 2 * pi / x)));
		glVertex2f(0.4 * R2 * cos(2 * pi / x * i + (pi / 2 - 2 * pi / x) + pi / x), 0.4 * R2 * sin(2 * pi / x * i + (pi / 2 - 2 * pi / x) + pi / x));
		glVertex2f(0.4 * R2 * cos(2 * pi / x * (i - 1) + (pi / 2 - 2 * pi / x) + pi / x), 0.4*R2*sin(2 * pi / x * (i - 1) + (pi / 2 - 2 * pi / x) + pi / x));
	}
	glEnd();
	//黑线
	glBegin(GL_LINES);
	glColor3f(0.0f, 0.0f, 0.0f);
	for (int i = 0; i < x; i++) {
		glColor3f(1.0f, 0.0f, 0.0f);
		glVertex2f(R2 * cos(2 * pi / x * i + (pi / 2 - 2 * pi / x)), R2 * sin(2 * pi / x * i + (pi / 2 - 2 * pi / x)));
		glVertex2f(0.0f, 0.0f);

		glColor3f(0.0f, 0.0f, 0.0f);
		glVertex2f(0.4 * R2 * cos(2 * pi / x * i + (pi / 2 - 2 * pi / x) + pi / x), 0.4 * R2 * sin(2 * pi / x * i + (pi / 2 - 2 * pi / x) + pi / x));
		glVertex2f(0.0f, 0.0f);

		glVertex2f(0.4 * R2 * cos(2 * pi / x * (i - 1) + (pi / 2 - 2 * pi / x) + pi / x), 0.4 * R2 * sin(2 * pi / x * (i - 1) + (pi / 2 - 2 * pi / x) + pi / x));
		glVertex2f(0.0f, 0.0f);

		glVertex2f(R2 * cos(2 * pi / x * i + (pi / 2 - 2 * pi / x)), R2 * sin(2 * pi / x * i + (pi / 2 - 2 * pi / x)));
		glVertex2f(0.4 * R2 * cos(2 * pi / x * i + (pi / 2 - 2 * pi / x) + pi / x), 0.4 * R2 * sin(2 * pi / x * i + (pi / 2 - 2 * pi / x) + pi / x));

		glVertex2f(R2 * cos(2 * pi / x * i + (pi / 2 - 2 * pi / x)), R2 * sin(2 * pi / x * i + (pi / 2 - 2 * pi / x)));
		glVertex2f(0.4 * R2 * cos(2 * pi / x * (i - 1) + (pi / 2 - 2 * pi / x) + pi / x), 0.4 * R2 * sin(2 * pi / x * (i - 1) + (pi / 2 - 2 * pi / x) + pi / x));
	}
	glEnd();

	glFlush();
}

int main(int argc, char *argv[])
{
	int chose;
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
	glutInitWindowPosition(300, 100);
	glutInitWindowSize(400, 400);
	glutCreateWindow("Hello World!");


	cout << "1:国民党徽" << endl;
	cout << "2:共青团徽" << endl;
	cout << "3:多瓣牵牛花" << endl;
	cout << "4:退出" << endl;
	cout << "请选择要绘制的图形:";
	cin >> chose;

	if (chose == 1)
		glutDisplayFunc(&myDisplay_GM);							 	//开始画图
	else if (chose == 2)
		glutDisplayFunc(&myDisplay_GQ);								//开始画图
	else if (chose == 3) {
		cout << "请输入花瓣数:";
		cin >> x;
		glutDisplayFunc(&myDisplay_FLOWER);
	}
	glutMainLoop();
	return 0;
}
/*
函数					函数意义
glVertex*()			设置顶点坐标
glColor*()			设置当前颜色
glIndex*()			设置当前颜色表
glNormal*()			设置法向坐标
glCoord*()			产生坐标
glTexCoord*()		设置纹理坐标
glEdgeFlag*()		控制边界绘制
glMaterial*()		设置材质
glVertex3f()表示了该函数属于gl库,参数是三个float型参数指针。我们用glVertex*()来表示这一类函数。

  类型					说明
GL_POINTS			单个顶点集
GL_LINES			多组双顶点线段
GL_POLYGON			单个简单填充凸多边形
GL_TRAINGLES		多组独立填充三角形
GL_QUADS			多组独立填充四边形
GL_LINE_STRIP		不闭合折线
GL_LINE_LOOP		闭合折线
GL_TRAINGLE_STRIP	线型连续填充三角形串
GL_TRAINGLE_FAN		扇形连续填充三角形串
GL_QUAD_STRIP		连续填充四边形串
*/

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值