//SE是坐标重叠部分
// returns true if segment A-B intersects with segment C-D. S->E is the overlap part
bool isOneDimensionSegmentOverlap(float A, float B, float C, float D, float *S, float * E)
{
//线段的最长点和最短点的单坐标 x 或 y
float ABmin = std::min(A, B);
float ABmax = std::max(A, B);
float CDmin = std::min(C, D);
float CDmax = std::max(C, D); //不可能相交的情况
if (ABmax < CDmin || CDmax < ABmin)
{
// ABmin->ABmax->CDmin->CDmax or CDmin->CDmax->ABmin->ABmax
return false;
}
else
{
//SE表示的是AB向量在CD向量上的投影或者是CD向量在AB向量上的投影
//单个坐标的排序一共四种情况
if (ABmin >= CDmin && ABmin <= CDmax)
{
// CDmin->ABmin->CDmax->ABmax or CDmin->ABmin->ABmax->CDmax
if (S != nullptr) *S = ABmin;
if (E != nullptr) *E = CDmax < ABmax ? CDmax : ABmax;
}
else if (ABmax >= CDmin && ABmax <= CDmax)
{
// ABmin->CDmin->ABmax->CDmax
if (S != nullptr) *S = CDmin;
if (E != nullptr) *E = ABmax;
}
else
{
// ABmin->CDmin->CDmax->ABmax
if (S != nullptr) *S = CDmin;
if (E != nullptr) *E = CDmax;
}
return true;
}
} //向量的外积 x1y2-x2y1
// cross product of 2 vector. A->B X C->D
float crossProduct2Vector(const Vec2& A, const Vec2& B, const Vec2& C, const Vec2& D)
{
return (D.y - C.y) * (B.x - A.x) - (D.x - C.x) * (B.y - A.y);
} //返回两个向量的夹角
float Vec2::angle(const Vec2& v1, const Vec2& v2)
{
float dz = v1.x * v2.y - v1.y * v2.x;
return atan2f(fabsf(dz) + MATH_FLOAT_SMALL, dot(v1, v2));
} //定义了向量的加法
void Vec2::add(const Vec2& v1, const Vec2& v2, Vec2* dst)
{
GP_ASSERT(dst); dst->x = v1.x + v2.x;
dst->y = v1.y + v2.y;
} //修改this为重叠部分的x y值 短板效应
void Vec2::clamp(const Vec2& min, const Vec2& max)
{
GP_ASSERT(!(min.x > max.x || min.y > max.y )); // Clamp the x value.
if (x < min.x)
x = min.x;
if (x > max.x)
x = max.x; // Clamp the y value.
if (y < min.y)
y = min.y;
if (y > max.y)
y = max.y;
} //与上面的方法只是调用方式不一样
void Vec2::clamp(const Vec2& v, const Vec2& min, const Vec2& max, Vec2* dst)
{
GP_ASSERT(dst);
GP_ASSERT(!(min.x > max.x || min.y > max.y )); // Clamp the x value.
dst->x = v.x;
if (dst->x < min.x)
dst->x = min.x;
if (dst->x > max.x)
dst->x = max.x; // Clamp the y value.
dst->y = v.y;
if (dst->y < min.y)
dst->y = min.y;
if (dst->y > max.y)
dst->y = max.y;
} //计算this向量和参数向量的距离
float Vec2::distance(const Vec2& v) const
{
float dx = v.x - x;
float dy = v.y - y; return std::sqrt(dx * dx + dy * dy);
} //计算v1v2的点积
float Vec2::dot(const Vec2& v1, const Vec2& v2)
{
return (v1.x * v2.x + v1.y * v2.y);
} //计算this向量的长
float Vec2::length() const
{
return std::sqrt(x * x + y * y);
} //将向量标准化,使得sqrt(x*x + y*y) == 1
void Vec2::normalize()
{
//先判断是否已经是标准化的了
float n = x * x + y * y;
// Already normalized.
if (n == 1.0f)
return; n = std::sqrt(n);
// Too close to zero.
if (n < MATH_TOLERANCE)
return; n = 1.0f / n;
x *= n;
y *= n;
} //获取标准化向量,不是标准化就转换
Vec2 Vec2::getNormalized() const
{
Vec2 v(*this);
v.normalize();
return v;
} //以指定的点point旋转angle角(以线段的锚点为中心旋转)
void Vec2::rotate(const Vec2& point, float angle)
{
float sinAngle = std::sin(angle);
float cosAngle = std::cos(angle);
//如果旋转点为(0,0)点
if (point.isZero())
{
float tempX = x * cosAngle - y * sinAngle;
y = y * cosAngle + x * sinAngle;
x = tempX;
}
//旋转点不在原点
else
{
//先移动到原点
float tempX = x - point.x;
float tempY = y - point.y; //旋转完再移动回去
x = tempX * cosAngle - tempY * sinAngle + point.x;
y = tempY * cosAngle + tempX * sinAngle + point.y;
}
} //使用数组初始化xy坐标
void Vec2::set(const float* array)
{
GP_ASSERT(array); x = array[0];
y = array[1];
} //a-b=c 得到的向量是从b的末尾指向a的末尾
void Vec2::subtract(const Vec2& v1, const Vec2& v2, Vec2* dst)
{
GP_ASSERT(dst); dst->x = v1.x - v2.x;
dst->y = v1.y - v2.y;
} //判断目标向量和this向量是否相等
bool Vec2::equals(const Vec2& target) const
{
return (std::abs(this->x - target.x) < FLT_EPSILON)
&& (std::abs(this->y - target.y) < FLT_EPSILON);
} //this向量与目标向量在一定范围内近似相等
bool Vec2::fuzzyEquals(const Vec2& b, float var) const
{
//判断xy值±var能不能包含b
if(x - var <= b.x && b.x <= x + var)
if(y - var <= b.y && b.y <= y + var)
return true;
return false;
} //获取两个向量的夹角
float Vec2::getAngle(const Vec2& other) const
{
Vec2 a2 = getNormalized();
Vec2 b2 = other.getNormalized();
float angle = atan2f(a2.cross(b2), a2.dot(b2));
if (std::abs(angle) < FLT_EPSILON) return 0.f;
return angle;
} //通过角度旋转,传递的参数是旋转的点和角度
Vec2 Vec2::rotateByAngle(const Vec2& pivot, float angle) const
{
//相当于按照原点旋转然后加上自定义旋转点的坐标
return pivot + (*this - pivot).rotate(Vec2::forAngle(angle));
} //检测直线是否相交
bool Vec2::isLineIntersect(const Vec2& A, const Vec2& B,
const Vec2& C, const Vec2& D,
float *S, float *T)
{
// FAIL: Line undefined
if ( (A.x==B.x && A.y==B.y) || (C.x==D.x && C.y==D.y) )
{
return false;
} //外积的其中一个意义是两个向量组成的平行四边形的面积
const float denom = 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;
} //判断是否平行
bool Vec2::isLineParallel(const Vec2& A, const Vec2& B,
const Vec2& C, const Vec2& D)
{
// FAIL: Line undefined
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)
{
return false;
} return true;
} return false;
} //判断是否重叠
bool Vec2::isLineOverlap(const Vec2& A, const Vec2& B,
const Vec2& C, const Vec2& D)
{
// FAIL: Line undefined //零向量
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 &&
(crossProduct2Vector(C, D, C, A) == 0 || crossProduct2Vector(A, B, C, A) == 0))
{
return true;
} return false;
} //判断是否部分重叠
bool Vec2::isSegmentOverlap(const Vec2& A, const Vec2& B, const Vec2& C, const Vec2& D, Vec2* S, Vec2* E)
{ if (isLineOverlap(A, B, C, D))
{
//判断是否相交
return isOneDimensionSegmentOverlap(A.x, B.x, C.x, D.x, &S->x, &E->x) &&
isOneDimensionSegmentOverlap(A.y, B.y, C.y, D.y, &S->y, &E->y);
} return false;
} //判断线段相交
bool Vec2::isSegmentIntersect(const Vec2& A, const Vec2& B, const Vec2& C, const Vec2& D)
{
float S, T; if (isLineIntersect(A, B, C, D, &S, &T )&&
(S >= 0.0f && S <= 1.0f && T >= 0.0f && T <= 1.0f))
{
return true;
} return false;
} //获取交点
Vec2 Vec2::getIntersectPoint(const Vec2& A, const Vec2& B, const Vec2& C, const Vec2& D)
{
float S, T; if (isLineIntersect(A, B, C, D, &S, &T))
{
// Vec2 of intersection
//获取交点的坐标
Vec2 P;
P.x = A.x + S * (B.x - A.x);
P.y = A.y + S * (B.y - A.y);
return P;
} return Vec2::ZERO;
}

cocos2dx Vec2的更多相关文章

  1. 【转】Cocos2d-x 3.x基础学习: 总结数学类Vec2/Size/Rect

    转载:http://www.taikr.com/article/1847 在Cocos2d-x 3.x中,数学类Vec2.Size.Rect,是比较常用的类.比如设置图片位置,图片大小,两图片的碰撞检 ...

  2. cocos2dx[3.2](8) 数学类Vec2/Size/Rect

    数学类Vec2.Size.Rect,是cocos2dx中比较常用的类. 比如设置图片位置,设置图片大小,两图片的碰撞检测等等. 比起2.x版本,在3.x中本质上其实没有太大的变化,主要的变化就是将全局 ...

  3. cocos2dx骨骼动画Armature源码分析(三)

    代码目录结构 cocos2dx里骨骼动画代码在cocos -> editor-support -> cocostudio文件夹中,win下通过筛选器,文件结构如下.(mac下没有分,是整个 ...

  4. cocos2d-x test学习[1]

    controller.cpp std::function<TestScene*()> callback;//一个是返回值,一个是参数.返回值是TestScene*,参数是()里的东西 Co ...

  5. cocos2d-x 3.0 事件分发机制

    在cocos2d-x 3.0中一共有五个事件监听器: 触摸事件(EventListenerTouch) 键盘响应事件 (EventListenerKeyboard) 加速器记录事件(EventList ...

  6. cocos2dx 3.2 Touch Listen和menu回调实现截屏

    在Cocos2d-X 3.x里面,已经集成了截屏功能,单独放在utils命名空间里,实现在base/ccUtils.h文件里面.看下函数申明 /** Capture the entire screen ...

  7. cocos2dx day 3 - Chapter5 Scene

    写在前面 越来越懒了,才3天,主要是cocos2dx官网的文章写的还是不是太完美,发现一段代码有个笔误,还有好几处写得不是很清楚的,所以有点泄气,不想继续读下去,不过为了我的第一款手游,一切困难都要先 ...

  8. cocos2dx day 1

    原文:http://www.cocos2d-x.org/programmersguide/2/index.html 一.Basic Concepts 1.director 2.scene 2.1 sc ...

  9. Cocos2d-x 3.2 学习笔记(十二)TimberMan!疯狂伐木工!

    学习cocos2dx有一段时间了,试着做了2048游戏,最近又发现个经典游戏,啥也不说果断开工做自己的游戏——TimberMan! 首先说明:游戏资源摘自同类游戏,感谢这些游戏的资源让我完成自己的开发 ...

随机推荐

  1. Git 进阶:10大技巧让你迅速提升

    1.Git自动补全 假使你使用命令行工具运行Git命令,那么每次手动输入各种命令是一件很令人厌烦的事情. 命令: cd ~ curl https://raw.github.com/git/git/ma ...

  2. Strange Way to Express Integers

    I. Strange Way to Express Integers 题目描述 原题来自:POJ 2891 给定 2n2n2n 个正整数 a1,a2,⋯,ana_1,a_2,\cdots ,a_na​ ...

  3. H3C 常用信息查看命令

  4. oracle使用DECODE函数来减少处理时间

    使用DECODE函数可以避免重复扫描相同记录或重复连接相同的表. 例如: SELECT COUNT(*),SUM(SAL) FROM EMP WHERE DEPT_NO = 0020 AND ENAM ...

  5. jq获取单选框、复选框、下拉菜单的值

    1.<input type="radio" name="testradio" value="jquery获取radio的值" /> ...

  6. css3 word-wrap属性

    允许长单词换行到下一行: word-wrap:break-word

  7. jq实现简单手风琴效果

    文章地址:https://www.cnblogs.com/sandraryan/ 利用slideUp slideDown动画 <!DOCTYPE html> <html lang=& ...

  8. 【t001】不高兴的津津

    Time Limit: 1 second Memory Limit: 50 MB [问题描述] 津津上初中了.妈妈认为津津应该更加用功学习,所以津津除了上学之外,还要参加妈妈为她报名的各科复习班.另外 ...

  9. [转]FileDescriptor详解

    FileDescriptor是"文件描述符". FileDescriptor可以被用来表示开放的文件,开放的套接字等. 当FileDescriptor表示文件来说,当FIleDes ...

  10. CSS画矩形、圆、半圆、弧形、半圆、小三角、疑问框

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...