Size

代码都是基础代码不注释了,写一些特别的

1、赋值时可以接收Size和Vec2类型的值,保证的类型的兼容性

2、对运算符进行了重载,可以按照正常的数学逻辑运算

3.、可以使用equals对比大小

Rect

1、Rect(x, y, width, height) 四个参数分别表示起点的 xy坐标 和 宽高

2、可以直接接受Vec2和Size作为参数,如 Rect(Vec2,Size)

//是否包含在矩形内
bool Rect::containsPoint(const Vec2& point) const
{
return (point.x >= getMinX() &&
point.x <= getMaxX() &&
point.y >= getMinY() &&
point.y <= getMaxY());
} //判断是否相交
bool Rect::intersectsRect(const Rect& rect) const
{
return !( getMaxX() < rect.getMinX() ||
rect.getMaxX() < getMinX() ||
getMaxY() < rect.getMinY() ||
rect.getMaxY() < getMinY());
} //与圆是否相交
bool Rect::intersectsCircle(const Vec2& center, float radius) const
{
//矩形中心
Vec2 rectangleCenter((origin.x + size.width / 2),
(origin.y + size.height / 2)); //矩形宽高的一半
float w = size.width / 2;
float h = size.height / 2; //圆心和矩形中心的距离
float dx = std::abs(center.x - rectangleCenter.x);
float dy = std::abs(center.y - rectangleCenter.y); //保证圆和矩形相交
//短板效应
//如果两个中点距离大于圆半径+宽或高的一半就返回false 因为如果这样圆和矩形不会相交 ?
if (dx > (radius + w) || dy > (radius + h))
{
return false;
} //获取两心的最小距离
Vec2 circleDistance(std::abs(center.x - origin.x - w),
std::abs(center.y - origin.y - h)); //圆在矩形内
if (circleDistance.x <= (w))
{
return true;
} if (circleDistance.y <= (h))
{
return true;
} //矩形在圆内
float cornerDistanceSq = powf(circleDistance.x - w, 2) + powf(circleDistance.y - h, 2); return (cornerDistanceSq <= (powf(radius, 2)));
} //找到可以容纳两种图形的最小矩形,并将当前rect设置为计算得到的矩形
void Rect::merge(const Rect& rect)
{
float minX = std::min(getMinX(), rect.getMinX());
float minY = std::min(getMinY(), rect.getMinY());
float maxX = std::max(getMaxX(), rect.getMaxX());
float maxY = std::max(getMaxY(), rect.getMaxY());
setRect(minX, minY, maxX - minX, maxY - minY);
} //计算可以容纳两个矩形的最小矩形并返回
Rect Rect::unionWithRect(const Rect & rect) const
{
//获取四个顶点坐标
float thisLeftX = origin.x;
float thisRightX = origin.x + size.width;
float thisTopY = origin.y + size.height;
float thisBottomY = origin.y; //交换大小
if (thisRightX < thisLeftX)
{
std::swap(thisRightX, thisLeftX); // This rect has negative width
} if (thisTopY < thisBottomY)
{
std::swap(thisTopY, thisBottomY); // This rect has negative height
} //获取目标四个顶点坐标
float otherLeftX = rect.origin.x;
float otherRightX = rect.origin.x + rect.size.width;
float otherTopY = rect.origin.y + rect.size.height;
float otherBottomY = rect.origin.y; //交换大小
if (otherRightX < otherLeftX)
{
std::swap(otherRightX, otherLeftX); // Other rect has negative width
} if (otherTopY < otherBottomY)
{
std::swap(otherTopY, otherBottomY); // Other rect has negative height
} //找到可以容纳两个矩形的最小矩形,并返回这个最小矩形
float combinedLeftX = std::min(thisLeftX, otherLeftX);
float combinedRightX = std::max(thisRightX, otherRightX);
float combinedTopY = std::max(thisTopY, otherTopY);
float combinedBottomY = std::min(thisBottomY, otherBottomY); return Rect(combinedLeftX, combinedBottomY, combinedRightX - combinedLeftX, combinedTopY - combinedBottomY);
} const Rect Rect::ZERO = Rect(0, 0, 0, 0); NS_CC_END

主要用来判断是否相交、包含,可能会用在碰撞检测中。

cocos2dx Geometry Size和Rect的更多相关文章

  1. OpenCV——创建Mat对象、格式化输出、常用数据结构和函数(point,vector、Scalar、Size、Rect、cvtColor)

    创建Mat对象:

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

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

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

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

  4. debug输出rect,size和point的宏

    #define NSLogRect(rect) NSLog(@"%s x:%.4f, y:%.4f, w:%.4f, h:%.4f", #rect, rect.origin.x,  ...

  5. cocos2d-x 纹理源码分析

    转自:http://blog.csdn.net/honghaier/article/details/8068895 当一张图片被加载到内存后,它是以纹理的形式存在的.纹理是什么东西呢?纹理就是一块内存 ...

  6. 使用cocos2d-x制作 Texture unpacker

    使用cocos2d-x制作 Texture unpacker 没错,就是unpacker. 在大多数游戏包里面,可以找到很多纹理图集,他们基本上是用texture packer制作的,有plist文件 ...

  7. Cocos2dx 3.0 交流篇

    创建项目: For(MAC) Runtime Requirements Android 2.3 or newer iOS 5.0 or newer OS X 10.7 or newer Windows ...

  8. cocos2dx - 伤害实现

    接上一节内容:cocos2dx - 生成怪物及AI 本节主要讲如何通过创建简单的矩形区域来造成伤害 在小游戏中简单的碰撞需求应用box2d等引擎会显得过于臃肿复杂,且功能不是根据需求定制,还要封装,为 ...

  9. cocos2d-x游戏引擎核心(3.x)----事件分发机制之事件从(android,ios,desktop)系统传到cocos2dx的过程浅析

    (一) Android平台下: cocos2dx 版本3.2,先导入一个android工程,然后看下AndroidManifest.xml <application android:label= ...

随机推荐

  1. jq 监听返回事件

    <script> $(document).ready(function(e) {             var counter = 0;            if (window.hi ...

  2. oracle函数 SYS_CONTEXT(c1,c2)

    [功能]返回系统c1对应的c2的值.可以使用在SQL/PLSQL中,但不可以用在并行查询或者RAC环境中 [参数] c1,'USERENV' c2,参数表,详见示例 [返回]字符串 [示例] sele ...

  3. epoll与fork

    使用epoll时,如果在调用epoll_create之后,调用了fork创建子进程,那么父子进程虽然有各自epoll实例的副本,但是在内核中,它们引用的是同一个实例.子进程向自己的epoll实例添加. ...

  4. etcd 在超大规模数据场景下的性能优化

    作者 | 阿里云智能事业部高级开发工程师 陈星宇(宇慕) 概述 etcd是一个开源的分布式的kv存储系统, 最近刚被cncf列为沙箱孵化项目.etcd的应用场景很广,很多地方都用到了它,例如kuber ...

  5. uva 11916 Emoogle Grid (BSGS)

    UVA 11916 BSGS的一道简单题,不过中间卡了一下没有及时取模,其他这里的100000007是素数,所以不用加上拓展就能做了. 代码如下: #include <cstdio> #i ...

  6. 2018-9-1-win10-uwp-轻量级-MVVM-框架入门-2.1.5.3199

    title author date CreateTime categories win10 uwp 轻量级 MVVM 框架入门 2.1.5.3199 lindexi 2018-09-01 16:24: ...

  7. "?:"在正则表达式中什么意思

    “?:”非获取匹配,匹配冒号后的内容但不获取匹配结果,不进行存储供以后使用. 单独的“?”:匹配前面的子表达式零次或一次. 当“?”紧跟在任何一个其他限制符(*,+,?,{n},{n,},{n,m}) ...

  8. 使C# WebApi返回Json

    找到Global.asax文件,在Application_Start()方法中添加一句: protected void Application_Start() { AreaRegistration.R ...

  9. HDU 2066最短路径Dijkstra、

    思路:枚举所有起点城市然后比较每个起点所去喜欢城市的最小距离 #include<cstdio> #include<cmath> #include<cstring> ...

  10. Adam那么棒,为什么还对SGD念念不忘 (3)—— 优化算法的选择与使用策略

    在前面两篇文章中,我们用一个框架梳理了各大优化算法,并且指出了以Adam为代表的自适应学习率优化算法可能存在的问题.那么,在实践中我们应该如何选择呢? 本文介绍Adam+SGD的组合策略,以及一些比较 ...