CPT205 Lab1 Code Collection

CPT205 Lab1 Code Collection

Hello C++

// Sample code
#include <iostream>
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 <<endl;

    User user1 = {"Lorain", "123456"}; // 局部变量
    // Access to the members in the structure variable Stu1 through the member operator
    cout << "First user is:" << user1.username << "\n ID number is:" << user1.password << endl;

    Student Stu[2] = {
            {"111", "LiSi"},
            {"222", "WangWu"}
    }; // 对象数组

}

Part 1

// File ID: Lab03a.cpp
// Title: Working with Graphics Primitives
// Author: Lorain
#define FREEGLUT_STATIC
#include <GL/freeglut.h>
#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();
}
  • 最终效果展示
    在这里插入图片描述

Part 2

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值