zoukankan      html  css  js  c++  java
  • 2d平面向量计算

    PlaneVector.hpp

    #ifndef PlaneVector_h__
    #define PlaneVector_h__
    
    template<typename coordinate_type>
    struct PlaneVector
    {
    	coordinate_type x;
    	coordinate_type y;
    };
    
    template<typename coordinate_type>
    std::ostream& operator<<(std::ostream& out, const PlaneVector<coordinate_type>& pt) {
    	out << '(' << pt.x << ',' << pt.y << ')';
    	return out;
    }
    
    /*!
    	向量加法
    	首尾相连,连接首尾,指向终点
    */
    template<typename coordinate_type>
    PlaneVector<coordinate_type> add(const PlaneVector<coordinate_type>& p1, const PlaneVector<coordinate_type>& p2)
    {
    	return (p1.x + p2.x, p1.y + p2.y);
    }
    
    /*!
    	向量减法
    	同起点,指被减(减向量终点指向被减向量终点)
    */
    template<typename coordinate_type>
    PlaneVector<coordinate_type> sub(const PlaneVector<coordinate_type>& p1, const PlaneVector<coordinate_type>& p2)
    {
    	return (p1.x - p2.x, p1.y - p2.y);
    }
    
    /*!
    	点乘
    	a * b = |a||b|cosθ
    	a·b>0    方向基本相同,向量夹角为锐角
    	a·b=0    正交,相互垂直
    	a·b<0    方向基本相反,向量夹角为钝角
    */
    template<typename coordinate_type>
    coordinate_type dotProduct(const PlaneVector<coordinate_type>& p1, const PlaneVector<coordinate_type>& p2)
    {
    	return p1.x * p2.x + p1.y * p2.y;
    }
    
    /*!
    	叉乘
    	a ^ b = |a||b|sinθ
    	共起点向量 a 和 向量 b 所构成平行四边形的面积
    */
    template<typename coordinate_type>
    PlaneVector<coordinate_type> crossProduct(const PlaneVector<coordinate_type>& p1, const PlaneVector<coordinate_type>& p2)
    {
    	return (p1.y * 0 - 0 * p2.y, 0 * p2.x - p1.x * 0);
    }
    
    #endif // PlaneVector_h__
    
    
    转载请注明出处并保持作品的完整性,谢谢
  • 相关阅读:
    Jquery Ajax 调用 WebService
    Dapper.NET
    HTML5 canvas标签
    SQL内外左右交叉连接
    水晶报表纵向重复
    AngularJS 菜鸟
    什么是MVC框架?
    伪类和伪元素的区别
    常用的本地存储-----cookie篇
    JavaScript中基本数据类型和引用数据类型的区别
  • 原文地址:https://www.cnblogs.com/cheungxiongwei/p/14478689.html
Copyright © 2011-2022 走看看