项目地址
C++实验作业描述
Lab01
主程序
project.cpp
#include "classes.h"
void mainloop() {
int x = 0;
Rectangular aline(Point(500,0),Point(520,500),Color(16777215, true));
Rectangular rec;
Circle cir;
for (int i=0; is_run(); delay_fps(60),(i=(i+1)% 16777216)) {
x = (x + 10) % 250;
aline.drawRectangular();
Rectangular(Point(250 - x, 250 - x), Point(250 + x, 250 + x), Color(random(16777216), false)).drawRectangular();
Circle(Point(770, 250),x, Color(random(16777216), false)).drawCircle();
}
}
int main() {
DrawControl board(1020, 500);
mainloop();
board.closeBoard();
}
包含所有头文件及所需要的库
classes.h
#pragma once
#include "graphics.h"
#include <string.h>
#include <string>
#include "DrawControl.h"
#include "Color.h"
#include "Point.h"
#include "Circle.h"
#include "Rectangular.h"
using namespace std;
绘图控制类
DrawControl.h
#pragma once
class DrawControl
{
public:
DrawControl();
DrawControl(int x, int y);
void closeBoard();
~DrawControl();
};
DrawControl.cpp
#include "classes.h"
DrawControl::DrawControl()
{
setinitmode(INIT_ANIMATION);
initgraph(640, 480);
randomize();
}
DrawControl::DrawControl(int x = 640, int y = 480)
{
setinitmode(INIT_ANIMATION);
initgraph(x, y);
randomize();
}
void DrawControl::closeBoard()
{
ege::getch();
closegraph();
}
DrawControl::~DrawControl()
{
ege::getch();
closegraph();
}
点类
Point.h
#pragma once
class Point
{
private:
int x_, y_;
public:
Point();
Point(int x, int y);
int getX();
int getY();
void setX(int x);
void setY(int y);
Point(const Point &);
~Point();
};
Point.cpp
#include "classes.h"
Point::Point()
{
x_ = 0;
y_ = 0;
}
Point::Point(int x, int y)
{
x_ = x;
y_ = y;
}
int Point::getX()
{
return x_;
}
int Point::getY()
{
return y_;
}
void Point::setX(int x)
{
x_ = x;
}
void Point::setY(int y)
{
y_ = y;
}
Point::Point(const Point & _Point)
{
this->x_ = _Point.x_;
this->y_ = _Point.y_;
}
Point::~Point()
{
}
颜色类
Color.h
#pragma once
class Color
{
private:
color_t color_;
bool isFilledColor_;
public:
Color();
Color(color_t,bool);
~Color();
void setColor(color_t color);
void setIsFilledColor(bool isFilledColor);
color_t getColor();
bool getIsFilledColor();
Color(const Color&);
};
Color.cpp
#include "classes.h"
Color::Color()
{
}
Color::Color(color_t color, bool isFilledColor)
{
this->color_ = color;
this->isFilledColor_ = isFilledColor;
}
Color::~Color()
{
}
void Color::setColor(color_t color)
{
color_ = color;
}
void Color::setIsFilledColor(bool isFilledColor)
{
isFilledColor_ = isFilledColor;
}
color_t Color::getColor()
{
return color_;
}
bool Color::getIsFilledColor()
{
return isFilledColor_;
}
Color::Color(const Color & _color)
{
this->color_ = _color.color_;
this->isFilledColor_ = _color.isFilledColor_;
}
圆类
Circle.h
#pragma once
class Circle
{
private:
int radius_;
Color color_;
Point center_;
public:
Circle();
Circle(Point center,int radius,Color color);
Point getCenter();
int getRadius();
Color getColor();
void setCenter(Point center);
void setRadius(int radius);
void setColor(Color color);
void drawCircle();
~Circle();
};
Circle.cpp
#include "classes.h"
Circle::Circle()
{
this->center_ = Point();
this->radius_ = 0;
this->color_ = Color();
}
Circle::Circle(Point center, int radius, Color color)
{
this->center_ = center;
this->radius_ = radius;
this->color_ = color;
}
Point Circle::getCenter()
{
return this->center_;
}
int Circle::getRadius()
{
return this->radius_;
}
Color Circle::getColor()
{
return this->color_;
}
void Circle::setCenter(Point center)
{
this->center_ = center;
}
void Circle::setRadius(int radius)
{
this->radius_ = radius;
}
void Circle::setColor(Color color)
{
this->color_ = color;
}
void Circle::drawCircle()
{
if (this->color_.getIsFilledColor() == false) {
setcolor(this->color_.getColor());
circle(this->center_.getX(), this->center_.getY(), this->radius_);
}
else {
setcolor(this->color_.getColor());
setfillcolor(this->color_.getColor());
fillellipse(this->center_.getX(), this->center_.getY(), this->radius_, this->radius_);
}
}
Circle::~Circle()
{
}
矩形类
Rectangular.h
#pragma once
class Rectangular
{
private:
Point point1_;
Point point2_;
Color color_;
public:
Rectangular();
Rectangular(Point point1,Point point2,Color color);
void setPoint1(Point);
void setPoint2(Point);
void setColor(Color);
Point getPoint1();
Point getPoint2();
Color getColor();
void drawRectangular();
~Rectangular();
};
Rectangular.cpp
#include "classes.h"
Rectangular::Rectangular()
{
this->point1_ = Point();
this->point2_ = Point();
this->color_ = Color();
}
Rectangular::Rectangular(Point point1, Point point2, Color color)
{
this->point1_ = point1;
this->point2_ = point2;
this->color_ = color;
}
void Rectangular::setPoint1(Point _point)
{
this->point1_ = _point;
}
void Rectangular::setPoint2(Point _point)
{
this->point2_ = _point;
}
void Rectangular::setColor(Color _color)
{
this->color_ = _color;
}
Point Rectangular::getPoint1()
{
return this->point1_;
}
Point Rectangular::getPoint2()
{
return this->point2_;
}
Color Rectangular::getColor()
{
return this->color_;
}
void Rectangular::drawRectangular()
{
if (this->color_.getIsFilledColor() == false) {
setcolor(this->color_.getColor());
rectangle(this->point1_.getX(), this->point1_.getY(), this->point2_.getX(), this->point2_.getY());
}
else {
setcolor(this->color_.getColor());
setfillcolor(this->color_.getColor());
bar(this->point1_.getX(), this->point1_.getY(), this->point2_.getX(), this->point2_.getY());
}
}
Rectangular::~Rectangular()
{
}
Lab02
主程序
project.cpp
#include <string>
#include "classes.h"
Circle *circles[30];
Rectangular *rectangulars[30];
void mainloop2();
void exit(DrawControl *board) {
for (int i = 0; i < 30; i++) {
delete circles[i];
delete rectangulars[i];
}
delete board;
}
void mainloop1() {
DrawControl *board = reinterpret_cast<DrawControl*>(new DrawControl(1020, 600));
int x = 0, now = 25;
memset(circles, 0, sizeof(circles));
memset(rectangulars, 0, sizeof(rectangulars));
for (int i = 1; i <= 24; i++) {
rectangulars[i] = reinterpret_cast<Rectangular*>(new Rectangular(Point(250 - i * 10, 250 - i * 10), Point(250 + i * 10, 250 + i * 10), Color(random(16777216), false)));
circles[i] = reinterpret_cast<Circle*>(new Circle(Point(770, 250), i * 10, Color(random(16777216), false)));
}
Rectangular *aline= reinterpret_cast<Rectangular*>(new Rectangular(Point(500, 0), Point(520, 500), Color(16777215, true)));
Rectangular *anotherline= reinterpret_cast<Rectangular*>(new Rectangular(Point(0, 500), Point(1020, 505), Color(16777215, true)));
for (int i = 0; is_run(); delay_fps(60), (i = (i + 1) % 16777216)) {
for (int j = 0; j < 30; j++) {
if (circles[j] != 0) {
circles[j]->setRadius(circles[j]->getRadius() - 1);
rectangulars[j]->setPoint1(Point(rectangulars[j]->getPoint1().getX() + 1, rectangulars[j]->getPoint1().getY() + 1));
rectangulars[j]->setPoint2(Point(rectangulars[j]->getPoint2().getX() - 1, rectangulars[j]->getPoint2().getY() - 1));
}
}
for (int j = 0; j < 30; j++) {
if (circles[j] != 0) {
if (circles[j]->getRadius() == 0) {
delete circles[j];
circles[j] = 0;
delete rectangulars[j];
rectangulars[j] = 0;
rectangulars[now] = reinterpret_cast<Rectangular*>(new Rectangular(Point(10, 10), Point(490, 490), Color(random(16777216), false)));
circles[now] = reinterpret_cast<Circle*>(new Circle(Point(770, 250), 240, Color(random(16777216), false)));
now = (now + 1) % 30;
}
}
}
aline->draw();
anotherline->draw();
for (int j = 0; j < 30; j++) {
if (circles[j] != 0) {
circles[j]->draw();
rectangulars[j]->draw();
}
}
xyprintf(10, 510, "Number of Circle=%d", Circle::getCircleNum());
xyprintf(10, 530, "Number of Rectangular=%d", Rectangular::getRectangularNum());
xyprintf(10, 550, "Number of Point=%d", Point::getPointNum());
xyprintf(10, 570, "Number of Color=%d", Color::getColorNum());
xyprintf(880, 510, "按“C”键结束动画");
if (keystate('C') != 0) {
exit(board);
delete aline;
delete anotherline;
break;
}
}
}
void mainloop2() {
DrawControl *board = reinterpret_cast<DrawControl*>(new DrawControl(500, 600));
int x = 0, now = 25;
memset(circles, 0, sizeof(circles));
memset(rectangulars, 0, sizeof(rectangulars));
for (int i = 1; i <= 24; i++) {
rectangulars[i] = reinterpret_cast<Rectangular*>(new Rectangular(Point(250 - i * 10, 250 - i * 10), Point(250 + i * 10, 250 + i * 10), Color(random(16777216), false)));
circles[i] = reinterpret_cast<Circle*>(new Circle(Point(770250, 250), i * 10, Color(random(16777216), false)));
}
Rectangular *aline = reinterpret_cast<Rectangular*>(new Rectangular(Point(0, 500), Point(500, 505), Color(16777215, true)));
for (int i = 0; is_run(); delay_fps(60), (i = (i + 1) % 16777216)) {
for (int j = 0; j < 30; j++) {
if (circles[j] != 0) {
circles[j]->setRadius(circles[j]->getRadius() - 1);
rectangulars[j]->setPoint1(Point(rectangulars[j]->getPoint1().getX() + 1, rectangulars[j]->getPoint1().getY() + 1));
rectangulars[j]->setPoint2(Point(rectangulars[j]->getPoint2().getX() - 1, rectangulars[j]->getPoint2().getY() - 1));
}
}
for (int j = 0; j < 30; j++) {
if (circles[j] != 0) {
if (circles[j]->getRadius() == 0) {
delete circles[j];
circles[j] = 0;
delete rectangulars[j];
rectangulars[j] = 0;
rectangulars[now] = reinterpret_cast<Rectangular*>(new Rectangular(Point(10, 10), Point(490, 490), Color(random(16777216), false)));
circles[now] = reinterpret_cast<Circle*>(new Circle(Point(250, 250), 240, Color(random(16777216), false)));
now = (now + 1) % 30;
}
}
}
aline->draw();
for (int j = 0; j < 30; j++) {
if (circles[j] != 0) {
if ((j + i * 1000) % 100000 > 50000) {
rectangulars[j]->draw();
circles[j]->draw();
}
else {
circles[j]->draw();
rectangulars[j]->draw();
}
}
}
xyprintf(10, 510, "Number of Circle = %d", Circle::getCircleNum());
xyprintf(10, 530, "Number of Rectangular = %d", Rectangular::getRectangularNum());
xyprintf(10, 550, "Number of Point = %d", Point::getPointNum());
xyprintf(10, 570, "Number of Color = %d", Color::getColorNum());
xyprintf(330, 510, "按“C”键结束精神污染");
if (keystate('C') != 0) {
exit(board);
delete aline;
break;
}
}
}
int main() {
DrawControl *board=reinterpret_cast<DrawControl*>(new DrawControl(400, 200));
for(;; delay_fps(60)) {
xyprintf(10, 10, "说明");
xyprintf(10, 40, "按“A”键观看普通版动画");
xyprintf(10, 70, "按“B”键观看加强版魔性动画");
xyprintf(10, 100, "按“C”键退出程序");
if (keystate('A') != 0) {
exit(board);
mainloop1();
break;
}
if (keystate('B') != 0) {
exit(board);
mainloop2();
break;
}
if (keystate('C') != 0) {
exit(board);
break;
}
}
}
包含所有头文件及所需要的库
classes.h
#pragma once
#include "graphics.h"
#include "DrawGraphics.h"
#include <string.h>
#include <string>
#include "DrawControl.h"
#include "Color.h"
#include "Point.h"
#include "Circle.h"
#include "Rectangular.h"
using namespace std;
绘图控制类
DrawControl.h
#pragma once
class DrawControl
{
public:
DrawControl();
DrawControl(int x, int y);
void closeBoard();
~DrawControl();
};
DrawControl.cpp
#include "classes.h"
DrawControl::DrawControl()
{
initgraph(640, 480);
randomize();
}
DrawControl::DrawControl(int x = 640, int y = 480)
{
initgraph(x, y);
randomize();
}
void DrawControl::closeBoard()
{
ege::getch();
closegraph();
}
DrawControl::~DrawControl()
{
ege::getch();
closegraph();
}
图形基类
DrawGraphics.h
#pragma once
class DrawGraphics
{
private:
static int graphicsNum_;
public:
DrawGraphics();
virtual void draw()=0;
static int getGraphicsNum();
~DrawGraphics();
};
DrawGraphics.cpp
#include "classes.h"
int DrawGraphics::graphicsNum_ = 0;
DrawGraphics::DrawGraphics()
{
graphicsNum_++;
}
int DrawGraphics::getGraphicsNum()
{
return graphicsNum_;
}
DrawGraphics::~DrawGraphics()
{
graphicsNum_--;
}
点类
Point.h
#pragma once
class Point
{
private:
int x_, y_;
static int pointNum_;
public:
Point();
Point(int x, int y);
int getX();
int getY();
void setX(int x);
void setY(int y);
static int getPointNum();
Point(const Point &);
~Point();
};
Point.cpp
#include "classes.h"
int Point::pointNum_ = 0;
Point::Point()
{
x_ = 0;
y_ = 0;
pointNum_++;
}
Point::Point(int x, int y)
{
x_ = x;
y_ = y;
pointNum_++;
}
int Point::getX()
{
return x_;
}
int Point::getY()
{
return y_;
}
void Point::setX(int x)
{
x_ = x;
}
void Point::setY(int y)
{
y_ = y;
}
int Point::getPointNum()
{
return pointNum_;
}
Point::Point(const Point & _Point)
{
this->x_ = _Point.x_;
this->y_ = _Point.y_;
pointNum_++;
}
Point::~Point()
{
pointNum_--;
}
颜色类
Color.h
#pragma once
class Color
{
private:
color_t color_;
bool isFilledColor_;
static int colorNum_;
public:
Color();
Color(color_t,bool);
void setColor(color_t color);
void setIsFilledColor(bool isFilledColor);
color_t getColor();
bool getIsFilledColor();
static int getColorNum();
Color(const Color&);
~Color();
};
Color.cpp
#include "classes.h"
int Color::colorNum_ = 0;
Color::Color()
{
this->colorNum_++;
}
Color::Color(color_t color, bool isFilledColor)
{
this->colorNum_++;
this->color_ = color;
this->isFilledColor_ = isFilledColor;
}
Color::~Color()
{
this->colorNum_--;
}
void Color::setColor(color_t color)
{
color_ = color;
}
void Color::setIsFilledColor(bool isFilledColor)
{
isFilledColor_ = isFilledColor;
}
color_t Color::getColor()
{
return color_;
}
bool Color::getIsFilledColor()
{
return isFilledColor_;
}
int Color::getColorNum()
{
return colorNum_;
}
Color::Color(const Color & _color)
{
this->colorNum_++;
this->color_ = _color.color_;
this->isFilledColor_ = _color.isFilledColor_;
}
圆类
Circle.h
#pragma once
class Circle:public DrawGraphics
{
private:
int radius_;
Color color_;
Point center_;
static int circleNum_;
public:
Circle();
Circle(Point center,int radius,Color color);
Point getCenter();
int getRadius();
Color getColor();
static int getCircleNum();
void setCenter(Point center);
void setRadius(int radius);
void setColor(Color color);
void draw();
Circle(const Circle&);
~Circle();
};
Circle.cpp
#include "classes.h"
int Circle::circleNum_ = 0;
Circle::Circle()
{
this->circleNum_++;
this->center_ = Point();
this->radius_ = 0;
this->color_ = Color();
}
Circle::Circle(Point center, int radius, Color color)
{
this->circleNum_++;
this->center_ = center;
this->radius_ = radius;
this->color_ = color;
}
Point Circle::getCenter()
{
return this->center_;
}
int Circle::getRadius()
{
return this->radius_;
}
Color Circle::getColor()
{
return this->color_;
}
int Circle::getCircleNum()
{
return circleNum_;
}
void Circle::setCenter(Point center)
{
this->center_ = center;
}
void Circle::setRadius(int radius)
{
this->radius_ = radius;
}
void Circle::setColor(Color color)
{
this->color_ = color;
}
void Circle::draw()
{
if (this->color_.getIsFilledColor() == false) {
setcolor(this->color_.getColor());
circle(this->center_.getX(), this->center_.getY(), this->radius_);
}
else {
setcolor(this->color_.getColor());
setfillcolor(this->color_.getColor());
fillellipse(this->center_.getX(), this->center_.getY(), this->radius_, this->radius_);
}
}
Circle::Circle(const Circle & circle)
{
this->circleNum_++;
this->radius_ = circle.radius_;
this->color_ = circle.color_;
this->center_ = circle.center_;
}
Circle::~Circle()
{
this->circleNum_--;
}
矩形类
Rectangular.h
#pragma once
class Rectangular:public DrawGraphics
{
private:
Point point1_;
Point point2_;
Color color_;
static int rectangularNum_;
public:
Rectangular();
Rectangular(Point point1,Point point2,Color color);
void setPoint1(Point);
void setPoint2(Point);
void setColor(Color);
Point getPoint1();
Point getPoint2();
Color getColor();
static int getRectangularNum();
void draw();
Rectangular(const Rectangular &);
~Rectangular();
};
Rectangular.cpp
#include "classes.h"
int Rectangular::rectangularNum_ = 0;
Rectangular::Rectangular()
{
rectangularNum_++;
this->point1_ = Point();
this->point2_ = Point();
this->color_ = Color();
}
Rectangular::Rectangular(Point point1, Point point2, Color color)
{
rectangularNum_++;
this->point1_ = point1;
this->point2_ = point2;
this->color_ = color;
}
void Rectangular::setPoint1(Point _point)
{
this->point1_ = _point;
}
void Rectangular::setPoint2(Point _point)
{
this->point2_ = _point;
}
void Rectangular::setColor(Color _color)
{
this->color_ = _color;
}
Point Rectangular::getPoint1()
{
return this->point1_;
}
Point Rectangular::getPoint2()
{
return this->point2_;
}
Color Rectangular::getColor()
{
return this->color_;
}
int Rectangular::getRectangularNum()
{
return rectangularNum_;
}
void Rectangular::draw()
{
if (this->color_.getIsFilledColor() == false) {
setcolor(this->color_.getColor());
rectangle(this->point1_.getX(), this->point1_.getY(), this->point2_.getX(), this->point2_.getY());
}
else {
setcolor(this->color_.getColor());
setfillcolor(this->color_.getColor());
bar(this->point1_.getX(), this->point1_.getY(), this->point2_.getX(), this->point2_.getY());
}
}
Rectangular::Rectangular(const Rectangular & rectangular)
{
rectangularNum_++;
this->point1_ = rectangular.point1_;
this->point2_ = rectangular.point2_;
this->color_ = rectangular.color_;
}
Rectangular::~Rectangular()
{
rectangularNum_--;
}
Lab03
主程序
project.cpp
#include "classes.h"
#include <iostream>
#include <fstream>
using namespace std;
void draw(const int type, const int* info, const int*color, const bool isfilled) {
if (1 == type) {
Circle(Point(info[0], info[1]), info[2], Color(color[0] * 256 * 256 + color[1] * 256 + color[2], isfilled)).draw();
}
else if (2 == type) {
Triangle(Point(info[0], info[1]), Point(info[2], info[3]), Point(info[4], info[5]), Color(color[0] * 256 * 256 + color[1] * 256 + color[2], isfilled)).draw();
}
else if (3 == type) {
Rectangular(Point(info[0], info[1]), Point(info[2], info[3]), Color(color[0] * 256 * 256 + color[1] * 256 + color[2], isfilled)).draw();
}
}
void exit(DrawControl *board) {
delete board;
}
bool getInfo(string s, int info[], int size) {
int num = 0;
s = s + ' ';
int a = 0;
for (int i = 0; i < s.length() && num != size; i++) {
if (s[i] >= '0' && s[i] <= '9') {
a = a * 10 + s[i] - '0';
if (a > 1000000000)return false;
if (s[i + 1] > '9' || s[i + 1] < '0') {
info[num] = a;
num++;
}
}
else {
a = 0;
}
}
return (num == size);
}
void mainloop1() {
int type;
int info[8];
int color[4];
char s[100];
for (;; delay_fps(60)) {
setcolor(12632256);
xyprintf(10, 10, "按“A”键绘制一个图形,按“C”键退出");
type = 0;
memset(info, 0, sizeof(info));
memset(color, 0, sizeof(color));
if (keystate('A') != 0) {
for (inputbox_getline("请输一个数字表示绘制图形的类型", "1表示圆形\n2表示三角形\n3表示矩形\n4表示返回", s, 2); s[0] > '4' || s[0] < '0'; delay_fps(60)) {
inputbox_getline("无效输入,请重新输一个数字表示绘制图形的类型", "1表示圆形\n2表示三角形\n3表示矩形\n4表示返回", s, 2);
}
type = s[0] - '0';
}
if (4 == type)continue;
else if (1 == type) {
for (inputbox_getline("请输入三个数字描述你要画的圆形", "前两个表示圆心坐标x,y\n第三个数表示半径\n以任意字符隔开\n输入多于三个数字只取前三个\n大于要求的数将被取余", s, 100); !getInfo(s, info, 3); delay_fps(60)) {
inputbox_getline("无效输入,请重新输入三个数字描述你要画的圆形", "前两个表示圆心坐标x,y\n第三个数表示半径\n以任意字符隔开\n输入多于三个数字只取前三个\n大于要求的数将被取余", s, 100);
}
}
else if (2 == type) {
for (inputbox_getline("请输入六个数字描述你要画的三角形", "分别表示三个顶点的坐标x,y\n以任意字符隔开\n输入多于六个数字只取前六个\n大于要求的数将被取余", s, 100); !getInfo(s, info, 6); delay_fps(60)) {
inputbox_getline("无效输入,请重新输入六个数字描述你要画的三角形", "分别表示三个顶点的坐标x,y\n以任意字符隔开\n输入多于六个数字只取前六个\n大于要求的数将被取余", s, 100);
}
}
else if (3 == type) {
for (inputbox_getline("请输入四个数字描述你要画的矩形", "分别表示两个对角顶点的坐标x,y\n以任意字符隔开\n输入多于四个数字只取前四个\n大于要求的数将被取余", s, 100); !getInfo(s, info, 4); delay_fps(60)) {
inputbox_getline("无效输入,请输入四个数字描述你要画的矩形", "分别表示两个对角顶点的坐标x,y\n以任意字符隔开\n输入多于四个数字只取前四个\n大于要求的数将被取余", s, 100);
}
}
if (4 == type)continue;
else if (type >= 1 && type <= 3) {
for (inputbox_getline("请输入四个数字描述你要画的图形的颜色", "前三个表示图形颜色的RGB值\n第四个数用0和1表示是否填充\n数字间以任意字符隔开\n输入多于四个数字只取前四个\n大于要求的数将被取余", s, 100); !getInfo(s, color, 4); delay_fps(60)) {
inputbox_getline("无效输入,请重新输入四个数字描述你要画的图形的颜色", "前三个表示图形颜色的RGB值\n第四个数用0和1表示是否填充\n数字间以任意字符隔开\n输入多于四个数字只取前四个\n大于要求的数将被取余", s, 100);
}
}
info[0] %= 600;
info[1] %= 600;
info[2] %= 600;
info[3] %= 600;
info[4] %= 600;
info[5] %= 600;
color[0] %= 256;
color[1] %= 256;
color[2] %= 256;
color[3] %= 2;
if (1 == type) {
Circle(Point(info[0], info[1]), info[2], Color(color[0] * 256 * 256 + color[1] * 256 + color[2], color[3])).draw();
ofstream output("out.txt", ios::app | ios::out);
output << 1 << endl;
for (int i = 0; i <= 2; i++) {
output << info[i] << ' ';
}
output << endl;
for (int i = 0; i <= 3; i++) {
output << color[i] << ' ';
}
output << endl;
output.close();
}
else if (2 == type) {
Triangle(Point(info[0], info[1]), Point(info[2], info[3]), Point(info[4], info[5]), Color(color[0] * 256 * 256 + color[1] * 256 + color[2], color[3])).draw();
ofstream output("out.txt", ios::app | ios::out);
output << 2 << endl;
for (int i = 0; i <= 5; i++) {
output << info[i] << ' ';
}
output << endl;
for (int i = 0; i <= 3; i++) {
output << color[i] << ' ';
}
output << endl;
output.close();
}
else if (3 == type) {
Rectangular(Point(info[0], info[1]), Point(info[2], info[3]), Color(color[0] * 256 * 256 + color[1] * 256 + color[2], color[3])).draw();
ofstream output("out.txt", ios::app | ios::out);
output << 3 << endl;
for (int i = 0; i <= 3; i++) {
output << info[i] << ' ';
}
output << endl;
for (int i = 0; i <= 3; i++) {
output << color[i] << ' ';
}
output << endl;
output.close();
}
if (keystate('C') != 0) {
break;
}
}
}
void mainloop2() {
ifstream input("in.txt", ios::in);
if (!input.fail()) {
int type;
int info[8];
int color[4];
string s;
for (; !input.eof(); delay_fps(60)) {
type = 0;
memset(info, 0, sizeof(info));
memset(color, 0, sizeof(color));
for (getline(input, s); !input.eof() && (s[0] > '4' || s[0] < '0'); delay_fps(60)) {
getline(input, s);
}
type = s[0] - '0';
if (4 == type)continue;
else if (1 == type) {
for (getline(input, s); !input.eof() && (!getInfo(s, info, 3)); delay_fps(60)) {
getline(input, s);
}
}
else if (2 == type) {
for (getline(input, s); !input.eof() && (!getInfo(s, info, 6)); delay_fps(60)) {
getline(input, s);
}
}
else if (3 == type) {
for (getline(input, s); !input.eof() && (!getInfo(s, info, 4)); delay_fps(60)) {
getline(input, s);
}
}
if (4 == type)continue;
else if (type >= 1 && type <= 3) {
for (getline(input, s); !input.eof() && (!getInfo(s, color, 4)); delay_fps(60)) {
getline(input, s);
}
}
info[0] %= 600;
info[1] %= 600;
info[2] %= 600;
info[3] %= 600;
info[4] %= 600;
info[5] %= 600;
color[0] = color[0] % 256;
color[1] %= 256;
color[2] %= 256;
color[3] %= 2;
if (1 == type) {
Circle(Point(info[0], info[1]), info[2], Color(color[0] * 256 * 256 + color[1] * 256 + color[2], color[3])).draw();
}
else if (2 == type) {
Triangle(Point(info[0], info[1]), Point(info[2], info[3]), Point(info[4], info[5]), Color(color[0] * 256 * 256 + color[1] * 256 + color[2], color[3])).draw();
}
else if (3 == type) {
Rectangular(Point(info[0], info[1]), Point(info[2], info[3]), Color(color[0] * 256 * 256 + color[1] * 256 + color[2], color[3])).draw();
}
}
input.close();
for (;; delay_fps(60)) {
setcolor(12632256);
xyprintf(10, 10, "按“C”键退出");
if (keystate('C') != 0) {
break;
}
}
}
else {
setcolor(12632256);
xyprintf(10, 10, "打开文件失败");
for (;; delay_fps(60)) {
xyprintf(10, 40, "按“C”键退出");
if (keystate('C') != 0) {
break;
}
}
}
}
int main() {
DrawControl *board = reinterpret_cast<DrawControl*>(new DrawControl(600, 600));
for (;; delay_fps(60)) {
setcolor(12632256);
xyprintf(30, 60, "按“1”键从屏幕读入数据并写入到out.txt文件中");
xyprintf(30, 120, "按“2”键从文件in.txt中读入数据");
xyprintf(30, 180, "按“C”键退出程序");
xyprintf(30, 240, "如果按键无效请尝试切换英文输入法");
if (keystate('1') != 0) {
cleardevice();
mainloop1();
exit(board);
break;
}
if (keystate('2') != 0) {
cleardevice();
mainloop2();
exit(board);
break;
}
if (keystate('C') != 0) {
exit(board);
break;
}
}
}
包含所有头文件及所需要的库
classes.h
#pragma once
#include "graphics.h"
#include "DrawGraphics.h"
#include <string.h>
#include <string>
#include "DrawControl.h"
#include "Color.h"
#include "Point.h"
#include "Circle.h"
#include "Rectangular.h"
#include "Triangle.h"
using namespace std;
绘图控制类
DrawControl.h
#pragma once
class DrawControl
{
public:
DrawControl();
DrawControl(int x, int y);
void closeBoard();
~DrawControl();
};
DrawControl.cpp
#include "classes.h"
DrawControl::DrawControl()
{
setinitmode(INIT_ANIMATION);
initgraph(640, 480);
randomize();
}
DrawControl::DrawControl(int x = 640, int y = 480)
{
setinitmode(INIT_ANIMATION);
initgraph(x, y);
randomize();
}
void DrawControl::closeBoard()
{
ege::getch();
closegraph();
}
DrawControl::~DrawControl()
{
ege::getch();
closegraph();
}
点类
Point.h
#pragma once
class Point
{
private:
int x_, y_;
static int pointNum_;
public:
Point();
Point(int x, int y);
int getX();
int getY();
void setX(int x);
void setY(int y);
static int getPointNum();
Point(const Point &);
~Point();
};
Point.cpp
#include "classes.h"
int Point::pointNum_ = 0;
Point::Point()
{
x_ = 0;
y_ = 0;
pointNum_++;
}
Point::Point(int x, int y)
{
x_ = x;
y_ = y;
pointNum_++;
}
int Point::getX()
{
return x_;
}
int Point::getY()
{
return y_;
}
void Point::setX(int x)
{
x_ = x;
}
void Point::setY(int y)
{
y_ = y;
}
int Point::getPointNum()
{
return pointNum_;
}
Point::Point(const Point & _Point)
{
this->x_ = _Point.x_;
this->y_ = _Point.y_;
pointNum_++;
}
Point::~Point()
{
pointNum_--;
}
颜色类
Color.h
#pragma once
class Color
{
private:
color_t color_;
bool isFilledColor_;
static int colorNum_;
public:
Color();
Color(color_t,bool);
void setColor(color_t color);
void setIsFilledColor(bool isFilledColor);
color_t getColor();
bool getIsFilledColor();
static int getColorNum();
Color(const Color&);
~Color();
};
Color.cpp
#include "classes.h"
int Color::colorNum_ = 0;
Color::Color()
{
this->colorNum_++;
}
Color::Color(color_t color, bool isFilledColor)
{
this->colorNum_++;
this->color_ = color;
this->isFilledColor_ = isFilledColor;
}
Color::~Color()
{
this->colorNum_--;
}
void Color::setColor(color_t color)
{
color_ = color;
}
void Color::setIsFilledColor(bool isFilledColor)
{
isFilledColor_ = isFilledColor;
}
color_t Color::getColor()
{
return color_;
}
bool Color::getIsFilledColor()
{
return isFilledColor_;
}
int Color::getColorNum()
{
return colorNum_;
}
Color::Color(const Color & _color)
{
this->colorNum_++;
this->color_ = _color.color_;
this->isFilledColor_ = _color.isFilledColor_;
}
图形基类
DrawGraphics.h
#pragma once
class DrawGraphics
{
private:
static int graphicsNum_;
public:
DrawGraphics();
virtual void draw()=0;
static int getGraphicsNum();
~DrawGraphics();
};
DrawGraphics.cpp
#include "classes.h"
int DrawGraphics::graphicsNum_ = 0;
DrawGraphics::DrawGraphics()
{
graphicsNum_++;
}
int DrawGraphics::getGraphicsNum()
{
return graphicsNum_;
}
DrawGraphics::~DrawGraphics()
{
graphicsNum_--;
}
圆类
Circle.h
#pragma once
class Circle:public DrawGraphics
{
private:
int radius_;
Color color_;
Point center_;
static int circleNum_;
public:
Circle();
Circle(Point center,int radius,Color color);
Point getCenter();
int getRadius();
Color getColor();
static int getCircleNum();
void setCenter(Point center);
void setRadius(int radius);
void setColor(Color color);
void draw();
Circle(const Circle&);
~Circle();
};
Circle.cpp
#include "classes.h"
int Circle::circleNum_ = 0;
Circle::Circle()
{
this->circleNum_++;
this->center_ = Point();
this->radius_ = 0;
this->color_ = Color();
}
Circle::Circle(Point center, int radius, Color color)
{
this->circleNum_++;
this->center_ = center;
this->radius_ = radius;
this->color_ = color;
}
Point Circle::getCenter()
{
return this->center_;
}
int Circle::getRadius()
{
return this->radius_;
}
Color Circle::getColor()
{
return this->color_;
}
int Circle::getCircleNum()
{
return circleNum_;
}
void Circle::setCenter(Point center)
{
this->center_ = center;
}
void Circle::setRadius(int radius)
{
this->radius_ = radius;
}
void Circle::setColor(Color color)
{
this->color_ = color;
}
void Circle::draw()
{
if (this->color_.getIsFilledColor() == false) {
setcolor(this->color_.getColor());
circle(this->center_.getX(), this->center_.getY(), this->radius_);
}
else {
setcolor(this->color_.getColor());
setfillcolor(this->color_.getColor());
fillellipse(this->center_.getX(), this->center_.getY(), this->radius_, this->radius_);
}
}
Circle::Circle(const Circle & circle)
{
this->circleNum_++;
this->radius_ = circle.radius_;
this->color_ = circle.color_;
this->center_ = circle.center_;
}
Circle::~Circle()
{
this->circleNum_--;
}
矩形类
Rectangular.h
#pragma once
class Rectangular:public DrawGraphics
{
private:
Point point1_;
Point point2_;
Color color_;
static int rectangularNum_;
public:
Rectangular();
Rectangular(Point point1,Point point2,Color color);
void setPoint1(Point);
void setPoint2(Point);
void setColor(Color);
Point getPoint1();
Point getPoint2();
Color getColor();
static int getRectangularNum();
void draw();
Rectangular(const Rectangular &);
~Rectangular();
};
Rectangular.cpp
#include "classes.h"
int Rectangular::rectangularNum_ = 0;
Rectangular::Rectangular()
{
rectangularNum_++;
this->point1_ = Point();
this->point2_ = Point();
this->color_ = Color();
}
Rectangular::Rectangular(Point point1, Point point2, Color color)
{
rectangularNum_++;
this->point1_ = point1;
this->point2_ = point2;
this->color_ = color;
}
void Rectangular::setPoint1(Point _point)
{
this->point1_ = _point;
}
void Rectangular::setPoint2(Point _point)
{
this->point2_ = _point;
}
void Rectangular::setColor(Color _color)
{
this->color_ = _color;
}
Point Rectangular::getPoint1()
{
return this->point1_;
}
Point Rectangular::getPoint2()
{
return this->point2_;
}
Color Rectangular::getColor()
{
return this->color_;
}
int Rectangular::getRectangularNum()
{
return rectangularNum_;
}
void Rectangular::draw()
{
if (this->color_.getIsFilledColor() == false) {
setcolor(this->color_.getColor());
rectangle(this->point1_.getX(), this->point1_.getY(), this->point2_.getX(), this->point2_.getY());
}
else {
setcolor(this->color_.getColor());
setfillcolor(this->color_.getColor());
bar(this->point1_.getX(), this->point1_.getY(), this->point2_.getX(), this->point2_.getY());
}
}
Rectangular::Rectangular(const Rectangular & rectangular)
{
rectangularNum_++;
this->point1_ = rectangular.point1_;
this->point2_ = rectangular.point2_;
this->color_ = rectangular.color_;
}
Rectangular::~Rectangular()
{
rectangularNum_--;
}
三角形类
Triangle.h
#pragma once
class Triangle :public DrawGraphics
{
private:
Point point1_;
Point point2_;
Point point3_;
Color color_;
static int triangleNum_;
public:
Triangle();
Triangle(Point ,Point ,Point ,Color );
void setPoint1(Point);
void setPoint2(Point);
void setPoint3(Point);
void setColor(Color);
Point getPoint1();
Point getPoint2();
Point getPoint3();
Color getColor();
static int getTriangleNum();
void draw();
Triangle(const Triangle &);
~Triangle();
};
Triangle.cpp
#include "classes.h"
int Triangle::triangleNum_ = 0;
Triangle::Triangle()
{
triangleNum_++;
this->point1_ = Point();
this->point2_ = Point();
this->point3_ = Point();
this->color_ = Color();
}
Triangle::Triangle(Point point1, Point point2, Point point3, Color color)
{
triangleNum_++;
this->point1_ = point1;
this->point2_ = point2;
this->point3_ = point3;
this->color_ = color;
}
void Triangle::setPoint1(Point _point)
{
this->point1_ = _point;
}
void Triangle::setPoint2(Point _point)
{
this->point2_ = _point;
}
void Triangle::setPoint3(Point _point)
{
this->point3_ = _point;
}
void Triangle::setColor(Color _color)
{
this->color_ = _color;
}
Point Triangle::getPoint1()
{
return this->point1_;
}
Point Triangle::getPoint2()
{
return this->point2_;
}
Point Triangle::getPoint3()
{
return this->point3_;
}
Color Triangle::getColor()
{
return this->color_;
}
int Triangle::getTriangleNum()
{
return triangleNum_;
}
void Triangle::draw()
{
int points[8];
points[0]=this->point1_.getX();
points[1]=this->point1_.getY();
points[2]=this->point2_.getX();
points[3]=this->point2_.getY();
points[4]=this->point3_.getX();
points[5]=this->point3_.getY();
points[6]=this->point1_.getX();
points[7]=this->point1_.getY();
if (this->color_.getIsFilledColor() == false) {
setcolor(this->color_.getColor());
drawpoly(4,points);
}
else {
setcolor(this->color_.getColor());
setfillcolor(this->color_.getColor());
fillpoly(4,points);
}
}
Triangle::Triangle(const Triangle & triangle)
{
triangleNum_++;
this->point1_ = triangle.point1_;
this->point2_ = triangle.point2_;
this->point3_ = triangle.point3_;
this->color_ = triangle.color_;
}
Triangle::~Triangle()
{
triangleNum_--;
}
Lab04
主程序
project.cpp
#include "classes.h"
#include <iostream>
#include <fstream>
using namespace std;
void draw(const int type, const int* info, const int*color, const bool isfilled) {
if (1 == type) {
Circle(Point(info[0], info[1]), info[2], Color(color[0] * 256 * 256 + color[1] * 256 + color[2], isfilled)).draw();
}
else if (2 == type) {
Triangle(Point(info[0], info[1]), Point(info[2], info[3]), Point(info[4], info[5]), Color(color[0] * 256 * 256 + color[1] * 256 + color[2], isfilled)).draw();
}
else if (3 == type) {
Rectangular(Point(info[0], info[1]), Point(info[2], info[3]), Color(color[0] * 256 * 256 + color[1] * 256 + color[2], isfilled)).draw();
}
}
void exit(DrawControl *board) {
delete board;
}
bool getInfo(string s, int info[], int size) {
int num = 0;
s = s + ' ';
int a = 0;
for (int i = 0; i < s.length() && num != size; i++) {
if (s[i] >= '0' && s[i] <= '9') {
a = a * 10 + s[i] - '0';
if (a > 1000000000)return false;
if (s[i + 1] > '9' || s[i + 1] < '0') {
info[num] = a;
num++;
}
}
else {
a = 0;
}
}
return (num == size);
}
void mainloop1() {
int type;
int info[8];
int color[4];
int point[2];
Point centerPoint;
char s[100];
for (;; delay_fps(60)) {
setcolor(12632256);
xyprintf(10, 10, "按“A”键绘制一个图形,按“C”键退出");
type = 0;
memset(info, 0, sizeof(info));
memset(color, 0, sizeof(color));
if (keystate('A') != 0) {
for (inputbox_getline("请输一个数字表示绘制图形的类型", "1表示圆形\n2表示三角形\n3表示矩形\n4表示返回", s, 2); s[0] > '4' || s[0] < '0'; delay_fps(60)) {
inputbox_getline("无效输入,请重新输一个数字表示绘制图形的类型", "1表示圆形\n2表示三角形\n3表示矩形\n4表示返回", s, 2);
}
type = s[0] - '0';
}
if (4 == type)continue;
else if (1 == type) {
for (inputbox_getline("请输入三个数字描述你要画的圆形", "前两个表示圆心坐标x,y\n第三个数表示半径\n以任意字符隔开\n输入多于三个数字只取前三个\n大于要求的数将被取余", s, 100); !getInfo(s, info, 3); delay_fps(60)) {
inputbox_getline("无效输入,请重新输入三个数字描述你要画的圆形", "前两个表示圆心坐标x,y\n第三个数表示半径\n以任意字符隔开\n输入多于三个数字只取前三个\n大于要求的数将被取余", s, 100);
}
}
else if (2 == type) {
for (inputbox_getline("请输入六个数字描述你要画的三角形", "分别表示三个顶点的坐标x,y\n以任意字符隔开\n输入多于六个数字只取前六个\n大于要求的数将被取余", s, 100); !getInfo(s, info, 6); delay_fps(60)) {
inputbox_getline("无效输入,请重新输入六个数字描述你要画的三角形", "分别表示三个顶点的坐标x,y\n以任意字符隔开\n输入多于六个数字只取前六个\n大于要求的数将被取余", s, 100);
}
}
else if (3 == type) {
for (inputbox_getline("请输入四个数字描述你要画的矩形", "分别表示两个对角顶点的坐标x,y\n以任意字符隔开\n输入多于四个数字只取前四个\n大于要求的数将被取余", s, 100); !getInfo(s, info, 4); delay_fps(60)) {
inputbox_getline("无效输入,请输入四个数字描述你要画的矩形", "分别表示两个对角顶点的坐标x,y\n以任意字符隔开\n输入多于四个数字只取前四个\n大于要求的数将被取余", s, 100);
}
}
if (4 == type)continue;
else if (type >= 1 && type <= 3) {
for (inputbox_getline("请输入四个数字描述你要画的图形的颜色", "前三个表示图形颜色的RGB值\n第四个数用0和1表示是否填充\n数字间以任意字符隔开\n输入多于四个数字只取前四个\n大于要求的数将被取余", s, 100); !getInfo(s, color, 4); delay_fps(60)) {
inputbox_getline("无效输入,请重新输入四个数字描述你要画的图形的颜色", "前三个表示图形颜色的RGB值\n第四个数用0和1表示是否填充\n数字间以任意字符隔开\n输入多于四个数字只取前四个\n大于要求的数将被取余", s, 100);
}
for (inputbox_getline("请输入两个数字描述你要变化的图形的坐标", "第一个值为X坐标\n第二个值为Y坐标", s, 100); !getInfo(s, point, 2); delay_fps(60)) {
inputbox_getline("无效输入,请输入两个数字描述你要变化的图形的坐标", "第一个值为X坐标\n第二个值为Y坐标", s, 100);
}
point[0] %= 600;
point[1] %= 600;
centerPoint = Point(point[0], point[1]);
}
info[0] %= 600;
info[1] %= 600;
info[2] %= 600;
info[3] %= 600;
info[4] %= 600;
info[5] %= 600;
color[0] %= 256;
color[1] %= 256;
color[2] %= 256;
color[3] %= 2;
if (1 == type) {
ofstream output("out.txt", ios::app | ios::out);
output << 1 << endl;
for (int i = 0; i <= 2; i++) {
output << info[i] << ' ';
}
output << endl;
for (int i = 0; i <= 3; i++) {
output << color[i] << ' ';
}
output << endl;
for (int i = 0; i <= 1; i++) {
output << point[i] << ' ';
}
output << endl;
output.close();
Circle draw = Circle(Point(info[0], info[1]), info[2], Color(color[0] * 256 * 256 + color[1] * 256 + color[2], color[3]));
for (int i = 1; i <= 5; i++, delay_fps(1), cleardevice()) {
draw.draw();
draw.setCenter(draw.getCenter()+centerPoint);
draw.setRadius(draw.getRadius() / 2);
}
}
else if (2 == type) {
ofstream output("out.txt", ios::app | ios::out);
output << 2 << endl;
for (int i = 0; i <= 5; i++) {
output << info[i] << ' ';
}
output << endl;
for (int i = 0; i <= 3; i++) {
output << color[i] << ' ';
}
output << endl;
for (int i = 0; i <= 1; i++) {
output << point[i] << ' ';
}
output << endl;
output.close();
Triangle draw = Triangle(Point(info[0], info[1]), Point(info[2], info[3]), Point(info[4], info[5]), Color(color[0] * 256 * 256 + color[1] * 256 + color[2], color[3]));
for (int i = 1; i <= 5; i++, delay_fps(1), cleardevice()) {
draw.draw();
draw.setPoint1(draw.getPoint1() + centerPoint);
draw.setPoint2(draw.getPoint2() + centerPoint);
draw.setPoint3(draw.getPoint3() + centerPoint);
}
}
else if (3 == type) {
ofstream output("out.txt", ios::app | ios::out);
output << 3 << endl;
for (int i = 0; i <= 3; i++) {
output << info[i] << ' ';
}
output << endl;
for (int i = 0; i <= 3; i++) {
output << color[i] << ' ';
}
output << endl;
for (int i = 0; i <= 1; i++) {
output << point[i] << ' ';
}
output << endl;
output.close();
Rectangular draw=Rectangular(Point(info[0], info[1]), Point(info[2], info[3]), Color(color[0] * 256 * 256 + color[1] * 256 + color[2], color[3]));
try
{
for (int i = 1; i <= 5; i++, delay_fps(1), cleardevice()) {
draw.draw();
draw[1] = draw[1] + centerPoint;
draw[2] = draw[1] + centerPoint;
}
}
catch (const bool)
{
xyprintf(30, 60, "地址访问错误!");
}
}
if (keystate('C') != 0) {
break;
}
}
}
void mainloop2() {
ifstream input("in.txt", ios::in);
if (!input.fail()) {
int type;
int info[8];
int color[4];
int point[2];
Point centerPoint;
string s;
for (; !input.eof(); delay_fps(60)) {
type = 0;
memset(info, 0, sizeof(info));
memset(color, 0, sizeof(color));
for (getline(input, s); !input.eof() && (s[0] > '4' || s[0] < '0'); delay_fps(60)) {
getline(input, s);
}
type = s[0] - '0';
if (4 == type)continue;
else if (1 == type) {
for (getline(input, s); !input.eof() && (!getInfo(s, info, 3)); delay_fps(60)) {
getline(input, s);
}
}
else if (2 == type) {
for (getline(input, s); !input.eof() && (!getInfo(s, info, 6)); delay_fps(60)) {
getline(input, s);
}
}
else if (3 == type) {
for (getline(input, s); !input.eof() && (!getInfo(s, info, 4)); delay_fps(60)) {
getline(input, s);
}
}
if (4 == type)continue;
else if (type >= 1 && type <= 3) {
for (getline(input, s); !input.eof() && (!getInfo(s, color, 4)); delay_fps(60)) {
getline(input, s);
}
for (getline(input, s); !getInfo(s, point, 2); delay_fps(60)) {
getline(input, s);
}
point[0] %= 600;
point[1] %= 600;
centerPoint = Point(point[0], point[1]);
}
info[0] %= 600;
info[1] %= 600;
info[2] %= 600;
info[3] %= 600;
info[4] %= 600;
info[5] %= 600;
color[0] = color[0] % 256;
color[1] %= 256;
color[2] %= 256;
color[3] %= 2;
if (1 == type) {
Circle draw = Circle(Point(info[0], info[1]), info[2], Color(color[0] * 256 * 256 + color[1] * 256 + color[2], color[3]));
for (int i = 1; i <= 5; i++, delay_fps(1), cleardevice()) {
draw.draw();
draw.setCenter(draw.getCenter() + centerPoint);
draw.setRadius(draw.getRadius() / 2);
}
}
else if (2 == type) {
Triangle draw = Triangle(Point(info[0], info[1]), Point(info[2], info[3]), Point(info[4], info[5]), Color(color[0] * 256 * 256 + color[1] * 256 + color[2], color[3]));
for (int i = 1; i <= 5; i++, delay_fps(1), cleardevice()) {
draw.draw();
draw.setPoint1(draw.getPoint1() + centerPoint);
draw.setPoint2(draw.getPoint2() + centerPoint);
draw.setPoint3(draw.getPoint3() + centerPoint);
}
}
else if (3 == type) {
Rectangular draw = Rectangular(Point(info[0], info[1]), Point(info[2], info[3]), Color(color[0] * 256 * 256 + color[1] * 256 + color[2], color[3]));
try
{
for (int i = 1; i <= 5; i++, delay_fps(1), cleardevice()) {
draw.draw();
draw[1] = draw[1] + centerPoint;
draw[2] = draw[1] + centerPoint;
}
}
catch (const bool)
{
xyprintf(30, 60, "地址访问错误!");
}
}
}
input.close();
for (;; delay_fps(60)) {
setcolor(12632256);
xyprintf(10, 10, "按“C”键退出");
if (keystate('C') != 0) {
break;
}
}
}
else {
setcolor(12632256);
xyprintf(10, 10, "打开文件失败");
for (;; delay_fps(60)) {
xyprintf(10, 40, "按“C”键退出");
if (keystate('C') != 0) {
break;
}
}
}
}
int main() {
DrawControl *board = reinterpret_cast<DrawControl*>(new DrawControl(600, 600));
for (;; delay_fps(60)) {
setcolor(12632256);
xyprintf(30, 60, "按“1”键从屏幕读入数据并写入到out.txt文件中");
xyprintf(30, 120, "按“2”键从文件in.txt中读入数据");
xyprintf(30, 180, "按“C”键退出程序");
xyprintf(30, 240, "如果按键无效请尝试切换英文输入法");
if (keystate('1') != 0) {
cleardevice();
mainloop1();
exit(board);
break;
}
if (keystate('2') != 0) {
cleardevice();
mainloop2();
exit(board);
break;
}
if (keystate('C') != 0) {
exit(board);
break;
}
}
}
包含所有头文件及所需要的库
classes.h
#pragma once
#include <algorithm>
#include "graphics.h"
#include "DrawGraphics.h"
#include <string.h>
#include <string>
#include "DrawControl.h"
#include "Color.h"
#include "Point.h"
#include "Circle.h"
#include "Rectangular.h"
#include "Triangle.h"
using namespace std;
绘图控制类
DrawControl.h
#pragma once
class DrawControl
{
public:
DrawControl();
DrawControl(int x, int y);
void closeBoard();
~DrawControl();
};
DrawControl.cpp
#include "classes.h"
DrawControl::DrawControl()
{
initgraph(640, 480);
randomize();
}
DrawControl::DrawControl(int x = 640, int y = 480)
{
initgraph(x, y);
randomize();
}
void DrawControl::closeBoard()
{
ege::getch();
closegraph();
}
DrawControl::~DrawControl()
{
ege::getch();
closegraph();
}
点类
Point.h
#pragma once
class Point
{
private:
int x_, y_;
static int pointNum_;
public:
Point();
Point(int x, int y);
const int getX();
const int getY();
void setX(int x);
void setY(int y);
static int getPointNum();
Point(const Point &);
Point operator +(const Point&);
Point operator =(const Point&);
bool operator ==(const Point&);
bool operator !=(const Point&);
int& operator [](const int);
Point operator +=(const Point&);
~Point();
};
Point.cpp
#include "classes.h"
int Point::pointNum_ = 0;
Point::Point()
{
x_ = 0;
y_ = 0;
pointNum_++;
}
Point::Point(int x, int y)
{
x_ = x;
y_ = y;
pointNum_++;
}
const int Point::getX()
{
return x_;
}
const int Point::getY()
{
return y_;
}
void Point::setX(int x)
{
x_ = x;
}
void Point::setY(int y)
{
y_ = y;
}
int Point::getPointNum()
{
return pointNum_;
}
Point::Point(const Point & _Point)
{
this->x_ = _Point.x_;
this->y_ = _Point.y_;
pointNum_++;
}
Point Point::operator+(const Point & Point_)
{
return Point((this->x_ + Point_.x_) / 2, (this->y_ + Point_.x_) / 2);
}
Point Point::operator=(const Point & Point_)
{
this->x_ = Point_.x_;
this->y_ = Point_.y_;
return (*this);
}
bool Point::operator==(const Point & Point_)
{
return (this->x_==Point_.x_&&this->y_==Point_.y_);
}
bool Point::operator!=(const Point & Point_)
{
return !(this->x_ == Point_.x_&&this->y_ == Point_.y_);
}
int& Point::operator[](const int n)
{
if (n==1) return this->x_;
else if (n==2) return this->y_;
else throw false;
}
Point Point::operator+=(const Point & Point_)
{
*this = *this + Point_;
return *this;
}
Point::~Point()
{
pointNum_--;
}
颜色类
Color.h
#pragma once
class Color
{
private:
color_t color_;
bool isFilledColor_;
static int colorNum_;
public:
Color();
Color(color_t,bool);
void setColor(color_t color);
void setIsFilledColor(bool isFilledColor);
color_t getColor();
bool getIsFilledColor();
static int getColorNum();
Color operator = (const Color&);
bool operator == (const Color&);
bool operator != (const Color&);
color_t &operator [] (const int&);
Color(const Color&);
~Color();
};
Color.cpp
#include "classes.h"
int Color::colorNum_ = 0;
Color::Color()
{
this->colorNum_++;
}
Color::Color(color_t color, bool isFilledColor)
{
this->colorNum_++;
this->color_ = color;
this->isFilledColor_ = isFilledColor;
}
Color::~Color()
{
this->colorNum_--;
}
void Color::setColor(color_t color)
{
color_ = color;
}
void Color::setIsFilledColor(bool isFilledColor)
{
isFilledColor_ = isFilledColor;
}
color_t Color::getColor()
{
return color_;
}
bool Color::getIsFilledColor()
{
return isFilledColor_;
}
int Color::getColorNum()
{
return colorNum_;
}
Color Color::operator=(const Color & Color_)
{
this->isFilledColor_ = Color_.isFilledColor_;
this->color_ = Color_.color_;
return *this;
}
bool Color::operator==(const Color &Color_)
{
return (this->color_ == Color_.color_&&this->isFilledColor_ == Color_.isFilledColor_);
}
bool Color::operator!=(const Color &Color_)
{
return (*this==Color_);
}
color_t& Color::operator[](const int &n)
{
if (n==1) return this->color_;
else throw false;
}
Color::Color(const Color & _color)
{
this->colorNum_++;
this->color_ = _color.color_;
this->isFilledColor_ = _color.isFilledColor_;
}
图形基类
DrawGraphics.h
#pragma once
class DrawGraphics
{
private:
static int graphicsNum_;
public:
DrawGraphics();
virtual void draw()=0;
static int getGraphicsNum();
~DrawGraphics();
};
DrawGraphics.cpp
#include "classes.h"
int DrawGraphics::graphicsNum_ = 0;
DrawGraphics::DrawGraphics()
{
graphicsNum_++;
}
int DrawGraphics::getGraphicsNum()
{
return graphicsNum_;
}
DrawGraphics::~DrawGraphics()
{
graphicsNum_--;
}
圆类
Circle.h
#pragma once
class Circle:public DrawGraphics
{
private:
int radius_;
Color color_;
Point center_;
static int circleNum_;
public:
Circle();
Circle(Point center,int radius,Color color);
Point getCenter();
int getRadius();
Color getColor();
static int getCircleNum();
void setCenter(Point center);
void setRadius(int radius);
void setColor(Color color);
void draw();
bool operator>(const Circle&);
bool operator<(const Circle&);
bool operator>=(const Circle&);
bool operator<=(const Circle&);
bool operator==(const Circle&);
Circle &operator=(const Circle&);
Point& operator[](const int);
Circle(const Circle&);
~Circle();
};
Circle.cpp
#include "classes.h"
int Circle::circleNum_ = 0;
Circle::Circle()
{
this->circleNum_++;
this->center_ = Point();
this->radius_ = 0;
this->color_ = Color();
}
Circle::Circle(Point center, int radius, Color color)
{
this->circleNum_++;
this->center_ = center;
this->radius_ = radius;
this->color_ = color;
}
Point Circle::getCenter()
{
return this->center_;
}
int Circle::getRadius()
{
return this->radius_;
}
Color Circle::getColor()
{
return this->color_;
}
int Circle::getCircleNum()
{
return circleNum_;
}
void Circle::setCenter(Point center)
{
this->center_ = center;
}
void Circle::setRadius(int radius)
{
this->radius_ = radius;
}
void Circle::setColor(Color color)
{
this->color_ = color;
}
void Circle::draw()
{
if (this->color_.getIsFilledColor() == false) {
setcolor(this->color_.getColor());
circle(this->center_.getX(), this->center_.getY(), this->radius_);
}
else {
setcolor(this->color_.getColor());
setfillcolor(this->color_.getColor());
fillellipse(this->center_.getX(), this->center_.getY(), this->radius_, this->radius_);
}
}
bool Circle::operator>(const Circle &Circle_)
{
return this->radius_ > Circle_.radius_;
}
bool Circle::operator<(const Circle &Circle_)
{
return this->radius_ < Circle_.radius_;
}
bool Circle::operator>=(const Circle &Circle_)
{
return this->radius_ >= Circle_.radius_;
}
bool Circle::operator<=(const Circle &Circle_)
{
return this->radius_ <= Circle_.radius_;
}
bool Circle::operator==(const Circle &Circle_)
{
return this->radius_ == Circle_.radius_;
}
Circle &Circle::operator=(const Circle &Circle_)
{
this->center_ = Circle_.center_;
this->radius_ = Circle_.radius_;
this->color_ = Circle_.color_;
return *this;
}
Point & Circle::operator[](const int n )
{
if (n==1) this->center_;
else throw false;
}
Circle::Circle(const Circle & circle)
{
this->circleNum_++;
this->radius_ = circle.radius_;
this->color_ = circle.color_;
this->center_ = circle.center_;
}
Circle::~Circle()
{
this->circleNum_--;
}
矩形类
Rectangular.h
#pragma once
class Rectangular:public DrawGraphics
{
private:
Point point1_;
Point point2_;
Color color_;
static int rectangularNum_;
public:
Rectangular();
Rectangular(Point point1,Point point2,Color color);
void setPoint1(Point);
void setPoint2(Point);
void setColor(Color);
Point getPoint1();
Point getPoint2();
Color getColor();
static int getRectangularNum();
void draw();
Rectangular(const Rectangular &);
bool operator>(const Rectangular&);
bool operator<(const Rectangular&);
bool operator>=(const Rectangular&);
bool operator<=(const Rectangular&);
bool operator==(const Rectangular&);
Rectangular &operator=(const Rectangular&);
Point& operator[](const int);
~Rectangular();
};
Rectangular.cpp
#include "classes.h"
int Rectangular::rectangularNum_ = 0;
Rectangular::Rectangular()
{
rectangularNum_++;
this->point1_ = Point();
this->point2_ = Point();
this->color_ = Color();
}
Rectangular::Rectangular(Point point1, Point point2, Color color)
{
rectangularNum_++;
this->point1_ = point1;
this->point2_ = point2;
this->color_ = color;
}
void Rectangular::setPoint1(Point _point)
{
this->point1_ = _point;
}
void Rectangular::setPoint2(Point _point)
{
this->point2_ = _point;
}
void Rectangular::setColor(Color _color)
{
this->color_ = _color;
}
Point Rectangular::getPoint1()
{
return this->point1_;
}
Point Rectangular::getPoint2()
{
return this->point2_;
}
Color Rectangular::getColor()
{
return this->color_;
}
int Rectangular::getRectangularNum()
{
return rectangularNum_;
}
void Rectangular::draw()
{
if (this->color_.getIsFilledColor() == false) {
setcolor(this->color_.getColor());
rectangle(this->point1_.getX(), this->point1_.getY(), this->point2_.getX(), this->point2_.getY());
}
else {
setcolor(this->color_.getColor());
setfillcolor(this->color_.getColor());
bar(this->point1_.getX(), this->point1_.getY(), this->point2_.getX(), this->point2_.getY());
}
}
Rectangular::Rectangular(const Rectangular & rectangular)
{
rectangularNum_++;
this->point1_ = rectangular.point1_;
this->point2_ = rectangular.point2_;
this->color_ = rectangular.color_;
}
bool Rectangular::operator>(const Rectangular &Rectangular_)
{
Rectangular temp = Rectangular_;
return abs(this->point1_.getX() - this->point2_.getX()*this->point1_.getY() - this->point2_.getY()) > abs((temp.getPoint1().getX() - temp.getPoint2().getX())*(temp.getPoint1().getY() - temp.getPoint2().getY()));
}
bool Rectangular::operator<(const Rectangular &Rectangular_)
{
Rectangular temp = Rectangular_;
return abs(this->point1_.getX() - this->point2_.getX()*this->point1_.getY() - this->point2_.getY()) < abs((temp.getPoint1().getX() - temp.getPoint2().getX())*(temp.getPoint1().getY() - temp.getPoint2().getY()));
}
bool Rectangular::operator>=(const Rectangular &Rectangular_)
{
Rectangular temp = Rectangular_;
return abs(this->point1_.getX() - this->point2_.getX()*this->point1_.getY() - this->point2_.getY()) >= abs((temp.getPoint1().getX() - temp.getPoint2().getX())*(temp.getPoint1().getY() - temp.getPoint2().getY()));
}
bool Rectangular::operator<=(const Rectangular &Rectangular_)
{
Rectangular temp = Rectangular_;
return abs(this->point1_.getX() - this->point2_.getX()*this->point1_.getY() - this->point2_.getY()) <= abs((temp.getPoint1().getX() - temp.getPoint2().getX())*(temp.getPoint1().getY() - temp.getPoint2().getY()));
}
bool Rectangular::operator==(const Rectangular &Rectangular_)
{
Rectangular temp = Rectangular_;
return abs(this->point1_.getX() - this->point2_.getX()*this->point1_.getY() - this->point2_.getY()) == abs((temp.getPoint1().getX() - temp.getPoint2().getX())*(temp.getPoint1().getY() - temp.getPoint2().getY()));
}
Rectangular &Rectangular::operator=(const Rectangular &Rectangular_)
{
this->point1_ = Rectangular_.point1_;
this->point2_ = Rectangular_.point2_;
this->color_ = Rectangular_.color_;
return *this;
}
Point & Rectangular::operator[](const int n)
{
if (n == 1)return this->point1_;
else if (n == 2) return this->point2_;
else throw false;
}
Rectangular::~Rectangular()
{
rectangularNum_--;
}
三角形类
Triangle.h
#pragma once
class Triangle :public DrawGraphics
{
private:
Point point1_;
Point point2_;
Point point3_;
Color color_;
static int triangleNum_;
public:
Triangle();
Triangle(Point ,Point ,Point ,Color );
void setPoint1(Point);
void setPoint2(Point);
void setPoint3(Point);
void setColor(Color);
Point getPoint1();
Point getPoint2();
Point getPoint3();
Color getColor();
static int getTriangleNum();
void draw();
Triangle(const Triangle &);
bool operator>(const Triangle&);
bool operator<(const Triangle&);
bool operator>=(const Triangle&);
bool operator<=(const Triangle&);
bool operator==(const Triangle&);
Triangle &operator=(const Triangle&);
Point& operator[](const int);
~Triangle();
};
Triangle.cpp
#include "classes.h"
int Triangle::triangleNum_ = 0;
Triangle::Triangle()
{
triangleNum_++;
this->point1_ = Point();
this->point2_ = Point();
this->point3_ = Point();
this->color_ = Color();
}
Triangle::Triangle(Point point1, Point point2, Point point3, Color color)
{
triangleNum_++;
this->point1_ = point1;
this->point2_ = point2;
this->point3_ = point3;
this->color_ = color;
}
void Triangle::setPoint1(Point _point)
{
this->point1_ = _point;
}
void Triangle::setPoint2(Point _point)
{
this->point2_ = _point;
}
void Triangle::setPoint3(Point _point)
{
this->point3_ = _point;
}
void Triangle::setColor(Color _color)
{
this->color_ = _color;
}
Point Triangle::getPoint1()
{
return this->point1_;
}
Point Triangle::getPoint2()
{
return this->point2_;
}
Point Triangle::getPoint3()
{
return this->point3_;
}
Color Triangle::getColor()
{
return this->color_;
}
int Triangle::getTriangleNum()
{
return triangleNum_;
}
void Triangle::draw()
{
int points[8];
points[0] = this->point1_.getX();
points[1] = this->point1_.getY();
points[2] = this->point2_.getX();
points[3] = this->point2_.getY();
points[4] = this->point3_.getX();
points[5] = this->point3_.getY();
points[6] = this->point1_.getX();
points[7] = this->point1_.getY();
if (this->color_.getIsFilledColor() == false) {
setcolor(this->color_.getColor());
drawpoly(4, points);
}
else {
setcolor(this->color_.getColor());
setfillcolor(this->color_.getColor());
fillpoly(4, points);
}
}
Triangle::Triangle(const Triangle & triangle)
{
triangleNum_++;
this->point1_ = triangle.point1_;
this->point2_ = triangle.point2_;
this->point3_ = triangle.point3_;
this->color_ = triangle.color_;
}
bool Triangle::operator>(const Triangle & triangle)
{
Triangle t = triangle;
return (sqrt(pow(point1_.getX() - point2_.getX(), 2) + pow(point1_.getY() - point2_.getY(), 2)) + sqrt(pow(point1_.getX() - point3_.getX(), 2) + pow(point1_.getY() - point3_.getY(), 2)) + sqrt(pow(point3_.getX() - point2_.getX(), 2) + pow(point3_.getY() - point2_.getY(), 2)))>(sqrt(pow(t.point1_.getX() - t.point2_.getX(), 2) + pow(t.point1_.getY() - t.point2_.getY(), 2)) + sqrt(pow(t.point1_.getX() - t.point3_.getX(), 2) + pow(t.point1_.getY() - t.point3_.getY(), 2)) + sqrt(pow(point3_.getX() - point2_.getX(), 2) + pow(point3_.getY() - point2_.getY(), 2)));
}
bool Triangle::operator<(const Triangle &triangle)
{
Triangle t = triangle;
return (sqrt(pow(point1_.getX() - point2_.getX(), 2) + pow(point1_.getY() - point2_.getY(), 2)) + sqrt(pow(point1_.getX() - point3_.getX(), 2) + pow(point1_.getY() - point3_.getY(), 2)) + sqrt(pow(point3_.getX() - point2_.getX(), 2) + pow(point3_.getY() - point2_.getY(), 2)))<(sqrt(pow(t.point1_.getX() - t.point2_.getX(), 2) + pow(t.point1_.getY() - t.point2_.getY(), 2)) + sqrt(pow(t.point1_.getX() - t.point3_.getX(), 2) + pow(t.point1_.getY() - t.point3_.getY(), 2)) + sqrt(pow(point3_.getX() - point2_.getX(), 2) + pow(point3_.getY() - point2_.getY(), 2)));
}
bool Triangle::operator>=(const Triangle &triangle)
{
Triangle t = triangle;
return (sqrt(pow(point1_.getX() - point2_.getX(), 2) + pow(point1_.getY() - point2_.getY(), 2)) + sqrt(pow(point1_.getX() - point3_.getX(), 2) + pow(point1_.getY() - point3_.getY(), 2)) + sqrt(pow(point3_.getX() - point2_.getX(), 2) + pow(point3_.getY() - point2_.getY(), 2))) >= (sqrt(pow(t.point1_.getX() - t.point2_.getX(), 2) + pow(t.point1_.getY() - t.point2_.getY(), 2)) + sqrt(pow(t.point1_.getX() - t.point3_.getX(), 2) + pow(t.point1_.getY() - t.point3_.getY(), 2)) + sqrt(pow(point3_.getX() - point2_.getX(), 2) + pow(point3_.getY() - point2_.getY(), 2)));
}
bool Triangle::operator<=(const Triangle &triangle)
{
Triangle t = triangle;
return (sqrt(pow(point1_.getX() - point2_.getX(), 2) + pow(point1_.getY() - point2_.getY(), 2)) + sqrt(pow(point1_.getX() - point3_.getX(), 2) + pow(point1_.getY() - point3_.getY(), 2)) + sqrt(pow(point3_.getX() - point2_.getX(), 2) + pow(point3_.getY() - point2_.getY(), 2))) <= (sqrt(pow(t.point1_.getX() - t.point2_.getX(), 2) + pow(t.point1_.getY() - t.point2_.getY(), 2)) + sqrt(pow(t.point1_.getX() - t.point3_.getX(), 2) + pow(t.point1_.getY() - t.point3_.getY(), 2)) + sqrt(pow(point3_.getX() - point2_.getX(), 2) + pow(point3_.getY() - point2_.getY(), 2)));
}
bool Triangle::operator==(const Triangle &triangle)
{
Triangle t = triangle;
return (sqrt(pow(point1_.getX() - point2_.getX(), 2) + pow(point1_.getY() - point2_.getY(), 2)) + sqrt(pow(point1_.getX() - point3_.getX(), 2) + pow(point1_.getY() - point3_.getY(), 2)) + sqrt(pow(point3_.getX() - point2_.getX(), 2) + pow(point3_.getY() - point2_.getY(), 2))) == (sqrt(pow(t.point1_.getX() - t.point2_.getX(), 2) + pow(t.point1_.getY() - t.point2_.getY(), 2)) + sqrt(pow(t.point1_.getX() - t.point3_.getX(), 2) + pow(t.point1_.getY() - t.point3_.getY(), 2)) + sqrt(pow(point3_.getX() - point2_.getX(), 2) + pow(point3_.getY() - point2_.getY(), 2)));
}
Triangle &Triangle::operator=(const Triangle &triangle)
{
this->point1_ = triangle.point1_;
this->point2_ = triangle.point2_;
this->point3_ = triangle.point3_;
this->color_ = triangle.color_;
return *this;
}
Point & Triangle::operator[](const int n)
{
switch (n)
{
case 1:
return this->point1_;
break;
case 2:
return this->point2_;
break;
case 3:
return this->point3_;
break;
default:
throw false;
break;
}
// TODO: 在此处插入 return 语句
}
Triangle::~Triangle()
{
triangleNum_--;
}