C++没有使用循环,但输出语句一直输出,且顺序错乱
主函数
#include<iostream>
#include"midtermExam.h"
using namespace std;
int main()
{
Vector3 v1(1.0,2.0,2.0);
Vector3 v2(3.0,4.0,0.0);
cout<<"向量v1:"<<"("<<v1.getX()<<","<<v1.getY()<<","<<v1.getZ()<<")"<<endl;
cout<<"向量v2:"<<"("<<v2.getX()<<","<<v2.getY()<<","<<v2.getZ()<<")"<<endl;
cout<<"向量v1的模:"<<v1.module()<<endl;
cout<<"向量v2的模:"<<v2.module()<<endl;
cout<<"向量v1与v2的夹角:"<<v1.angle(v2)<<"度"<<endl;
v1.unitzation();
cout<<"向量v1单位化:"<<"("<<v1.getX()<<","<<v1.getY()<<","<<v1.getZ()<<")"<<endl;
cout<<endl;
v1++;
cout<<"运算符++重载 "<<"v1++:"<<"("<<v1.getX()<<","<<v1.getY()<<","<<v1.getZ()<<")"<<endl;
v2--;
cout<<"运算符--重载 "<<"v2--:"<<"("<<v2.getX()<<","<<v2.getY()<<","<<v2.getZ()<<")"<<endl;
Vector3 v3 = v1 + v2;
cout<<"运算符+重载 "<<"v1+v2:"<<"("<<v3.getX()<<","<<v3.getY()<<","<<v3.getZ()<<")"<<endl;
Vector3 v4 = v1 - v2;
cout<<"运算符-重载 "<<"v1-v2:"<<"("<<v4.getX()<<","<<v4.getY()<<","<<v4.getZ()<<")"<<endl;
cout<<"运算符*重载(点乘) "<<"v1*v2:"<<v1 * v2<<endl;
cout<<endl;
Vector3 point(1.5,2.0,1.0);
Vector3 vo(2.0,3.0,4.0);
Vector3 t(1.0,1.0,2.0);
Line l(vo,t);
cout<<"一点p:"<<"("<<point.getX()<<","<<point.getY()<<","<<point.getZ()<<")"<<endl;
cout<<"直线l.vo"<<"("<<l.getVO().getX()<<","<<l.getVO().getY()
<<","<<l.getVO().getZ()<<")"<<endl;
cout<<"直线l.t"<<"("<<l.getT().getX()<<","<<l.getT().getY()
<<","<<l.getT().getZ()<<")"<<endl;
cout<<"点p到直线l的距离:"<<l.pointToLineDistance(point)<<endl;
Vector3 projPoint = l.projectionPoint(point);
cout<<"点p在直线l上的投影点:"<<"("<<projPoint.getX()<<","<<projPoint.getY()
<<","<<projPoint.getZ()<<")"<<endl;
cout<<endl;
Vector3 vo1(1.0,1.5,2.0);
Vector3 t1(1.0,2.0,3.0);
Vector3 vo2(1.0,1.5,3.0);
Vector3 t2(2.0,1.0,1.0);
Line l1(vo1,t1);
Line l2(vo2,t2);
Plane plane(l1,l2);
Vector3 nV = l1.getT().crossProduct(l2.getT());//法向量
cout<<"构成平面的直线:"<<endl;
cout<<"直线l1.vo1"<<"("<<l1.getVO().getX()<<","<<l1.getVO().getY()
<<","<<l1.getVO().getZ()<<")"<<endl;
cout<<"直线l1.t1"<<"("<<l1.getT().getX()<<","<<l1.getT().getY()
<<","<<l1.getT().getZ()<<")"<<endl;
cout<<"直线l2.vo2"<<"("<<l2.getVO().getX()<<","<<l2.getVO().getY()
<<","<<l2.getVO().getZ()<<")"<<endl;
cout<<"直线l2.t2"<<"("<<l2.getT().getX()<<","<<l2.getT().getY()
<<","<<l2.getT().getZ()<<")"<<endl;
cout<<"平面plane的法向量:"<<"("<<nV.getX()<<","<<nV.getY()<<","<<nV.getZ()<<")"<<endl;
cout<<"平面plane:";
plane.planarEquation();
cout<<"点p到平面plane的距离:"<<plane.pointToPlaneDistance(point)<<endl;
return 0;
}
头文件
#pragma once
#define _USE_MATH_DEFINES
#include<iostream>
#include<cmath>
using namespace std;
class Vector3
{
private:
double x, y, z;
public:
Vector3():x(0),y(0),z(0){}
Vector3(double x,double y,double z):x(x),y(y),z(z){}
void setX(double x){
this->x = x;
}
void setY(double y){
this->y = y;
}
void setZ(double z){
this->z = z;
}
double getX(){
return x;
}
double getY(){
return y;
}
double getZ(){
return z;
}
double module(){//求模长
return sqrt(x * x + y * y + z * z);
}
double angle(Vector3 otherVector){//求夹角
return (acos((x * otherVector.x + y * otherVector.y + z * otherVector.z)
/ (sqrt(x * x + y * y + z * z) * otherVector.module())))
* 180 / M_PI ;
}
void unitzation(){//单位化
x /= sqrt(x * x + y * y + z * z);
y /= sqrt(x * x + y * y + z * z);
z /= sqrt(x * x + y * y + z * z);
}
Vector3 crossProduct(Vector3 t){//向量叉乘
return Vector3(y * t.z - z * t.y,
z * t.x - x * t.z,
x * t.y - y * t.x);
}
//运算符重载
Vector3 operator+(Vector3& otherVector){
Vector3 temp;
temp.setX(x + otherVector.getX());
temp.setY(y + otherVector.getY());
temp.setZ(z + otherVector.getZ());
return temp;
}
Vector3 operator-(Vector3& otherVector){
Vector3 temp;
temp.setX(x - otherVector.getX());
temp.setY(y - otherVector.getY());
temp.setZ(z - otherVector.getZ());
return temp;
}
double operator*(Vector3& otherVector){
return x * otherVector.getX() + y * otherVector.getY() + z * otherVector.getZ();
}
Vector3 operator++(int){
x++;
y++;
z++;
return *this;
}
Vector3 operator--(int){
x--;
y--;
z--;
return *this;
}
virtual ~Vector3(){
}
};
class Line : public Vector3
{
private:
Vector3 vo;
Vector3 t;
public:
Line():vo(Vector3()),t(Vector3()){};
Line(Vector3 vo,Vector3 t):vo(vo),t(t){};
Vector3 getT(){
return t;
}
Vector3 getVO(){
return vo;
}
Vector3 getLinePoint(double s){
Vector3 temp(s * t.getX(),s * t.getY(),s * t.getZ());
return vo + temp;
}
double pointToLineDistance(Vector3 p){//点到直线距离
Vector3 temp = p - vo;
return temp.crossProduct(t).module() / t.module();
}
Vector3 projectionPoint(Vector3 p){//投影点
Vector3 temp1 = p - vo;
t.unitzation();
double temp2 = temp1 * t;
Vector3 temp3 = Vector3(temp2 * t.getX(),temp2 * t.getY(),temp2 * t.getZ());
return vo + temp3;
}
virtual ~Line(){
}
};
class Plane : public Line
{
private:
Line l1;
Line l2;
public:
Plane():l1(Line()),l2(Line()){};
Plane(Line l1,Line l2):l1(l1),l2(l2){};
void planarEquation(){//求平面方程
Vector3 nV = l1.getT().crossProduct(l2.getT());//法向量
double d = -l1.getVO().getX() * nV.getX() - l1.getVO().getY()
* nV.getY() - l1.getVO().getZ() * nV.getZ();
cout<<nV.getX()<<"x"<<"+"<<nV.getY()<<"y"<<"+"<<nV.getZ()<<"z"<<"="<<d<<endl;
}
double pointToPlaneDistance(Vector3 p){//点到平面距离
Vector3 nV = l1.getT().crossProduct(l2.getT());//法向量
double d = -l1.getVO().getX() * nV.getX() - l1.getVO().getY()
* nV.getY() - l1.getVO().getZ() * nV.getZ();
double temp1 = fabs(nV.getX() * p.getX() + nV.getY()
* p.getY() + nV.getZ() * p.getZ() - d);
double temp2 = nV.module();
return temp1 / temp2;
}
virtual ~Plane(){
}
};
输出结果

输出结果中投影点一直输出,求助
...全文
相关推荐
修复cpp默认按字典序输出导致字段顺序错乱的问题
cpp默认使用std::map存储键值对,导致序列化时字段按key字母顺序排列,而非插入顺序。本资源提供已修改的cpp源码,通过替换内部容器为std::vector+map组合或采用有序映射实现,确保
字幕 顺序轴数字 矫正
在某些情况下,如字幕分割后,可能会导致SRT字幕的顺序轴出现问题,即字幕出现错乱或不连续的情况。这通常是因为在分割过程中,字幕编号没有正确地重新排序。
c++难以发现的bug(有趣)
"C++编程中的一些隐晦且有趣的bug,包括与assert相关的、对象成员数据错乱以及内存泄漏的问题。"在C++编程中,有时会遇到一些难以发现的bug,这些bug可能会让开发者花费大量时间和精力去
全局hook实现鼠标反向移动,键盘输入错乱
在这个场景中,"全局hook实现鼠标反向移动,键盘输入错乱"是一个有趣但可能具有破坏性的应用,它可以作为整人程序使用,但也揭示了Hook技术的基本原理和应用。首先,让我们了解什么是Hook。
用C++解八数码问题
`out()`函数则用于输出整个解题路径。在主函数`main()`中,首先初始化了状态队列`ar1`和`ar2`,然后在一个循环中,不断尝试移动空位并检查是否到达目标状态。


