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. @atcoder - AGC036E@ ABC String

    目录 @description@ @solution@ @accepted code@ @details@ @description@ 给定一个仅由 A, B, C 组成的字符串 S. 求 S 的一个 ...

  2. oracle函数 SUBSTRB(c1,n1[,n2])

    [功能]取子字符串 [说明]多字节符(汉字.全角符等),按2个字符计算 [参数]在字符表达式c1里,从n1开始取n2个字符;若不指定n2,则从第y个字符直到结束的字串. [返回]字符型,如果从多字符右 ...

  3. SDUT-2132_数据结构实验之栈与队列二:一般算术表达式转换成后缀式

    数据结构实验之栈与队列二:一般算术表达式转换成后缀式 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 对于一个基于二元运 ...

  4. linux下修改gcc编译器版本

    可以使用如下命令行来让 gcc 选择不同的 C++ 版本: g++ -std=c++11 main.cpp 在你的系统中,由于编译器或是编译器设定上的差别,操作也许有所不同.    

  5. 初识 Knative: 跨平台的 Serverless 编排框架

    Knative 是什么 Knative 是 Google 在 2018 的 Google Cloud Next 大会上发布的一款基于 Kubernetes 的 Serverless 框架.Knativ ...

  6. 深度学习的Xavier初始化方法

    在tensorflow中,有一个初始化函数:tf.contrib.layers.variance_scaling_initializer.Tensorflow 官网的介绍为: variance_sca ...

  7. hdu 3068 最长回文 (Manacher算法求最长回文串)

    参考博客:Manacher算法--O(n)回文子串算法 - xuanflyer - 博客频道 - CSDN.NET 从队友那里听来的一个算法,O(N)求得每个中心延伸的回文长度.这个算法好像比较偏门, ...

  8. 【codeforces 520A】Pangram

    [题目链接]:http://codeforces.com/problemset/problem/520/A [题意] 给你一个字符串. 统计里面有没有出现所有的英文字母->'a'..'z' 每个 ...

  9. H3C 高级ACL

  10. Django入门9--Django shell