mx = int((x1 + x2) / 2) my = int((y1 + y2) / 2)
时间: 2024-03-18 15:44:09 浏览: 19
这两行代码是计算矩形框的中心点坐标。具体来说,`x1`和`y1`是矩形框的左上角点的横纵坐标,`x2`和`y2`是矩形框的右下角点的横纵坐标。那么,将这两个点的横纵坐标分别相加并除以2,就可以得到矩形框的中心点坐标。
具体实现时,使用了取整函数`int()`将计算结果取整。具体来说,`mx`的值是`(x1 + x2) / 2`的整数部分,`my`的值是`(y1 + y2) / 2`的整数部分,即矩形框中心点的横纵坐标。
相关问题
//mypoint.h class myPoint { public: myPoint(); myPoint(double x, double y); double getX(); double getY(); private: double mX,mY; };
好的,根据您给出的点类定义,我可以为您提供构造线三角形类的代码,如下所示:
```
#include <cmath>
#include "mypoint.h"
class myLine {
public:
myLine(myPoint p1, myPoint p2): mP1(p1), mP2(p2) {}
double getLength() {
double x1 = mP1.getX(), y1 = mP1.getY();
double x2 = mP2.getX(), y2 = mP2.getY();
return std::sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
}
private:
myPoint mP1, mP2;
};
class myTriangle {
public:
myTriangle(myPoint p1, myPoint p2, myPoint p3): mP1(p1), mP2(p2), mP3(p3) {}
void print() {
std::cout << "三角形的三个顶点为:" << std::endl;
std::cout << "A(" << mP1.getX() << ", " << mP1.getY() << ")" << std::endl;
std::cout << "B(" << mP2.getX() << ", " << mP2.getY() << ")" << std::endl;
std::cout << "C(" << mP3.getX() << ", " << mP3.getY() << ")" << std::endl;
std::cout << "三边长分别为:" << std::endl;
std::cout << "AB = " << mL1.getLength() << std::endl;
std::cout << "BC = " << mL2.getLength() << std::endl;
std::cout << "CA = " << mL3.getLength() << std::endl;
std::cout << "三角形的周长为:" << getPerimeter() << std::endl;
std::cout << "三角形的面积为:" << getArea() << std::endl;
}
double getPerimeter() {
return mL1.getLength() + mL2.getLength() + mL3.getLength();
}
double getArea() {
double a = mL1.getLength(), b = mL2.getLength(), c = mL3.getLength();
double p = (a + b + c) / 2;
return std::sqrt(p * (p - a) * (p - b) * (p - c));
}
private:
myPoint mP1, mP2, mP3;
myLine mL1{mP1, mP2}, mL2{mP2, mP3}, mL3{mP3, mP1};
};
```
您可以在主函数中使用该类进行测试,如下所示:
```
#include <iostream>
#include "mytriangle.h"
int main() {
myPoint p1(0, 0), p2(0, 1), p3(1, 0);
myTriangle tri(p1, p2, p3);
tri.print();
return 0;
}
```
样例输出:
```
三角形的三个顶点为:
A(0, 0)
B(0, 1)
C(1, 0)
三边长分别为:
AB = 1
BC = 1.41421
CA = 1.41421
三角形的周长为:3.41421
三角形的面积为:0.5
```
c语言编写程序根据点的类构造线三角形类,并测试 【输入形式】输入三角形的3个坐标点 【输出形式】三角形的3个坐标点、3边长及周长和面积 【样例输入】 请输入点1的x的值:1 请输入点1的y的值:1 请输入点2的x的值:4 请输入点2的y的值:5 请输入点3的x的值:8 请输入点3的y的值:9 【样例输出】 点1的坐标为:(1,1) 点2的坐标为:(4,5) 点3的坐标为:(8,9) 边长1的长度:5 边长2的长度:10.6301 边长3的长度:5.65685 该三角形的周长为:21.287 该三角形的面积为:2 【样例说明】 【评分标准】3个评分点 //根据点的定义,写出线段和三角形的定义,并通过主程序进行验证 //mypoint.h class myPoint { public: myPoint(); myPoint(double x, double y); double getX(); double getY(); private: double mX,mY; };
//myline.h
#include "mypoint.h"
class myLine{
public:
myLine();
myLine(myPoint p1, myPoint p2);
double getLength();
private:
myPoint mP1,mP2;
};
//mytriangle.h
#include "mypoint.h"
#include "myline.h"
class myTriangle{
public:
myTriangle();
myTriangle(myPoint p1, myPoint p2, myPoint p3);
double getPerimeter(); //周长
double getArea(); //面积
private:
myPoint mP1,mP2,mP3;
myLine mLine1,mLine2,mLine3;
};
//main.cpp
#include "mytriangle.h"
#include <iostream>
#include <cmath>
using namespace std;
int main(){
double x1,y1,x2,y2,x3,y3;
cout<<"请输入点1的x的值:";
cin>>x1;
cout<<"请输入点1的y的值:";
cin>>y1;
cout<<"请输入点2的x的值:";
cin>>x2;
cout<<"请输入点2的y的值:";
cin>>y2;
cout<<"请输入点3的x的值:";
cin>>x3;
cout<<"请输入点3的y的值:";
cin>>y3;
myPoint p1(x1,y1);
myPoint p2(x2,y2);
myPoint p3(x3,y3);
myTriangle tri(p1,p2,p3);
cout<<"点1的坐标为:("<<p1.getX()<<","<<p1.getY()<<")"<<endl;
cout<<"点2的坐标为:("<<p2.getX()<<","<<p2.getY()<<")"<<endl;
cout<<"点3的坐标为:("<<p3.getX()<<","<<p3.getY()<<")"<<endl;
cout<<"边长1的长度:"<<tri.mLine1.getLength()<<endl;
cout<<"边长2的长度:"<<tri.mLine2.getLength()<<endl;
cout<<"边长3的长度:"<<tri.mLine3.getLength()<<endl;
cout<<"该三角形的周长为:"<<tri.getPerimeter()<<endl;
cout<<"该三角形的面积为:"<<tri.getArea()<<endl;
return 0;
}
//mypoint.cpp
#include "mypoint.h"
myPoint::myPoint(){
mX = 0;
mY = 0;
}
myPoint::myPoint(double x, double y){
mX = x;
mY = y;
}
double myPoint::getX(){
return mX;
}
double myPoint::getY(){
return mY;
}
//myline.cpp
#include "myline.h"
#include <cmath>
myLine::myLine(){
mP1 = myPoint();
mP2 = myPoint();
}
myLine::myLine(myPoint p1, myPoint p2){
mP1 = p1;
mP2 = p2;
}
double myLine::getLength(){
return sqrt(pow(mP1.getX()-mP2.getX(),2)+pow(mP1.getY()-mP2.getY(),2));
}
//mytriangle.cpp
#include "mytriangle.h"
#include <cmath>
myTriangle::myTriangle(){
mP1 = myPoint();
mP2 = myPoint();
mP3 = myPoint();
mLine1 = myLine();
mLine2 = myLine();
mLine3 = myLine();
}
myTriangle::myTriangle(myPoint p1, myPoint p2, myPoint p3){
mP1 = p1;
mP2 = p2;
mP3 = p3;
mLine1 = myLine(mP1,mP2);
mLine2 = myLine(mP2,mP3);
mLine3 = myLine(mP3,mP1);
}
double myTriangle::getPerimeter(){
return mLine1.getLength()+mLine2.getLength()+mLine3.getLength();
}
double myTriangle::getArea(){
double a = mLine1.getLength();
double b = mLine2.getLength();
double c = mLine3.getLength();
double p = (a+b+c)/2;
return sqrt(p*(p-a)*(p-b)*(p-c));
}
相关推荐
![m](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)