zoukankan      html  css  js  c++  java
  • 三维向量类

    public class Vector3
     {
      public float [] vector;
      private const float E = 0.0000001f;

      public Vector3(float x,float y, float z)
      {
       vector = new float[3]{x,y,z};
      }
      public Vector3(Vector3 vct)
      {
       vector = new float[3];
       vector[0] = vct.x;
       vector[1] = vct.y;
       vector[2] = vct.z;
      }
      public float x
      {
       get{return vector[0];}
       set{vector[0] = value;}
      }
      public float y
      {
       get{return vector[1];}
       set{vector[1] = value;}
      }
      public float z
      {
       get{return vector[2];}
       set{vector[2] = value;}
      }
      public override string ToString()
      {
       return "("+x+","+y+","+z+")";
      }
      public static Vector3 operator +(Vector3 lhs, Vector3 rhs)//向量加法
      {
       Vector3 result = new Vector3(lhs);
       result.x += rhs.x;
       result.y += rhs.y;
       result.z += rhs.z;
       return result;
      }
      public static Vector3 operator -(Vector3 lhs, Vector3 rhs)//向量减法
      {
       Vector3 result = new Vector3(lhs);
       result.x -= rhs.x;
       result.y -= rhs.y;
       result.z -= rhs.z;
       return result;
      }
      public static Vector3 operator /(Vector3 lhs, float rhs)//向量除以数量
      {
       if(rhs!=0)
           return new Vector3(lhs.x/rhs, lhs.y/rhs, lhs.z/rhs);
       else
        return new Vector3(0,0,0);
      }
      public static Vector3 operator *(float lhs, Vector3 rhs)//左乘数量
      {
       return new Vector3(lhs*rhs.x, lhs*rhs.y, lhs*rhs.z);
      }
      public static Vector3 operator *(Vector3 lhs, float rhs)//右乘数量
      {
       return new Vector3(lhs.x*rhs, lhs.y*rhs, lhs.z*rhs);
      }
      public static float operator *(Vector3 lhs, Vector3 rhs)//向量数性积
      {
       return lhs.x*rhs.x +lhs.y*rhs.y + lhs.z*rhs.z;
      }
      public static bool operator ==(Vector3 lhs, Vector3 rhs)
      {
       if(Math.Abs(lhs.x-rhs.x)    return true;
       else
        return false;
      }
      public static bool operator !=(Vector3 lhs, Vector3 rhs)
      {
       return !(lhs==rhs);
      }
      public override bool Equals(object obj)
      {
       return base.Equals (obj);
      }
      public override int GetHashCode()
      {
       return base.GetHashCode ();
      }
      ///
      /// 向量叉积,求与两向量垂直的向量
      ///

      public static Vector3 Cross(Vector3 v1, Vector3 v2)
      {
       Vector3 r = new Vector3(0,0,0);
       r.x = (v1.y * v2.z) - (v1.z * v2.y);
       r.y = (v1.z * v2.x) - (v1.x * v2.z);
       r.z = (v1.x * v2.y) - (v1.y * v2.x);
       return r;
      }
      ///
      /// 求向量长度
      ///

      public static float Magnitude(Vector3 v1)
      {
       return (float)Math.Sqrt( (v1.x * v1.x) + (v1.y * v1.y) + (v1.z * v1.z) );
      }
      ///
      /// 单位化向量
      ///

      public static Vector3 Normalize(Vector3 v1)
      {
       float magnitude = Magnitude(v1);    
       v1 = v1 / magnitude;  
     
       return v1;         
      }
     }
  • 相关阅读:
    vue中表格自适应屏幕一屏显示
    css+jq实现星星评分
    CSS中width,min-width和max-width之间的联系
    用jq动态给导航菜单添加active
    解决ios中input兼容性问题
    swiper按钮点击无效及控制器无效问题
    bootstrap 模态框在iphone微信内点击无效
    vue,onerror实现当图片加载失败时使用默认图
    MVC模板页使用
    MVC框架+vue+elementUI
  • 原文地址:https://www.cnblogs.com/gxlinhai/p/1009385.html
Copyright © 2011-2022 走看看