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. BKDRhash

     哈希: 字符串(数字同理): 例如有100000个字符串,现在要插入一些字符串,插入前比较是否已经存在避免含有重复数据 用暴力计较的话会比较慢,在某字符串插入时,最好的情况是在第一个位置就遇见该字符 ...

  2. iptables 负裁平衡(Load balancing)

    「负戴平衡」的作用是将连線平均分散给一组伺服器,以充分利用资源.最简单的作法是利用「通讯端口转接」技术,使其以循环顺序选择目的地位址. 设定iptables的组态 各家Linux系统的iptables ...

  3. 使用colab平台进行训练

    https://www.zhongxiaoping.cn/2018/12/01/%E4%BD%BF%E7%94%A8colab%E5%B9%B3%E5%8F%B0%E8%BF%9B%E8%A1%8C% ...

  4. TortoiseSVN各种状态

    黄色感叹号(有冲突): --这是有冲突了,冲突就是说你对某个文件进行了修改,别人也对这个文件进行了修改,别人抢在你提交之前先提交了,这时你再提交就会被提示发生冲突,而不允许你提交,防止你的提交覆盖了别 ...

  5. Lua环境搭建之使用EditPlus搭建Lua开发环境

    luatools正则表达式extension工具encoding 一.语法高亮 打开 EditPlus,Tools 工具-->Preferences首选项==>setting & ...

  6. yii框架不输出头文件和尾文件

    控制器: public function actionCat(){ return $this->renderPartial('cat');} 在进行页面输出渲染的时候. 1.render 输出父 ...

  7. 多线程:“对象当前正在其他地方使用”如何解决 system.drawing

    使用这个委托,在拥有此控件的基础窗口句柄的线程上执行指定委托 this.Invoke(new Action(() => { node.SetValues(values); }));

  8. [转]Jquery属性选择器(同时匹配多个条件,与或非)(附样例)

    1. 前言 为了处理除了两项不符合条件外的选择,需要用到jquery选择器的多个条件匹配来处理,然后整理了一下相关的与或非的条件及其组合. 作为笔记记录. 2. 代码 1 2 3 4 5 6 7 8 ...

  9. Vue 循环为选中的li列表添加效果

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

  10. P1004 奶牛与牧场

    题目描述 有一个牧场,牧场上的牧草每天都在匀速生长,这片牧场可供 \(a\) 头牛吃 \(b\) 天,或可供 \(c\) 头牛吃 \(d\) 天,那么,这片牧场每天新生的草量最多可供几头牛吃1天? 输 ...