OPengl 方格程序

这是一个使用OpenGL进行方格绘制的C++程序,包含了窗口设置、颜色定义、坐标处理、边表构建、活化边表管理等步骤。程序使用了扫描线填充算法,并支持鼠标操作进行画线、画圆和椭圆等图形绘制。
摘要由CSDN通过智能技术生成

#include "stdafx.h"
#include "iostream"
#include "vector"
#include "algorithm"
#include "GL/glut.h"
#define	 color_0 color(0,0,0)
#define  color_1 color(0.25,0.9,0.8)
#define  color_2 color(0.15,0.8,0.6)
#define color_3 color(1,0,0)
#define color_4 color(0,0,1)
#define color_5 color(1,1,0)
using namespace std;
const unsigned int maxn = 700;
int win_width = 700;
int win_hight = 700;
int flag[maxn];    //用于标记边那些特定的 要y-1的点
int visit[maxn];   //用于在寻找 特定点 时 标记删除掉的点
int point_amount = 0;   //顶点的个数(边的个数)
int  Point[maxn][2];
int EDGE_PRO[maxn][2][2];   //没有处理的边
int minx_y = INT_MAX;
int setpoint_[maxn][maxn];

struct edge {
	float y2;
	float x1;
	float m;
	edge *next;
	friend bool operator <(const edge&e1, const edge&e2) {
		return e1.x1 < e2.x1;
	}
};								//用vector 构建活化边表 
edge *edge_table[maxn];  //  edge_table
struct node {
	int x, y;
	node(int a, int b) {
		x = a, y = b;
	}
};
int ans = 0;
int time = 0;
int keytime = 0;
int time3 = 0;
float x1, y1_, x2, y2;		 //划线算法的 两个端点
float x3, y3, x4, y4, x5, y5, xmove0, ymove0, xmove1, ymove1;
float xr3, yr3;
float r1, c1, r2, c2;
int rx, ry; //画圆算法 中的长轴 短轴
int xc, yc; //画圆算法 中的中心坐标
int  time_mouse_3 = 0;
int xpro, ypro;
int r;
float sf = 1;
int asn = 0;
bool determine = false;
bool check = true;
class color {
public:
	float r, g, b;

	color(float r_, float g_, float b_) {
		r = r_, g = g_, b = b_;
	}
	color() {};
};

class  screen
{
private:
	color p[maxn][maxn];
	int hight;
	int width;
	int length;
	int xo, yo;
public:
	screen(int a, int b, int c, int d, int e);
	void initscreen();
	void setpoint(int x, int y, color rgb);
	void reviselength(int  x);   //修改方格的长度
	int getlength();
	void newxoyo(int x, int y); //修改方格左下角的x,y
	int getxo();
	int getyo();
	int mapped_x(int x);
	int mapped_y(int y);
	void show();
	void filling(int a, int b, int c);

};

screen::screen(int a, int b, int c, int d, int e) {
	hight = a, width = b, length = c, xo = d, yo = e;
	for (int i = 0; i < width; i++)
		for (int j = 0; j < hight; j++) {
			p[i][j] = color_0;
		}
}

int screen::getlength() {
	return length;
}

void screen::initscreen() {
	memset(setpoint_, 0, sizeof setpoint_);
	for (int i = 0; i < width; i++)
		for (int j = 0; j < hight; j++) {
			p[i][j] = color_0;
		}
}

void screen::setpoint(int x, int y, color rgb) {
	if (x < 0 || y < 0 || x >= win_width || y >= win_hight)
		return;
	p[x][y] = rgb;
}

void screen::reviselength(int x) {
	length = length + x;
}

void screen::newxoyo(int x, int y) {
	xo = x;
	yo = y;
}

int screen::getxo() {
	return xo;
}

int screen::getyo() {
	return yo;
}

int screen::mapped_x(int x) {
	return int(float(x - xo) / float(length));
}

int screen::mapped_y(int y) {
	return int(float(700 - y - yo) / float(length));
}

void screen::show() {

	glColor3f(0.5, 0.5, 0.5);
	glBegin(GL_QUADS);
	glVertex2i(xo, yo);
	glVertex2i(xo, yo + hight*length);
	glVertex2i(xo + width*length, yo + hight*length);
	glVertex2i(xo + width*length, yo);
	glEnd();
	glFlush();
	glColor3f(0.4, 0.6, 0.7);
	glBegin(GL_LINES);
	for (int i = 0; i <= hight; i++) {
		glVertex2i(xo, yo + i*length);
		glVertex2i(xo + width*length, yo + i*length);
	}
	for (int i = 0; i <= width; i++) {
		glVertex2i(xo + i*length, yo);
		glVertex2i(xo + i*length, yo + hight*length);
	}
	glEnd();
	glFlush();
	glBegin(GL_QUA
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值