初始速度

//
// vector.h
//
// Vector structure and its operations for 3-d space vectors.
//
// Written by: Yanting Wang           12/25/2002
//

#include <iostream>
using namespace std;

#include <math.h>

#ifndef __VECTOR__H
#define __VECTOR__H

struct Vector
{
  double x, y, z;

  Vector() : x(0.0), y(0.0), z(0.0) {}
  Vector( double x, double y, double z ) : x(x), y(y), z(z) {}

  void set( double xx, double yy, double zz ) { x = xx; y = yy; z = zz; }

  void clear() { x = 0.0;   y = 0.0;   z = 0.0; }

  // square length of a vector
  double r2() const { return x*x + y*y + z*z; }

  // length of a vector
  double r() const { return sqrt( r2() ); }

  // the unit vector
  Vector normal() const
  {
    double r = sqrt( x*x + y*y + z*z );
    return Vector( x/r, y/r, z/r );
  }

  const Vector &operator = ( const Vector &v ) 
  { 
    x = v.x;   y = v.y;   z = v.z; 
    return v;
  }

  const Vector &operator += ( const Vector &v )
  {
    x += v.x;   y += v.y;   z += v.z;
    return *this;
  }

  const Vector &operator -= ( const Vector &v )
  {
    x -= v.x;   y -= v.y;   z -= v.z;
    return *this;
  }

  const Vector &operator *= ( double d )
  {
    x *= d;   y *= d;   z *= d;
    return *this;
  }

  const Vector &operator /= ( double d )
  {
    x /= d;   y /= d;   z /= d;
    return *this;
  }
};


Vector operator+( const Vector &v1, const Vector &v2 )
{
  return Vector( v1.x + v2.x, v1.y + v2.y, v1.z + v2.z );
}

Vector operator-( const Vector &v1, const Vector &v2 )
{
  return Vector( v1.x - v2.x, v1.y - v2.y, v1.z - v2.z );
}

Vector operator-( const Vector &v1 )
{
  return Vector( -v1.x, -v1.y, -v1.z );
}

Vector operator*( const Vector &v, const double d )
{
  return Vector( v.x*d, v.y*d, v.z*d );
}

Vector operator*( const double d, const Vector &v )
{
  return Vector( v.x*d, v.y*d, v.z*d );
}

//scalar product
double operator*( const Vector &v1, const Vector &v2 )
{
  return v1.x*v2.x + v1.y*v2.y + v1.z*v2.z;
}

//vector product
Vector operator^( const Vector &v1, const Vector &v2 )
{
  return Vector( v1.y*v2.z - v1.z*v2.y,
		 v1.z*v2.x - v1.x*v2.z,
		 v1.x*v2.y - v1.y*v2.x );
}

Vector operator/( const Vector &v, const double d )
{
  return Vector( v.x/d, v.y/d, v.z/d );
}

ostream &operator<<( ostream &output, const Vector &p )
{
  output << p.x << " " << p.y << " " << p.z;
  return output;
}

istream &operator>>( istream &input, Vector &p )
{
  input >> p.x >> p.y >> p.z;
  return input;
}

#endif //__VECTOR__H

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值