美文网首页
Vec2 (二维向量)

Vec2 (二维向量)

作者: blue_lights | 来源:发表于2021-09-09 10:34 被阅读0次

点乘、叉乘参考文章:

https://zhuanlan.zhihu.com/p/359975221

点和向量可以理解为三维空间里的基本元素对象

对点和向量的加、减、乘、除、点乘、叉乘、相交、平行等操作,可以看成是行为、

面向对象的编程:对象+行为

点乘也叫数量积。结果是一个向量在另一个向量方向上投影的长度,是一个标量。

叉乘,也叫向量积。结果是一个和已有两个向量都垂直的向量。

Vec.h

#include<stdio.h>

#include <algorithm>

#include<cmath>

class  Vec2 {

public:

    floatx =0.f;

    floaty =0.f;

    Vec2(floatx_,floaty_):x(x_) ,y(y_) { }

    Vec2(constVec2& other):x(other.x), y(other.y) { }

    Vec2(constfloat* arr):x(arr[0]),y(arr[1]) {}

    /**

     *点乘

     */

    inline float Dot(const Vec2& v) const {

        return(x * v.x + y * v.y);

    }

    staticfloatDot(constVec2& v1,constVec2& v2) {

        return(v1.x * v2.x + v1.y * v2.y);

    }

    inlinevoidScale(floatscalar) {

        x *= scalar;

        y *= scalar;

    }

    inline Vec2 operator*(float s) const {

        Vec2result(*this);

        result.Scale(s);

        returnresult;

    }

    /**

     *叉乘

     */

    inlineVec2Project(constVec2& other)const{

        returnother * (Dot(other)/other.Dot(other));

    }

    /**

     *向量长度

     */

    floatLength()const{

        returnstd::sqrt(x * x + y * y);

    }

    inline float LengthSquared() const {

        return(x * x + y * y);

    }

    inline float GetLength() const {

        returnsqrtf(x*x + y*y);

    }

    /**

     *向量相减

     */

    inlinevoidSubtract(constVec2& v) {

        x -= v.x;

        y -= v.y;

    }

    /**

     *operator-

     */

    inlineVec2operator-(constVec2& v)const{

        Vec2 result(*this);

        result.Subtract(v);

        returnresult;

    }

    /**

     *两点之间距离

     */

    inlinefloatGetDistance(constVec2& other)const{

        return(*this- other).GetLength();

    }

    /**

     *两条线是否相交

     */

    staticboolIsLineIntersect(constVec2& A,constVec2& B,

                                 constVec2& C,constVec2& D,

                                 float*S =nullptr,float*T =nullptr);

    /**

     *两条线是否平行

     */

    staticboolIsLineParallel(constVec2& A,constVec2& B,

                   constVec2& C,constVec2& D);

};

Vec2.cc

/**

 *cross product of 2 vector. A->B X C->D

 */

floatcrossProduct2Vector(constVec2& A,constVec2& B,constVec2& C,constVec2& D) {

    return(D.y - C.y) * (B.x - A.x) - (D.x - C.x) * (B.y - A.y);

}

/**

 *两条线是否相交

 */

boolVec2::IsLineIntersect(constVec2& A,constVec2& B,

                             constVec2& C,constVec2& D,

                            float*S,float*T) {

    if( (A.x==B.x && A.y==B.y) || (C.x==D.x && C.y==D.y) ) {

        return false;

    }

    constfloatdenom = crossProduct2Vector(A, B, C, D);

    if(denom ==0) {

        // Lines parallel or overlap

        return false;

    }

    if(S !=nullptr) *S = crossProduct2Vector(C, D, C, A) / denom;

    if(T !=nullptr) *T = crossProduct2Vector(A, B, C, A) / denom;

    return true;

}

/**

 *两条线是否平行

 */

boolVec2::IsLineParallel(constVec2& A,constVec2& B,

                           constVec2& C,constVec2& D) {

    if( (A.x==B.x && A.y==B.y) || (C.x==D.x && C.y==D.y) ) {

        return false;

    }

    if(crossProduct2Vector(A, B, C, D) ==0) {

        // line overlap

        if(crossProduct2Vector(C, D, C, A) ==0|| crossProduct2Vector(A, B, C, A) ==0) {

            returnfalse;

        }

        returntrue;

    }

    return false;

}

参考:

点乘和叉乘的区别

相关文章

网友评论

      本文标题:Vec2 (二维向量)

      本文链接:https://www.haomeiwen.com/subject/gtfiwltx.html