实验名称 计算机图形学软件开发环境
实验目的:
1、熟悉计算机图形学开发环境的配置;
2、在该程序框架下绘制简单图形
任务1:
windows环境下配置计算机图形学模拟系统框架
需要安装软件:
1)Visual studio 20xx
2)Qt
第一步:在VS环境下创建简单的Qt GUI Application程序,创建程序后运行成功,表示该步骤通过。
第二步:在Qt UI下填加菜单,
菜单名为“test“
在项目程序构造函数中添加
connect(ui.actiontest, SIGNAL(triggered()), this, SLOT(test()));
在项目程序中完成测试函数test()
QMessageBox::information(NULL, "Title", "OK");
测试成功,项目框架测试完成。
任务2:绘制一条直线
任务3:绘制一个正六边形
任务4:绘制一个圆形
任务5:绘制一个y= sinx+sin2x的曲线
GraphAigorithm.h:
#ifndef _GRAPHALGORITHM_H
#define _GRAPHALGORITHM_H
#include <QPainter>
#include <algorithm>
//Digital Differential Analyzer
void DDAF(QImage *image, int x1, int y1, int x2, int y2, QRgb value);
void DrawHexagon(QImage *image, int x, int y, int len, QRgb value);
void DrawCircle(QImage *image, int x, int y, int len, QRgb value);
void DrawSin(QImage *image);
#endif
test1.h:
#pragma once
#include <QtWidgets/QMainWindow>
#include "ui_test1.h"
#include <qmessagebox.h>
#include <QPainter>
class test1 : public QMainWindow
{
Q_OBJECT
public:
test1(QWidget *parent = Q_NULLPTR);
void paintEvent(QPaintEvent*);
QPainter *paint;
private:
Ui::test1Class ui;
int state;
public slots:
void test();
void hexagon();
void circle();
void cos();
};
GraphAigorithm.cpp:
#include "GraphAlgorithm.h"
const QRgb value = qRgb(255, 0, 0);
void DDAF(QImage *image, int x1, int y1, int x2, int y2, QRgb value)
{
int x0, y0, a;
float dx, dy, x, y, k1, k2;
dx = x2 - x1;
dy = y2 - y1;
k1 = dy / dx;
k2 = dx / dy;
if (abs(dx) > abs(dy))
{
if (x1 > x2) {
a = x1;
x1 = x2;
x2 = a;
a = y1;
y1 = y2;
y2 = a;
}
y = y1;
for (x0 = x1; x0 <= x2; x0 ++)
{
image -> setPixel(x0, int(y + 0.5), value);
y = y + k1;
}
}
else
{
if (y1 > y2) {
a = y1;
y1 = y2;
y2 = a;
a = x1;
x1 = x2;
x2 = a;
}
x = x1;
for (y0 = y1; y0 <= y2; y0++)
{
image -> setPixel(int(x + 0.5), y0, value);
x = x + k2;
}
}
}
void DrawHexagon(QImage *image, int x, int y, int len, QRgb value)
{
float a, b;
a = len / 2;
b = a / tan(60 * 3.1415926 / 360);
DDAF(image, x - len, y, x - a, y - b, value);
DDAF(image, x - a, y - b, x + a, y - b, value);
DDAF(image, x + a, y - b, x + len, y, value);
DDAF(image, x + len, y, x + a, y + b, value);
DDAF(image, x + a, y + b, x - a, y + b, value);
DDAF(image, x - a, y + b, x - len, y, value);
}
void DDA(QImage *image, int x1, int y1, int x2, int y2)
{
image -> setPixel(x1 + x2, y1 + y2, value);
image -> setPixel(x1 - x2, y1 + y2, value);
image -> setPixel(x1 + x2, y1 - y2, value);
image -> setPixel(x1 - x2, y1 - y2, value);
image -> setPixel(x1 + y2, y1 + x2, value);
image -> setPixel(x1 - y2, y1 + x2, value);
image -> setPixel(x1 + y2, y1 - x2, value);
image -> setPixel(x1 - y2, y1 - x2, value);
}
void DrawCircle(QImage *image, int x, int y, int len, QRgb value)
{
int x1 = 0, y1 = len, a = 1 - len;
DDA(image, x, y, x1, y1);
while (x1 <= y1)
{
if (a >= 0)
{
a = a + 2 * (x1 - y1) + 5;
y1 --;
}
else
{
a = a + 2 * x1 + 3;
}
x1 ++;
DDA(image, x, y, x1, y1);
}
}
void DrawSin(QImage *image)
{
float y = 0, x = 0;
while (x <= 559)
{
y = sin(x * 3.1415926 / 180) + sin(2 * (x * 3.1415926 / 180));
image -> setPixel(x, y * 40 + 300, value);
image -> setPixel(x, y * 40 + 300 - 1, value);
image -> setPixel(x, y * 40 + 300 + 1, value);
image -> setPixel(x, y * 40 + 300 - 2, value);
image -> setPixel(x, y * 40 + 300 + 2, value);
x ++;
}
}
main.cpp:
#include "test1.h"
#include <QtWidgets/QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
test1 w;
w.show();
return a.exec();
}
test1.cpp:
#include "test1.h"
#include "GraphAlgorithm.h"
test1::test1(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
QMessageBox::information(NULL, "Title", "OK");
connect(ui.actionTest1, SIGNAL(triggered()), this, SLOT(test()));
connect(ui.actionhexagon, SIGNAL(triggered()), this, SLOT(hexagon()));
connect(ui.actioncircle, SIGNAL(triggered()), this, SLOT(circle()));
connect(ui.actionsin, SIGNAL(triggered()), this, SLOT(cos()));
state = 0;
}
void test1::paintEvent(QPaintEvent*)
{
QImage image(900, 900, QImage::Format_RGB32);
paint = new QPainter;
paint->begin(this);
switch (state)
{
case 1:
DDAF(&image, 0, 0, 300, 300, qRgb(255, 0, 0));
break;
case 2:
DrawHexagon(&image, 270, 270, 270, qRgb(255, 0, 0));
break;
case 3:
DrawCircle(&image, 210, 210, 150, qRgb(255, 0, 0));
break;
case 4:
DrawSin(&image);
break;
default:
break;
}
paint->drawImage(0, 0, image);
paint->end();
}
void test1::test()
{
state = 1;
repaint();
}
void test1::hexagon()
{
state = 2;
repaint();
}
void test1::circle()
{
state = 3;
repaint();
}
void test1::cos()
{
state = 4;
repaint();
}