CPT205 Lab1 Code Collection

CPT205 Lab1 Code Collection

Hello C++

// Sample code
#include 
using namespace std;
const double pi = 3.14159;

struct User {
    char username[10];
    char password[20];
};

struct Student {
    char id[20];
    char name[10];
} stu1 = {"114514", "ZhangSan"}; // 全局变量

int main ()
{
//    double r; // radius
//    double circle; // circumference of circle
//    cout << "Please enter radius: ";
//    cin >> r;
//    circle = 2 * pi * r;
//    cout << "Circumference of circle is: " << circle <

Part 1

// File ID: Lab03a.cpp
// Title: Working with Graphics Primitives
// Author: Lorain
#define FREEGLUT_STATIC
#include 
#include "math.h"
void define_to_OpenGL();
///
int main(int argc, char** argv)
{
	glutInit(&argc, argv);
	// Task 2
	glutCreateWindow("Graphics Primitives");
	glutDisplayFunc(define_to_OpenGL);
	glutMainLoop();
}
///
void define_to_OpenGL()
{
	glClearColor(1, 1, 1, 1);
	glClear(GL_COLOR_BUFFER_BIT);
	// The stuff to appear on screen goes here

	// Task 2 - Set the Dimensions
	glutInitWindowSize(600, 400);
	glutInitWindowPosition(50, 50);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluOrtho2D(-100, +500, -200, 200);

	// Task 3 - Draw the Axes
	glLineWidth(1.0);
	glColor3f(0, 0, 0);
	// 画一条线(x轴)
	glBegin(GL_LINES); // 括号中定义绘制图形的类型(e.g. 点,线,三角形...)
		glVertex2f(0.0, 0.0); // start location
		glVertex2f(450.0, 0.0); // end location
	glEnd();

	// 再画一条线(y轴)
	glBegin(GL_LINES);
		glVertex2f(0.0, -150.0); // start location
		glVertex2f(0.0, +150.0); // end location
	glEnd();

	// Task 4 - Draw a Dot at the Origin
	glPointSize(10);
	glColor3f(255, 255, 0); // 如果需要对颜色进行更改每次需要重新定义
	glBegin(GL_POINTS);
		glVertex2f(0, 0);
	glEnd();
	 
	// Task 5 - Plot a Sine Wave
	// draw a sine wave 绘制正弦波
	int i;
	float x, y;
	glColor3f(0.0, 0.0, 1.0);
	glPointSize(1);
	glBegin(GL_POINTS);
	for (i = 0; i < 361; i = i + 5)
	{
		x = (float)i;
		y = 100.0 * sin(i * (6.284 / 360.0));
		glVertex2f(x, y);
	}
	glEnd();

	// Tasks 6, 7 and 8
	// Task 6 - Draw a Triangle
	// Task 7 - Draw A Multi-coloured Triangle
	// Task 8 - Draw a Single-coloured Triangle
	
	//glShadeModel(GL_FLAT); // 如果想要绘制单色的图形,告诉OpenGL关闭平滑功能即可
	glBegin(GL_TRIANGLES);
		glColor3f(255, 255, 0); // 这里对三角形每一个角的颜色进行定义
		glVertex2f(-50, 50);

		glColor3f(255, 0, 255);
		glVertex2f(-50, 0);

		glColor3f(0, 0, 255);
		glVertex2f(0, 0);
	glEnd();

	glFlush();
}
  • 最终效果展示
    CPT205 Lab1 Code Collection_第1张图片

Part 2

你可能感兴趣的:(课堂笔记,c++,经验分享)