cocos2dx 仿射变换
AffineTransform __CCAffineTransformMake(float a, float b, float c, float d, float tx, float ty)
{
AffineTransform t;
t.a = a; t.b = b; t.c = c; t.d = d; t.tx = tx; t.ty = ty;
return t;
}
Vec2 __CCPointApplyAffineTransform(const Vec2& point, const AffineTransform& t)
{
Vec2 p;
p.x = (float)((double)t.a * point.x + (double)t.c * point.y + t.tx);
p.y = (float)((double)t.b * point.x + (double)t.d * point.y + t.ty);
return p;
}
Vec2 PointApplyTransform(const Vec2& point, const Mat4& transform)
{
Vec3 vec(point.x, point.y, 0);
transform.transformPoint(&vec);
return Vec2(vec.x, vec.y);
}
Size __CCSizeApplyAffineTransform(const Size& size, const AffineTransform& t)
{
Size s;
s.width = (float)((double)t.a * size.width + (double)t.c * size.height);
s.height = (float)((double)t.b * size.width + (double)t.d * size.height);
return s;
}
AffineTransform AffineTransformMakeIdentity()
{
return __CCAffineTransformMake(1.0, 0.0, 0.0, 1.0, 0.0, 0.0);
}
extern const AffineTransform AffineTransformIdentity = AffineTransformMakeIdentity();
const AffineTransform AffineTransform::IDENTITY = AffineTransformMakeIdentity();
//获取仿射变换转换之后的坐标
Rect RectApplyAffineTransform(const Rect& rect, const AffineTransform& anAffineTransform)
{
//四个最值
float top = rect.getMinY(); //左上角Y
float left = rect.getMinX(); //左上角X
float right = rect.getMaxX(); //右下角X
float bottom = rect.getMaxY(); //右下角Y
//四个顶点坐标
Vec2 topLeft = PointApplyAffineTransform(Vec2(left, top), anAffineTransform); //左上角
Vec2 topRight = PointApplyAffineTransform(Vec2(right, top), anAffineTransform); //右上角
Vec2 bottomLeft = PointApplyAffineTransform(Vec2(left, bottom), anAffineTransform); //左下角
Vec2 bottomRight = PointApplyAffineTransform(Vec2(right, bottom), anAffineTransform); //右下角
//找到可以承载这个矩形的最大正矩形坐标
float minX = min(min(topLeft.x, topRight.x), min(bottomLeft.x, bottomRight.x));
float maxX = max(max(topLeft.x, topRight.x), max(bottomLeft.x, bottomRight.x));
float minY = min(min(topLeft.y, topRight.y), min(bottomLeft.y, bottomRight.y));
float maxY = max(max(topLeft.y, topRight.y), max(bottomLeft.y, bottomRight.y));
//返回可容纳最大矩形的Rect
return Rect(minX, minY, (maxX - minX), (maxY - minY));
}
//直接获取坐标
Rect RectApplyTransform(const Rect& rect, const Mat4& transform)
{
//四个最值
float top = rect.getMinY();
float left = rect.getMinX();
float right = rect.getMaxX();
float bottom = rect.getMaxY();
//四个顶点坐标
Vec3 topLeft(left, top, 0);
Vec3 topRight(right, top, 0);
Vec3 bottomLeft(left, bottom, 0);
Vec3 bottomRight(right, bottom, 0);
transform.transformPoint(&topLeft);
transform.transformPoint(&topRight);
transform.transformPoint(&bottomLeft);
transform.transformPoint(&bottomRight);
//获取四个最值点
float minX = min(min(topLeft.x, topRight.x), min(bottomLeft.x, bottomRight.x));
float maxX = max(max(topLeft.x, topRight.x), max(bottomLeft.x, bottomRight.x));
float minY = min(min(topLeft.y, topRight.y), min(bottomLeft.y, bottomRight.y));
float maxY = max(max(topLeft.y, topRight.y), max(bottomLeft.y, bottomRight.y));
//返会可容纳的最大矩形
return Rect(minX, minY, (maxX - minX), (maxY - minY));
}
// [ 1 0 0 ]
//[x y 1]* [ 0 1 0 ]
// [ tx ty 1 ]
//平移到(x+tx, y+ty, 1)
AffineTransform AffineTransformTranslate(const AffineTransform& t, float tx, float ty)
{
return __CCAffineTransformMake(t.a, t.b, t.c, t.d, t.tx + t.a * tx + t.c * ty, t.ty + t.b * tx + t.d * ty);
}
// [ a*sx 0 0 ]
//[x y 1]* [ 0 d*sy 0 ]
// [ 0 0 1 ]
//x坐标缩放sx倍,y坐标缩放sy倍
AffineTransform AffineTransformScale(const AffineTransform& t, float sx, float sy)
{
return __CCAffineTransformMake(t.a * sx, t.b * sx, t.c * sy, t.d * sy, t.tx, t.ty);
}
// [ cosa sina 0 ]
//[x y 1]* [ -sina cosa 0 ]
// [ 0 0 1 ]
//旋转
AffineTransform AffineTransformRotate(const AffineTransform& t, float anAngle)
{
float sine = sinf(anAngle);
float cosine = cosf(anAngle);
return __CCAffineTransformMake( t.a * cosine + t.c * sine,
t.b * cosine + t.d * sine,
t.c * cosine - t.a * sine,
t.d * cosine - t.b * sine,
t.tx,
t.ty);
}
//将两个矩阵相乘 得到第二个矩阵的xy轴在第一个矩阵xy轴上的投影
//下面的参数化简完就变成了
//[ a1*a2 0 0 ]
//[ 0 d1*d2 0 ]
//[ 0 0 0 ]
/* Concatenate `t2' to `t1' and return the result:
t' = t1 * t2 */
AffineTransform AffineTransformConcat(const AffineTransform& t1, const AffineTransform& t2)
{
return __CCAffineTransformMake( t1.a * t2.a + t1.b * t2.c, t1.a * t2.b + t1.b * t2.d, //a,b
t1.c * t2.a + t1.d * t2.c, t1.c * t2.b + t1.d * t2.d, //c,d
t1.tx * t2.a + t1.ty * t2.c + t2.tx, //tx
t1.tx * t2.b + t1.ty * t2.d + t2.ty); //ty
}
Mat4 TransformConcat(const Mat4& t1, const Mat4& t2)
{
return t1 * t2;
}
//
/* Return true if `t1' and `t2' are equal, false otherwise. */
bool AffineTransformEqualToTransform(const AffineTransform& t1, const AffineTransform& t2)
{
return (t1.a == t2.a && t1.b == t2.b && t1.c == t2.c && t1.d == t2.d && t1.tx == t2.tx && t1.ty == t2.ty);
}
//交换xy轴的值
//转换为
//[ d 0 0 ]
//[ 0 a 0 ]
//[ 0 0 0 ]
AffineTransform AffineTransformInvert(const AffineTransform& t)
{
float determinant = 1 / (t.a * t.d - t.b * t.c);
return __CCAffineTransformMake(determinant * t.d, -determinant * t.b, -determinant * t.c, determinant * t.a,
determinant * (t.c * t.ty - t.d * t.tx), determinant * (t.b * t.tx - t.a * t.ty) );
}
NS_CC_END
如果不明白为什么一些原理,比如tx=ty=0,可以看一下下面这篇文章,有很细致的讲解,我这个是只是对代码的解读。
仿射变换几何讲解
cocos2dx 仿射变换的更多相关文章
- cocos2dx骨骼动画Armature源码分析(三)
代码目录结构 cocos2dx里骨骼动画代码在cocos -> editor-support -> cocostudio文件夹中,win下通过筛选器,文件结构如下.(mac下没有分,是整个 ...
- [原创]cocos2d-x研习录-第二阶 概念类之精灵类(CCSprite)
上一节说布景层CCLayer是小容器,那么精灵类CCSprite就是容器添加的内容,它是构成游戏的主要元素.精灵这个名称应该是游戏专用,它表示游戏中玩家操作的主角.敌人.NPC(Non Player ...
- [置顶] COcos2d-X 中文API
本文来自http://blog.csdn.net/runaying ,引用必须注明出处! COcos2d-X 中文API 温馨提醒:使用二维码扫描软件,就可以在手机上访问我的博客啦!另外大家可以访问另 ...
- Cocos2D-x权威指南: CCNode类方法:
5.1.4 画图节点CCNode和图层CCLayer CCNode中提供的方法,在public块中的方法主要有下面几个部分: 1.针对节点显示的属性信息读写 2.针对节点变换的属性信息读写 3.针对子 ...
- Cocos2D-X扫盲之坐标系、锚点
一.引言 在Cocos2D-X的开发过程中,经常会碰到设置精灵位置的问题.而设置位置的过程,涉及到两个问题:第一是坐标系,包括原点的位置.X/Y坐标轴的方向灯:第二是基准点(Cocos2D-X中叫锚点 ...
- cocos2dx调用浏览器打开网址
安卓端cocos2dx/platform/android路径下CCApplication.h: virtual void openURL(const char* pszUrl); CCApplicat ...
- 使用“Cocos引擎”创建的cpp工程如何在VS中调试Cocos2d-x源码
前段时间Cocos2d-x更新了一个Cocos引擎,这是一个集合源码,IDE,Studio这一家老小的整合包,我们可以使用这个Cocos引擎来创建我们的项目. 在Cocos2d-x被整合到Cocos引 ...
- Cocos2d-x不要随便在onEnter里面addChild
使用任何版本的Cocos2d-x(1.x,2.x,3.0),在onEnter中调用addChild,都要小心谨慎,因为它有可能导致两种莫名其妙的BUG,莫名其妙的BUG当然难以定位了!更何况这个BUG ...
- 通过Matrix进行二维图形仿射变换
Affine Transformation是一种二维坐标到二维坐标之间的线性变换,保持二维图形的"平直性"和"平行性".仿射变换可以通过一系列的原子变换的复合来 ...
随机推荐
- Flask学习之三 web表单
本部分Miguel Grinberg教程的翻译地址:http://www.pythondoc.com/flask-mega-tutorial/webforms.html 开源中国的:http://ww ...
- 自定义View系列教程07--详解ViewGroup分发Touch事件
深入探讨Android异步精髓Handler 站在源码的肩膀上全解Scroller工作机制 Android多分辨率适配框架(1)- 核心基础 Android多分辨率适配框架(2)- 原理剖析 Andr ...
- 15-2 mysql的数据类型
一.整数类型 整数类型:TINYINT SMALLINT MEDIUMINT INT BIGINT 作用:存储年龄,等级,id,各种号码等 ============================== ...
- PHP 网站大流量与高并发的解决方法
php 网站如何应对大流量与高并发呢? 首先,确认服务器硬件是否足够支持当前的流量. 普通的P4服务器一般最多能支持每天10万地理IP,如果访问量比这个还要大,则请配置一台更高性能的专用服务器. 否则 ...
- vue1——vue引入方法,npm本地安装
博客地址 :https://www.cnblogs.com/sandraryan/ Vue.js(读音 /vjuː/, 类似于 view) 是一套构建用户界面的渐进式框架.Vue 只关注视图层, 采用 ...
- POJ 3159 Candies、
题意:n个小孩,m个比较(给你两个孩子代号a,b.然后c意味着a比b最多只能少c个糖果),问1和n之间差距最大的糖果数量. 思路:这是一个差分约束思路 不懂得:传送门, 转化一下就是一个SPFA求最短 ...
- Jquery FormData文件异步上传 快速指南
网站中文件的异步上传是个比较麻烦的问题,不过现在通过jquery 可以很容易的解决这个问题: 使用jquery2.1版本,较老版本不支持异步文件上传功能: 表单代码: <form id=&quo ...
- Project Euler Problem 10-Summation of primes
还是素数线性筛 MAXN = 2000000 prime = [0 for i in range(MAXN+1)] res = 0 for i in range(2,MAXN+1): if prime ...
- SpringBoot2.0--- 多数据源配置
在开发的过程中我们可能都会遇到对接公司其他系统等需求,对于外部的系统可以采用接口对接的方式,对于一个公司开发的两个系统,并且知道相关数据库结构的情况下,就可以考虑使用多数据源来解决这个问题.Spr ...
- H3C 高级ACL部署位置示例