为了账号安全,请及时绑定邮箱和手机立即绑定

求解矩形解法C++

求解矩形解法C++

C++
风韵犹存的小明 2018-08-20 10:36:04
设计一个点类Point,再设计一个矩形类,矩形类使用Point类的两个坐标点作为矩形对角顶点,并可以输出4个坐标值和面积。使用测试程序验证程序。求助各位同学,问题在代码中, 万分感谢。class Point { private:        float x, y; public:        Point() { x = 0; y = 0; }        Point(float X, float Y) { x = X; y = Y; }             float getX() { return x; };        float getY() { return y; };        void setX(float X) { x = X; };        void setY(float Y) { y = Y; }; };class Rectangular { private:     Point point[4]; public:        Rectangular(Point a, Point d) {                //请问这段矩形构造函数是什么意思?()               point[0] = a;                              point[1].setX(d.getX());                //为什么是d.               point[1].setY(a.getY());                               point[2] = d;                               point[3].setX(a.getX());                //为什么是a.               point[3].setY(d.getY());        }        void printPointsLocation() {                for(int i = 0; i < 4; ++i) {  std::cout << point[i].getX() << ", " << point[i].getY() << std::endl;    }     }        float getArea() {               float height, width, area;              height = point[0].getY() - point[3].getY();                //这里减的意思是?   width = point[1].getX() - point[0].getX();                   area = height * width;                  return area;        }        void printArea() { std::cout << "area:" << getArea() << std::endl; } };
查看完整描述

1 回答

已采纳
?
AAnonymous

TA贡献62条经验 获得超31个赞

// Regarding the constructor
+-------------------------> x
|   a
| (a.x, a.y)         (d.x, a.y)
| [0]----------------[1]
|  |                  |
|  |                  |
|  |                  |
| [3]----------------[2]
| (a.x, d.y)         (d.x, d.y)
|                     d
y

// Suppose given point a(0, 0) and point d(2, 2).
// We have point[0] (0, 0), point[1] (2, 0), 
// point[2] (2, 2), point[3] (0, 2)

// Regarding area calculation
// area = width x height
//      = ([1].x - [0].x) x ([3].y - [0].y)
// The y-axis's direction in your code is opposite to mine,
// so you use ([0].y - [3].y)


查看完整回答
1 反对 回复 2018-08-21
  • 1 回答
  • 0 关注
  • 1128 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信