C++程序设计原理与实践 习题答案 第十七章 第17章习题答案

第十七章:一个显示模型 习题答案

17.1

#include"../../lib_files/std_lib_facilities.h"
#include"../../lib_files/Graph.h"
#include"../../lib_files/Simple_window.h"


int main()
try
{
	using namespace Graph_lib;
	Point tl{ 100,100 };
	Simple_window win{ tl, 600, 400, "17_1" };

	Graph_lib::Rectangle r{Point{200,200},100,50};
	r.set_color(Color::blue);
	win.attach(r);

	Graph_lib::Polygon pr;
	pr.add(Point{ 400,200 });
	pr.add(Point{ 500,200 });
	pr.add(Point{ 500,300 });
	pr.add(Point{ 400,300 });
	pr.set_color(Color::red);
	win.attach(pr);

	win.wait_for_button();
	return 0;
}
catch (exception& e)
{
	cerr << "Exception: " << e.what() << endl;
	return 1;
}
catch (...)
{
	cerr << "Exception occurred\n";
	return 2;
}

17.2

#include"../../lib_files/std_lib_facilities.h"
#include"../../lib_files/Graph.h"
#include"../../lib_files/Simple_window.h"


int main()
try
{
	using namespace Graph_lib;
	Point tl{ 100,100 };
	Simple_window win{ tl, 600, 400, "17_2" };

	Graph_lib::Rectangle r{ Point{300,150},100,30 };
	r.set_color(Color::blue);
	win.attach(r);
	
	Text t{ Point{320,170},"Howdy!" };
	t.set_color(Color::red);
	win.attach(t);

	win.wait_for_button();
	return 0;
}
catch (exception& e)
{
	cerr << "Exception: " << e.what() << endl;
	return 1;
}
catch (...)
{
	cerr << "Exception occurred\n";
	return 2;
}

17.3

#include"../../lib_files/std_lib_facilities.h"
#include"../../lib_files/Graph.h"
#include"../../lib_files/Simple_window.h"


int main()
try
{
	using namespace Graph_lib;
	Point tl{ 100,100 };
	Simple_window win{ tl, 600, 400, "17_1" };
	win.set_label("17_3");

	Text t1{ Point{250,150},"A" };
	t1.set_font(Font::times_bold);
	t1.set_font_size(150);
	t1.set_color(Color::red);
	win.attach(t1);

	win.wait_for_button();
	return 0;
}
catch (exception& e)
{
	cerr << "Exception: " << e.what() << endl;
	return 1;
}
catch (...)
{
	cerr << "Exception occurred\n";
	return 2;
}

17.4

#include"../../lib_files/std_lib_facilities.h"
#include"../../lib_files/Graph.h"
#include"../../lib_files/Simple_window.h"


int main()
try
{
	using namespace Graph_lib;
	Point tl{ 100,100 };
	Simple_window win{ tl, 600, 400, "17_1" };
	win.set_label("17_4");

	const int N{ 3 };	//3x3棋盘格
	const int init_x{ 100 }, init_y{ 100 };	//棋盘格左上角的位置
	int x{ init_x }, y{ init_y };
	const int step{ 100 };	//棋盘格每一格的长和宽
	vector<Graph_lib::Rectangle> vr;
	vector<Color> vc;
	vc.push_back(Color::red);
	vc.push_back(Color::white);
	int choose{0};
	
	Graph_lib::Rectangle r1c1{ Point{x,y},step,step };
	r1c1.set_fill_color(vc[choose]);
	win.attach(r1c1);
	choose = int(!choose);
	x += step;
	Graph_lib::Rectangle r1c2{ Point{x,y},step,step };
	r1c2.set_fill_color(vc[choose]);
	win.attach(r1c2);
	choose = !choose;
	x += step;
	Graph_lib::Rectangle r1c3{ Point{x,y},step,step };
	r1c3.set_fill_color(vc[choose]);
	win.attach(r1c3);
	choose = !choose;
	x = init_x;
	y += step;

	Graph_lib::Rectangle r2c1{ Point{x,y},step,step };
	r2c1.set_fill_color(vc[choose]);
	win.attach(r2c1);
	choose = !choose;
	x += step;
	Graph_lib::Rectangle r2c2{ Point{x,y},step,step };
	r2c2.set_fill_color(vc[choose]);
	win.attach(r2c2);
	choose = !choose;
	x += step;
	Graph_lib::Rectangle r2c3{ Point{x,y},step,step };
	r2c3.set_fill_color(vc[choose]);
	win.attach(r2c3);
	choose = !choose;
	x = init_x;
	y += step;

	Graph_lib::Rectangle r3c1{ Point{x,y},step,step };
	r3c1.set_fill_color(vc[choose]);
	win.attach(r3c1);
	choose = !choose;
	x += step;
	Graph_lib::Rectangle r3c2{ Point{x,y},step,step };
	r3c2.set_fill_color(vc[choose]);
	win.attach(r3c2);
	choose = !choose;
	x += step;
	Graph_lib::Rectangle r3c3{ Point{x,y},step,step };
	r3c3.set_fill_color(vc[choose]);
	win.attach(r3c3);

	win.wait_for_button();
	return 0;
}
catch (exception& e)
{
	cerr << "Exception: " << e.what() << endl;
	return 1;
}
catch (...)
{
	cerr << "Exception occurred\n";
	return 2;
}

17.5

#include"../../lib_files/std_lib_facilities.h"
#include"../../lib_files/Graph.h"
#include"../../lib_files/Simple_window.h"


int main()
try
{
	using namespace Graph_lib;
	using Graph_lib::Rectangle;
	using Graph_lib::Polygon;
	Point tl{ 100,100 };
	Simple_window win{ tl, 1200, 800, "17_1" };
	win.set_label("17_5");

	Rectangle r{ Point{100,100},win.x_max()*2/3,win.y_max()*3/4 };
	r.set_color(Color::blue);
	win.attach(r);

	Polygon poly;
	poly.add(Point{ 600, 200 });
	poly.add(Point{ 800, 200 });
	poly.add(Point{ 800, 400 });
	poly.add(Point{ 600, 400 });
	poly.set_color(Color::red);
	poly.set_style(Line_style(Line_style::solid,4));
	win.attach(poly);

	win.wait_for_button();
	return 0;
}
catch (exception& e)
{
	cerr << "Exception: " << e.what() << endl;
	return 1;
}
catch (...)
{
	cerr << "Exception occurred\n";
	return 2;
}

17.6

超出窗口区域的内容都会被图形系统剪裁掉

17.7

#include"../../lib_files/std_lib_facilities.h"
#include"../../lib_files/Graph.h"
#include"../../lib_files/Simple_window.h"


int main()
try {
    using namespace Graph_lib;
    using Graph_lib::Rectangle;
    using Graph_lib::Polygon;

    
    Point tl{ 200, 200 };
    Simple_window win{ tl, 600, 400, "17_7" };

    // body
    Rectangle body{ Point{200, 200}, 200, 100 };
    body.set_color(Color::blue);
    body.set_fill_color(Color::blue);
    win.attach(body);

    // chimney
    Rectangle chimney{ Point{225, 125}, 25, 75 };
    chimney.set_color(Color::red);
    chimney.set_fill_color(Color::red);
    win.attach(chimney);

    // roof
    Polygon roof;
    roof.add(Point{ 300, 100 });
    roof.add(Point{ 420, 200 });
    roof.add(Point{ 180, 200 });
    roof.set_color(Color::black);
    roof.set_fill_color(Color::black);
    win.attach(roof);

    // door
    Rectangle door{ Point{280, 240}, 40, 60 };
    door.set_color(Color::white);
    door.set_fill_color(Color::white);
    win.attach(door);

    Mark m{ Point{290, 270}, 'o' };
    win.attach(m);

    // windows
    Rectangle window1{ Point{235, 235}, 30, 30 };
    window1.set_color(Color::white);
    window1.set_fill_color(Color::dark_blue);
    window1.set_style(Line_style(Line_style::solid, 4));
    win.attach(window1);

    Rectangle window2{ Point{335, 235}, 30, 30 };
    window2.set_color(Color::white);
    window2.set_fill_color(Color::dark_blue);
    window2.set_style(Line_style(Line_style::solid, 4));
    win.attach(window2);

    win.wait_for_button();
}
catch (exception& e) {
    cerr << "exception: " << e.what() << '\n';
    return 1;
}
catch (...) {
    cerr << "error\n";
    return 2;
}

17.8

#include"../../lib_files/std_lib_facilities.h"
#include"../../lib_files/Graph.h"
#include"../../lib_files/Simple_window.h"

int main()
try
{
	using namespace Graph_lib;
	using Graph_lib::Polygon;
	using Graph_lib::Rectangle;
	using Graph_lib::Ellipse;

	Point tl{ 100,100 };
	Simple_window win{ tl, 600,400,"Olympic Logo" };

	Circle c1{ Point{200,150}, 50 };
	c1.set_color(Color::blue);
	c1.set_style(Line_style(Line_style::solid, 4));
	win.attach(c1);
	
	Circle c2{ Point{305,150}, 50 };
	c2.set_color(Color::black);
	c2.set_style(Line_style(Line_style::solid, 4));
	win.attach(c2);
	
	Circle c3{ Point{410,150}, 50 };
	c3.set_color(Color::red);
	c3.set_style(Line_style(Line_style::solid, 4));
	win.attach(c3);
	
	Circle c4{ Point{252,200}, 50 };
	c4.set_color(Color::yellow);
	c4.set_style(Line_style(Line_style::solid, 4));
	win.attach(c4);
	
	Circle c5{ Point{357,200}, 50 };
	c5.set_color(Color::green);
	c5.set_style(Line_style(Line_style::solid, 4));
	win.attach(c5);

	win.wait_for_button();
	return 0;
}
catch (exception& e)
{
	cerr << "Exception: " << e.what() << endl;
	return 1;
}
catch (...)
{
	cerr << "Exception occurred\n";
	return 2;
}

17.11

#include"../../lib_files/std_lib_facilities.h"
#include"../../lib_files/Graph.h"
#include"../../lib_files/Simple_window.h"
#include <cmath>


int main()
try {
    // draw a series of regular polygons from inside out: equilateral triangle,
    // square, pentagon, etc.

    using namespace Graph_lib;
    using Graph_lib::Polygon;
    using Graph_lib::Rectangle;

    Point tl{ 100, 100 };
    Simple_window win{ tl, 600, 400, "17_11" };
    
    constexpr double pi = 3.14159265;
    //X,Y 是中心点
    const int X = win.x_max() / 2 - 50;
    const int Y = win.y_max() / 2 - 50;
    //len是所有多边形的边长
    const int len = 100;

    //正三角形
    Polygon tri;
    int pr1 = len / 2 / sin(pi * 60 / 180);
    tri.add(Point{ X - len / 2, Y + pr1 / 2 });  //左下
    tri.add(Point{ X, Y - pr1 });               //中上
    tri.add(Point{ X + len / 2, Y + pr1 / 2 }); //右下
    tri.set_color(Color::red);
    win.attach(tri);

    //正方形
    Rectangle sqr{ Point{X - len / 2,Y - len / 2}, len, len };
    sqr.set_color(Color::blue);
    win.attach(sqr);

    //正五边形:内角108°,中心旋转角72°
    Polygon penta;
    int pr2 = len / 2 / sin(pi * 36 / 180);
    penta.add(Point{ static_cast<int>(X - len * sin(pi * 54 / 180)), static_cast<int>(Y - pr2 * cos(pi * 72 / 180)) }); //最左边
    penta.add(Point{ X,Y - pr2 });          //正中上
    penta.add(Point{ static_cast<int>(X + len * sin(54.0 / 180 * pi)), static_cast<int>(Y - pr2 * cos(72.0 / 180 * pi)) }); //最右边
    penta.add(Point{ static_cast<int>(X + pr2 * sin(36.0 / 180 * pi)), static_cast<int>(Y + pr2 * cos(36.0 / 180 * pi)) });  //右下
    penta.add(Point{ static_cast<int>(X - pr2 * sin(36.0 / 180 * pi)) ,static_cast<int>(Y + pr2 * cos(36.0 / 180 * pi)) });  //左下
    penta.set_color(Color::yellow);
    win.attach(penta);

    //正六边形:内角120°,中心旋转角60°
    Polygon hex;
    int pr3 = len;
    hex.add(Point{ X - pr3, Y });    //最左边
    hex.add(Point{ static_cast<int>(X - pr3 * cos(60.0 / 180 * pi)), static_cast<int>(Y - pr3 * sin(60.0 / 180 * pi)) });   //左上
    hex.add(Point{ static_cast<int>(X + pr3 * cos(60.0 / 180 * pi)), static_cast<int>(Y - pr3 * sin(60.0 / 180 * pi)) });   //右上
    hex.add(Point{ X + pr3, Y });    //最右边
    hex.add(Point{ static_cast<int>(X + pr3 * cos(60.0 / 180 * pi)), static_cast<int>(Y + pr3 * sin(60.0 / 180 * pi)) });   //右下
    hex.add(Point{ static_cast<int>(X - pr3 * cos(60.0 / 180 * pi)), static_cast<int>(Y + pr3 * sin(60.0 / 180 * pi)) });   //左上
    hex.set_color(Color::green);
    win.attach(hex);

    //画不动了...

    win.wait_for_button();
    return 0;
}
catch (exception& e) {
    cerr << "Exception: " << e.what() << '\n';
    return 1;
}
catch (...) {
    cerr << "Exception occurred!\n";
    return 2;
}

17.12

#include"../../lib_files/std_lib_facilities.h"
#include"../../lib_files/Graph.h"
#include"../../lib_files/Simple_window.h"
#include<cmath>


/*
Reference by Wikipedia
superellipse
x(t) = |cos(t)|^(2/m) * a * sgn(cos(t))
y(t) = |sin(t)|^(2/n) * b * sgn(sin(t))
	0 <= t < 2*pi
sgn(w) = -1, w < 0
		 0,  w = 0
		 1,  w > 0
*/
int sgn(double w);
vector<pair<double, double>> superellipse(double a, double b, double m, double n, int N)
{
	if((a <= 0 || b <= 0) || (m <= 0 || n <= 0))
		error("superellipse needs positive arguments");
	if (N <= 0)
		error("number of points must be positive");
	constexpr double pi{ 3.1415926 };
	vector<pair<double, double>> solutions;
	double p = 2 * pi / N;
	double x, y, t;

	for (size_t i = 0; i < N; i++)
	{
		t = p * i;
		x = pow(abs(cos(t)), 2.0 / m) * a * sgn(cos(t));
		y = pow(abs(sin(t)), 2.0 / n) * b * sgn(sin(t));
		solutions.push_back(make_pair(x, y));
	}
	return solutions;
}

int sgn(double w)
{
	if (w < 0)
		return -1;
	else if (w > 0)
		return 1;
	else
		return 0;
}

int main()
try
{
	using namespace Graph_lib;
	using Graph_lib::Polygon;
	using Graph_lib::Rectangle;
	using Graph_lib::Ellipse;

	Point tl{ 100,100 };
	Simple_window win{ tl, 600,400,"17_12" };
	const int Xc = win.x_max() / 2;	//中心点
	const int Yc = win.x_max() / 2;

	double a, b, m, n;
	int N;
	cout << "input parameters of superellipse, a, b, m, n, and type in number of points that you want\n";
	cin >> a >> b >> m >> n >> N;
	vector<pair<double, double>> superellipse_solution = superellipse(a, b, m, n, N);

	vector<Point>vt;
	for (const pair<double, double>& p : superellipse_solution)
		vt.push_back(Point{ static_cast<int>(p.first + Xc), static_cast<int>(p.second + Yc) });

	//连接其他所有点
	Lines lis;
	for (size_t i = 0; i < N - 1; i++)
	{
		for (size_t j = i + 1; j < N; j++)
			lis.add(vt[i], vt[j]);
	}
	lis.set_color(Color::black);
	win.attach(lis);

	win.wait_for_button();
	return 0;
}
catch (exception& e)
{
	cerr << "Exception: " << e.what() << endl;
	return 1;
}
catch (...)
{
	cerr << "Exception occurred\n";
	return 2;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值