三维向量的标准化,两点距离,点乘,叉乘

该博客介绍了如何在三维空间中进行向量操作,包括计算向量的长度、两点之间的距离、点乘以及叉乘。提供了C++实现的代码示例,用于标准化向量、计算距离、点乘和叉乘,并在主函数中展示了输入和输出的示例。
摘要由CSDN通过智能技术生成
#include <iostream>
using namespace std;
struct vector
{  
	float x;
	float y;
	float z;

};
 vector c;
 float Get_Length(vector &a)
 {
	 return a.x*a.x+a.y*a.y+a.z*a.z;
 }
 float Distance(vector &a,vector &b)
 {
	 vector c;
	 c.x = a.x -b.x;
	 c.y = a.y -b.y;
	 c.z = a.z -b.z;
	 float distance=sqrt(Get_Length(c));
	return distance;
 }
 float Dot_Porduct(vector &a,vector &b)
 {
	 return a.x*b.x+a.y*b.y+a.z*b.z;
 }

 vector Cross_Product(vector &a,vector &b)
 { 
	 vector c;
	 c.x=a.y*b.z-a.z-b.y;
	 c.y=a.z*b.x-a.x*b.z;
	 c.z=a.x*b.y-a.y*b.x;
	 return c;
 }
  vector Normal(vector &a)
  {
	  float length=sqrt(Get_Length(a));
	  vector normal;
	  normal.x=a.x/length;
	  normal.y=a.y/length;
	  normal.z=a.z/length;
	  return normal;
}
  void main()
  {
	  vector a;
	  vector b;
	  vector normal;
	  cout<<"Enter 3 numbers :"<<endl;
	  cin>&g
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的C++三维向量类的示例代码,包含了向量加、减、点乘叉乘运算: ```cpp #include <iostream> #include <cmath> using namespace std; class Vector3D { public: Vector3D() : x(0), y(0), z(0) {} Vector3D(double x, double y, double z) : x(x), y(y), z(z) {} Vector3D operator+(const Vector3D& v) const { return Vector3D(x + v.x, y + v.y, z + v.z); } Vector3D operator-(const Vector3D& v) const { return Vector3D(x - v.x, y - v.y, z - v.z); } double operator*(const Vector3D& v) const { return x * v.x + y * v.y + z * v.z; } Vector3D operator^(const Vector3D& v) const { return Vector3D(y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x); } double length() const { return sqrt(x * x + y * y + z * z); } void normalize() { double len = length(); if (len != 0) { x /= len; y /= len; z /= len; } } void print() const { cout << "(" << x << ", " << y << ", " << z << ")" << endl; } private: double x, y, z; }; int main() { Vector3D v1(1, 2, 3); Vector3D v2(4, 5, 6); Vector3D v3 = v1 + v2; v3.print(); Vector3D v4 = v1 - v2; v4.print(); double dot_product = v1 * v2; cout << "Dot Product: " << dot_product << endl; Vector3D cross_product = v1 ^ v2; cross_product.print(); return 0; } ``` 该类实现了向量加、减、点乘叉乘运算,以及向量的长度归一化和输出功能。在主函数中,我们创建了两个三维向量v1和v2,并使用运算符重载实现了向量加、减、点乘叉乘运算。最后,我们输出了向量点乘叉乘结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值