3template<
class Type, 
int Dim>
 
    9class APICALL COMMS_ALIASING 
cVec2 {
 
   15    explicit cVec2(
const float S);
 
   16    cVec2(
const float X, 
const float Y);
 
   19    void Copy(
const float *Src);
 
   22    void Set(
const float S);
 
   23    void Set(
const float X, 
const float Y);
 
   25    float operator [] (
const int index) 
const;
 
   26    float & operator [] (
const int index);
 
   28    const cVec2 operator - () 
const;
 
   33    cVec2& operator *= (
const float);
 
   35    cVec2& operator /= (
const float);
 
   37    void Transform(
const cMat3 &); 
 
   38    void operator *= (
const cMat3 &);
 
   40    void TransformCoordinate(
const cMat4 &); 
 
   41    void TransformNormal(
const cMat4 &); 
 
   46    const cVec2 operator * (
const float) 
const;
 
   47    friend const cVec2 operator * (
const float, 
const cVec2 &);
 
   49    const cVec2 operator / (
const float) 
const;
 
   50    friend const cVec2 operator / (
const float, 
const cVec2 &);
 
   59    bool operator == (
const cVec2 &) 
const;
 
   60    bool operator != (
const cVec2 &) 
const;
 
   61    static bool Equals(
const cVec2 &, 
const cVec2 &, 
const float Eps = cMath::Epsilon);
 
   64    float LengthSq() 
const;
 
   66    float NormalizeSafe(
const cVec2 &Fallback);
 
   68    bool IsNormalized(
const float Eps = cMath::Epsilon) 
const;
 
   69    bool IsZero(
const float Eps = cMath::Epsilon) 
const;
 
   72    const cVec2 ToRound() 
const;
 
   77    static float Angle(
const cVec2&, 
const cVec2&);
 
   78    static float AreaSigned(
const cVec2 &t0, 
const cVec2 &t1, 
const cVec2 &t2);
 
   79    static float Ccw(
const cVec2 &, 
const cVec2 &);
 
   81    static float Distance(
const cVec2 &, 
const cVec2 &);
 
   82    static float DistanceSq(
const cVec2 &, 
const cVec2 &);
 
   83    static float Dot(
const cVec2 &, 
const cVec2 &);
 
   84    static float Length(
const cVec2 &);
 
   85    static float LengthSq(
const cVec2 &);
 
   91    static const cVec2 NormalizeSafe(
const cVec2 &Src, 
const cVec2 &Fallback);
 
   95    static const cVec2 Refract(
const cVec2 &RayDir, 
const cVec2 &Normal, 
const float Eta);
 
   96    static const cVec2 Truncate(
const cVec2 &u, 
const float MaxLength);
 
   97    void Truncate(
const float MaxLength);
 
   98    static const cVec2 RandRange1();
 
   99    static const cVec2 RandNormal();
 
  101    __inline 
float distance2(
const cVec2&);
 
  102    __inline 
float distance(
const cVec2&);
 
  104    float DistanceToLineSegSq(
const cVec2 &A, 
const cVec2 &B, 
float *pScale = 
nullptr) 
const;
 
  105    static bool SegIntersection(
const cVec2 &L0, 
const cVec2 &L1, 
const cVec2 &R0, 
const cVec2 &R1, 
float *l = 
nullptr, 
float *r = 
nullptr);
 
  107    static const cVec2 FromBaryCentric(
const cVec2 &t0, 
const cVec2 &t1, 
const cVec2 &t2, 
const float u, 
const float v);
 
  108    static const cVec2 FromPolar(
const float Radius, 
const float Angle);
 
  110    std::tuple<float, float> __getstate__();
 
  111    void __setstate__(
const std::tuple<float, float>& state);
 
  112    const std::string __repr__() 
const;
 
  114    static const cVec2 Zero;
 
  115    static const cVec2 One;
 
  116    static const cVec2 Infinity;
 
  117    static const cVec2 AxisX;
 
  118    static const cVec2 AxisY;
 
  119    static const cVec2 AxisNegX;
 
  120    static const cVec2 AxisNegY;
 
  122    int GetDimension() 
const;
 
  124    const float * ToFloatPtr() 
const;
 
  125    float * ToFloatPtr();
 
  128    operator cVec<Type, 2> () 
const;
 
  130    void ToPolar(
float *Radius, 
float *Angle) 
const;
 
  131    float ToBaryCentric(
const cVec2 &t0, 
const cVec2 &t1, 
const cVec2 &t2, 
float &u, 
float &v) 
const;
 
  132    bool IsInsideTri(
const cVec2 &t0, 
const cVec2 &t1, 
const cVec2 &t2) 
const;
 
  134    const cStr ToString(
const int Prec = 2) 
const;
 
  137    const cVec2 ToNormal() 
const;
 
  139    const cVec2 ToPerpCw() 
const;
 
  140    const cVec2 ToPerpCcw() 
const;
 
  144    inline void Clear(
void* = 0) {
 
  147    inline void AddWithWeight(
cVec2 const& src, 
float weight) {
 
  151    inline void SetPosition(
float aX, 
float aY) {
 
  155    inline const float* GetPosition()
 const {
 
 
  164inline cVec2::cVec2() {
 
  168inline cVec2::cVec2(
const float S)
 
  172inline cVec2::cVec2(
const float X, 
const float Y)
 
  176inline void cVec2::Copy(
const float *Src) {
 
  177    cAssert(Src != 
nullptr);
 
  183inline void cVec2::SetZero() {
 
  188inline void cVec2::SetOne() {
 
  193inline void cVec2::Set(
const float S) {
 
  198inline void cVec2::Set(
const float X, 
const float Y) {
 
  204inline float cVec2::operator [] (
const int index)
 const {
 
  205    cAssert(index >= 0 && index < 2);
 
  210inline float & cVec2::operator [] (
const int index) {
 
  211    cAssert(index >= 0 && index < 2);
 
  216inline const cVec2 cVec2::operator - ()
 const {
 
  217    return cVec2(-x, -y);
 
  221inline cVec2& cVec2::operator += (
const cVec2 &u) {
 
  228inline cVec2& cVec2::operator -= (
const cVec2 &u) {
 
  235inline cVec2& cVec2::operator *= (
const cVec2 &u) {
 
  242inline cVec2& cVec2::operator *= (
const float s) {
 
  249inline cVec2& cVec2::operator /= (
const cVec2 &u) {
 
  256inline cVec2& cVec2::operator /= (
const float s) {
 
  264inline const cVec2 cVec2::operator + (
const cVec2 &u)
 const {
 
  265    return cVec2(x + u.x, y + u.y);
 
  269inline const cVec2 cVec2::operator - (
const cVec2 &u)
 const {
 
  270    return cVec2(x - u.x, y - u.y);
 
  274inline const cVec2 cVec2::operator * (
const cVec2 &u)
 const {
 
  275    return cVec2(x * u.x, y * u.y);
 
  279inline const cVec2 cVec2::operator * (
const float s)
 const {
 
  280    return cVec2(x * s, y * s);
 
  284inline const cVec2 operator * (
const float s, 
const cVec2 &u) {
 
  285    return cVec2(s * u.x, s * u.y);
 
  289inline const cVec2 cVec2::operator / (
const cVec2 &u)
 const {
 
  290    return cVec2(x / u.x, y / u.y);
 
  294inline const cVec2 cVec2::operator / (
const float s)
 const {
 
  296    return cVec2(x * is, y * is);
 
  300inline const cVec2 operator / (
const float s, 
const cVec2 &u) {
 
  301    return cVec2(s / u.x, s / u.y);
 
  305inline bool cVec2::operator == (
const cVec2 &u)
 const {
 
  306    return x == u.x && y == u.y;
 
  310inline bool cVec2::operator != (
const cVec2 &u)
 const {
 
  311    return x != u.x || y != u.y;
 
  315inline bool cVec2::Equals(
const cVec2 &u, 
const cVec2 &v, 
const float Eps) {
 
  316    if(cMath::Abs(u.x - v.x) > Eps) 
return false;
 
  317    if(cMath::Abs(u.y - v.y) > Eps) 
return false;
 
  322inline float cVec2::Length()
 const {
 
  323    return cMath::Sqrt(x * x + y * y);
 
  327inline float cVec2::LengthSq()
 const {
 
  328    return x * x + y * y;
 
  332inline float cVec2::Normalize() {
 
  333    const float l = Length();
 
  335        const float il = 1.0f / l;
 
  343inline float cVec2::NormalizeSafe(
const cVec2 &Fallback) {
 
  344    const float l = Length();
 
  346        const float il = 1.0f / l;
 
  349        cAssert(IsNormalized());
 
  353    cAssert(IsNormalized());
 
  358inline bool cVec2::IsValid()
 const {
 
  359    return cMath::IsValid(x) && cMath::IsValid(y);
 
  363inline bool cVec2::IsNormalized(
const float Eps)
 const {
 
  364    return cMath::IsOne(LengthSq(), Eps);
 
  368inline bool cVec2::IsZero(
const float Eps)
 const {
 
  369    if(!cMath::IsZero(x, Eps)) 
return false;
 
  370    if(!cMath::IsZero(y, Eps)) 
return false;
 
  375inline const cVec2 cVec2::Normalize(
const cVec2 &u) {
 
  382inline const cVec2 cVec2::NormalizeSafe(
const cVec2 &Src, 
const cVec2 &Fallback) {
 
  384    t.NormalizeSafe(Fallback);
 
  389inline const cVec2 cVec2::FromPolar(
const float Radius, 
const float Angle) {
 
  390    float A = cMath::Rad(Angle);
 
  391    return cVec2(Radius * cMath::Cos(A), Radius * cMath::Sin(A));
 
  394inline std::tuple<float, float> cVec2::__getstate__() {
 
  395    return std::make_tuple(x, y);
 
  398inline void cVec2::__setstate__(
const std::tuple<float, float>& state) {
 
  399    x = std::get<0>(state);
 
  400    y = std::get<1>(state);
 
  404inline const cVec2 cVec2::FromBaryCentric(
const cVec2 &t0, 
const cVec2 &t1, 
const cVec2 &t2, 
const float u, 
const float v) {
 
  405    return t0 + u * (t1 - t0) + v * (t2 - t0);
 
  409inline const cVec2 cVec2::Abs(
const cVec2& u) {
 
  410    return cVec2(cMath::Abs(u.x), cMath::Abs(u.y));
 
  414inline const cVec2 cVec2::Fract(
const cVec2& u) {
 
  415    return cVec2(u.x-floorf(u.x), u.y-floorf(u.y));
 
  419inline const cVec2 cVec2::Round(
const cVec2 &u) {
 
  420    return cVec2(cMath::Round(u.x), cMath::Round(u.y));
 
  424inline const cVec2 cVec2::ToRound()
 const {
 
  425    return cVec2(cMath::Round(x), cMath::Round(y));
 
  429inline void cVec2::Round() {
 
  435inline float cVec2::Angle(
const cVec2 &u, 
const cVec2 &v) {
 
  436    const float d = u.x * v.y - v.x * u.y;
 
  437    const float s = d != 0.0f ? cMath::Sign(d) : 1.0f;
 
  438    const float D = cVec2::Dot(u, v);
 
  439    const float ll = u.Length() * v.Length();
 
  440    return s * cMath::Deg(cMath::ACos(D / ll));
 
  444inline float cVec2::AreaSigned(
const cVec2 &t0, 
const cVec2 &t1, 
const cVec2 &t2) {
 
  445    const cVec2 e1 = t1 - t0;
 
  446    const cVec2 e2 = t2 - t0;
 
  447    return Ccw(e1, e2) * 0.5f;
 
  451inline float cVec2::ToBaryCentric(
const cVec2 &t0, 
const cVec2 &t1, 
const cVec2 &t2, 
float &u, 
float &v)
 const {
 
  452    const float A = AreaSigned(t0, t1, t2);
 
  454        const float iA = 1.0f / A;
 
  455        u = AreaSigned(t0, *
this, t2) * iA;
 
  456        v = AreaSigned(t0, t1, *
this) * iA;
 
  464inline bool cVec2::IsInsideTri(
const cVec2 &t0, 
const cVec2 &t1, 
const cVec2 &t2)
 const {
 
  466    ToBaryCentric(t0, t1, t2, u, v);
 
  467    return u >= 0.0f && v >= 0.0f && 1.0f - u - v >= 0.0f;
 
  471inline void cVec2::ToPolar(
float *Radius, 
float *Angle)
 const {
 
  472    float R = Length(), A = 0.0f;
 
  473    if(cMath::IsZero(R)) {
 
  476        A = cMath::ACos(x / R);
 
  480        A = cMath::Deg(cMath::AngleNormalizeTwoPi(A));
 
  482    if(Radius != 
nullptr) {
 
  485    if(Angle != 
nullptr) {
 
  491inline const cVec2 cVec2::Reflect(
const cVec2 &RayDir, 
const cVec2 &Normal) {
 
  492    return RayDir - 2.0f * Dot(RayDir, Normal) * Normal;
 
  496inline const cVec2 cVec2::Refract(
const cVec2 &RayDir, 
const cVec2 &Normal, 
const float Eta) {
 
  497    float k = 1.0f - Eta * Eta * (1.0f - cMath::Square(Dot(Normal, RayDir)));
 
  501    return Eta * RayDir - (Eta * Dot(Normal, RayDir) + cMath::Sqrt(k)) * Normal;
 
  505inline void cVec2::Truncate(
const float MaxLen) {
 
  512        float il = MaxLen / l;
 
  519inline const cVec2 cVec2::Truncate(
const cVec2 &u, 
const float MaxLen) {
 
  523    float l = u.Length();
 
  525        float il = MaxLen / l;
 
  526        return cVec2(u.x * il, u.y * il);
 
  532inline int cVec2::GetDimension()
 const {
 
  537inline const float * cVec2::ToFloatPtr()
 const {
 
  538    return (
const float *)&x;
 
  542inline float * cVec2::ToFloatPtr() {
 
  548cVec2::operator cVec<Type, 2> ()
 const {
 
  549    return cVec<Type, 2>( (Type)x, (Type)y );
 
  555    r.x = u.x > v.x ? u.x : v.x;
 
  556    r.y = u.y > v.y ? u.y : v.y;
 
  563    r.x = u.x < v.x ? u.x : v.x;
 
  564    r.y = u.y < v.y ? u.y : v.y;
 
  569inline float cVec2::Ccw(
const cVec2 &u, 
const cVec2 &v) {
 
  570    return u.x * v.y - u.y * v.x;
 
  576    r.x = Value.x < Lo.x ? Lo.x : (Value.x > Hi.x ? Hi.x : Value.x);
 
  577    r.y = Value.y < Lo.y ? Lo.y : (Value.y > Hi.y ? Hi.y : Value.y);
 
  582inline float cVec2::Distance(
const cVec2 &p0, 
const cVec2 &p1) {
 
  583    float dx = p0.x - p1.x;
 
  584    float dy = p0.y - p1.y;
 
  585    return cMath::Sqrt(dx * dx + dy * dy);
 
  587float cVec2::distance2(
const cVec2& pt) {
 
  588    return DistanceSq(*
this, pt);
 
  590float cVec2::distance(
const cVec2& pt) {
 
  591    return Distance(*
this, pt);
 
  594inline float cVec2::DistanceSq(
const cVec2 &p0, 
const cVec2 &p1) {
 
  595    float dx = p0.x - p1.x;
 
  596    float dy = p0.y - p1.y;
 
  597    return dx * dx + dy * dy;
 
  601inline float cVec2::Dot(
const cVec2 &u, 
const cVec2 &v) {
 
  602    return u.x * v.x + u.y * v.y;
 
  606inline float cVec2::Length(
const cVec2 &u) {
 
  607    return cMath::Sqrt(u.x * u.x + u.y * u.y);
 
  611inline float cVec2::LengthSq(
const cVec2 &u) {
 
  612    return u.x * u.x + u.y * u.y;
 
  616inline const cVec2 cVec2::Lerp(
const cVec2 &u, 
const cVec2 &v, 
const float s) {
 
  618    r.x = u.x + s * (v.x - u.x);
 
  619    r.y = u.y + s * (v.y - u.y);
 
  626    r.x = (u.x + v.x) * 0.5f;
 
  627    r.y = (u.y + v.y) * 0.5f;
 
  632inline const cVec2 cVec2::PerpCw(
const cVec2 &u) {
 
  633    return cVec2(u.y, -u.x);
 
  637inline const cVec2 cVec2::ToPerpCw()
 const {
 
  642inline const cVec2 cVec2::PerpCcw(
const cVec2 &u) {
 
  643    return cVec2(-u.y, u.x);
 
  647inline const cVec2 cVec2::ToPerpCcw()
 const {
 
  652inline const cVec2 cVec2::ToNormal()
 const {
 
  660    return cVec2(cMath::Rand(Lo.x, Hi.x), cMath::Rand(Lo.y, Hi.y));
 
The 3D-matrix, refer it as coat::mat3 in the Core API.
Definition cMat3.h:6
 
The 4D-matrix, refer it as coat::mat4 in the Core API.
Definition cMat4.h:6
 
The 2D-vector, refer it as coat::vec2 in the Core API.
Definition cVec2.h:9
 
The 4D-vector, refer it as coat::vec4 in the Core API.
Definition cVec4.h:9