cocos2dx Geometry Size和Rect
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的更多相关文章
- OpenCV——创建Mat对象、格式化输出、常用数据结构和函数(point,vector、Scalar、Size、Rect、cvtColor)
创建Mat对象:
- 【转】Cocos2d-x 3.x基础学习: 总结数学类Vec2/Size/Rect
转载:http://www.taikr.com/article/1847 在Cocos2d-x 3.x中,数学类Vec2.Size.Rect,是比较常用的类.比如设置图片位置,图片大小,两图片的碰撞检 ...
- cocos2dx[3.2](8) 数学类Vec2/Size/Rect
数学类Vec2.Size.Rect,是cocos2dx中比较常用的类. 比如设置图片位置,设置图片大小,两图片的碰撞检测等等. 比起2.x版本,在3.x中本质上其实没有太大的变化,主要的变化就是将全局 ...
- debug输出rect,size和point的宏
#define NSLogRect(rect) NSLog(@"%s x:%.4f, y:%.4f, w:%.4f, h:%.4f", #rect, rect.origin.x, ...
- cocos2d-x 纹理源码分析
转自:http://blog.csdn.net/honghaier/article/details/8068895 当一张图片被加载到内存后,它是以纹理的形式存在的.纹理是什么东西呢?纹理就是一块内存 ...
- 使用cocos2d-x制作 Texture unpacker
使用cocos2d-x制作 Texture unpacker 没错,就是unpacker. 在大多数游戏包里面,可以找到很多纹理图集,他们基本上是用texture packer制作的,有plist文件 ...
- Cocos2dx 3.0 交流篇
创建项目: For(MAC) Runtime Requirements Android 2.3 or newer iOS 5.0 or newer OS X 10.7 or newer Windows ...
- cocos2dx - 伤害实现
接上一节内容:cocos2dx - 生成怪物及AI 本节主要讲如何通过创建简单的矩形区域来造成伤害 在小游戏中简单的碰撞需求应用box2d等引擎会显得过于臃肿复杂,且功能不是根据需求定制,还要封装,为 ...
- cocos2d-x游戏引擎核心(3.x)----事件分发机制之事件从(android,ios,desktop)系统传到cocos2dx的过程浅析
(一) Android平台下: cocos2dx 版本3.2,先导入一个android工程,然后看下AndroidManifest.xml <application android:label= ...
随机推荐
- Nacos Committer 张龙:Nacos Sync 的设计原理和规划
图:Nacos Meetup @杭州 与你同行,抬头便是星空. 本文整理自Nacos Committer 张龙的现场分享,阿里巴巴中间件受权发布. 随着 Nacos 1.0.0 稳定版的发布,越来越多 ...
- Libev源码分析08:Libev中的内存扩容方法
在Libev中,如果某种结构的数组需要扩容,它使用array_needsize宏进行处理,比如: array_needsize (int, fdchanges, fdchangemax, fdchan ...
- 3、.net core 部署到IIS
1)下载对应的Hosting Bundle https://dotnet.microsoft.com/download/dotnet-core/2.2 2)VS发布项目,选择window平台环境 3 ...
- @noi.ac - 493@ trade
目录 @description@ @solution@ @part - 1@ @part - 2@ @part - 3@ @part - 4@ @accepted code@ @details@ @d ...
- css 文字超出部分隐藏
未做隐藏处理 执行结果: 1.1行超出部分省略号 效果: 2.多行超出部分隐藏(目前只能在chrome浏览器中使用,其他浏览器不兼容) 效果: -webkit-line-clamp 属性定义显示行数可 ...
- phpstorm 里能做git的命令行操作吗?
在VCS菜单下面有 GIT -> Branches 然后会弹出branch菜单,后面怎么操作应该不需要解释吧,所有的branch都列出来自己选 在Tools菜单下面有Open Terminal. ...
- H3C NAPT
- 2018-8-10-如何入门-C++-AMP-教程
title author date CreateTime categories 如何入门 C++ AMP 教程 lindexi 2018-08-10 19:16:51 +0800 2018-2-13 ...
- vue v-for循环中key属性的使用
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 如何让in/exists 子查询(半连接)作为驱动表?
一哥们问我,怎么才能让子查询作为驱动表? SQL如下: select rowid rid from its_car_pass7 v where 1 = 1 and pass_datetime > ...