My Game --线段数据
在背景中用到了一个自定义的类 VectArr :
class VectArr
{ public:
VectArr( const Bezier & bz, int conut = 30 )
: _bezier( bz )
, _count( conut )
, _bottom( 0 )
{
_lines = new Vect[ _count + 2 ];
getBezier( bz );
}
VectArr( int count = 30 )
: _count( count )
, _bottom( 0 )
{
_lines = new Vect[ count + 2];
}
VectArr( Vect * point )
: _count( 2 )
{
_lines = new Vect[ _count + 2 ];
_lines[ 0 ] = Vect( point->x, 0 );
_lines[ 1 ] = Vect( point->x, Director::getInstance( )->getVisibleSize( ).height );
}
~VectArr( )
{
delete[ ]_lines;
}
private:
Bezier _bezier;
Vect * _lines;
int _count;
int _bottom;
public:
Bezier getBezier( ) const;
Color4F getColor( ) const;
void setColor(const Color4F & c );
Vect * getLines( ) const;
void setCountToBottom( int count );
int getCountToBottom( );
void setCount( int count );
int getCount( ) const;
VectArr * stiching( VectArr * other );
};
类中保存一个Bezier(自定义,在Data文件夹下)变量 _bezier,一个Vect(cocos2d-x 中的二维向量,通常表示点)数组指针 _lines,一个整数 _count 对应的数组大小,一个整数 _bottom 附加点个数
两点一直线,一个有N个点的数组可以看成是N-1条线段的数组,除了开头和结尾的两个点,其他点都与前后的连成两条线段,为了方便,我把这个类的每个对象叫一条线,一条弯弯曲曲的线
这个类中还有几个处理,线段的函数,比如计算线段交点的函数,上面没给出,完整代码点这里查看。
回过头来说下 Bezier 这个结构:
typedef struct Bezier
{
Vect begin;
Vect end;
Vect control1;
Vect control2;
Color4F color; Bezier( ) { }
Bezier( Vect b, Vect e, Vect c1, Vect c2, Color4F c )
: begin( b )
, end( e )
, control1( c1 )
, control2( c2 )
, color( c )
{
}
~Bezier( ) { }
void offset( const Vect & offset );
}Bezier;
Bezier 有四个点和一个颜色(Color4F是cocos2d-x中保存颜色的类,颜色的四个分量:r,g,b,a,数值在0〜1之间)
这个结构体保存一个贝赛尔曲线和对应的颜色,贝赛尔曲线由一个或两个控制点,贝赛尔曲线可以画出平滑的曲线,山体看起来更自然,也可以简化数据存储,不用保存每一个点,只需要四个点就可以了,程序中在 VectArr 中转化为绘图所需的点数组:
Vect * VectArr::getBezier( const Vect & from,
const Vect & to,
const Vect & control1,
const Vect & control2
)
{
float t = 0.0f;
if( control2.isZero( ) )
{
for( unsigned int i = 0; i < _count; i++ )
{
_lines[ i ].x = powf( 1 - t, 2 ) * from.x + 2.0f * ( 1 - t ) * t * control1.x + t * t * to.x;
_lines[ i ].y = powf( 1 - t, 2 ) * from.y + 2.0f * ( 1 - t ) * t * control1.y + t * t * to.y;
t += 1.0f / _count;
}
_lines[ _count ].x = to.x;
_lines[ _count ].y = to.y;
}
else
{
for( unsigned int i = 0; i < _count; i++ )
{
_lines[ i ].x = powf( 1 - t, 3 ) * from.x + 3.0f * powf( 1 - t, 2 ) * t * control1.x + 3.0f * ( 1 - t ) * t * t * control2.x + t * t * t * to.x;
_lines[ i ].y = powf( 1 - t, 3 ) * from.y + 3.0f * powf( 1 - t, 2 ) * t * control1.y + 3.0f * ( 1 - t ) * t * t * control2.y + t * t * t * to.y;
t += 1.0f / _count;
}
_lines[ _count ].x = to.x;
_lines[ _count ].y = to.y;
}
_count += 1;
return _lines;
}
画一座山需要多条线才有层次,用 LineLayer 保存一座山的所有线段;而整个背景中有两座山,用 BgLayerData 保存这个两座山的数据:
class LineLayer
{
public:
LineLayer( );
~LineLayer( ); void AddBezier( float b1, float b2, float e1, float e2,
float c11, float c12, float c21, float c22 );
void AddBezier( float b1, float b2, float e1, float e2,
float c11, float c12, float c21, float c22,
float r, float g, float b, float a );
std::vector<Bezier> getLines( ) const;
bool isEmpty( )const;
private:
std::vector<Bezier> _lines;
}; class BgLayerData
{
public:
BgLayerData( );
~BgLayerData( ); void AddLineLayer( LineLayer * l );
std::vector<LineLayer*> * getLineLayer( ) const;
private:
std::vector<LineLayer*> * _lineLayer;
};
这样就把背景的数据保存好,这画背景时,用 VectArr 类的几个方法处理下,就可以绘制背景了。
My Game --线段数据的更多相关文章
- ArcMap 导入 CGCS2000 线段数据
1. 先确定数据的经纬度和X.Y列是否正确, 2. ArcToolBox ---> DataManagement Tools ---> XY to line 3. 选择数据,选择对应起始点 ...
- My Game --文件读取数据
My Game --线段数据 中说到背景的绘制由贝赛尔曲线生成线段,用 DrawNode 画多边形,同时一张背景有两座山,一座山有两条以上贝赛尔曲线保存,用了嵌套的数据类:Bezier,LineLay ...
- OpenCASCADE Hidden Line Removal
OpenCASCADE Hidden Line Removal eryar@163.com Abstract. To provide the precision required in industr ...
- (转)用AGG实现高质量图形输出(二)
本文上接<用AGG实现高质量图形输出(一)>,分别介绍了AGG显示流程中的各个环节. 上次讲了AGG的显示原理并举了一个简单的例子,这一篇文章开始讲AGG工作流程里的每个环节.为了方便对照 ...
- [CF787D]遗产(Legacy)-线段树-优化Dijkstra(内含数据生成器)
Problem 遗产 题目大意 给出一个带权有向图,有三种操作: 1.u->v添加一条权值为w的边 2.区间[l,r]->v添加权值为w的边 3.v->区间[l,r]添加权值为w的边 ...
- hihoCoder #1079 : 离散化 (线段树,数据离散化)
题意:有一块宣传栏,高一定,给出长度,再给出多张海报的张贴位置,问还能见到几张海报(哪怕有一点被看到)?假设海报的高于宣传栏同高. 思路:问题转成“给出x轴上长为L的一条线段,再用n条线段进行覆盖上去 ...
- POJ 2528(线段树+离散化+特殊离散化)网上博客很少有人真正写对!!! 是POJ数据太水...
Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral electio ...
- 好题 线段树对数据的保存+离线的逆向插入 POJ 2887
题目大意:给一个字符串,有插入和询问操作,每次往一个位置插入一个字符或者询问第p个位置的字符是什么. 思路:我们离线询问,逆向把所有的字符都插入给线段树,然后再查询就好了,每次都要记得插入线段树的最后 ...
- BZOJ4732. [清华集训2016]数据交互(树链剖分+线段树+multiset)
题目链接 https://www.lydsy.com/JudgeOnline/problem.php?id=4732 题解 首先,一个比较显然的结论是:对于一棵有根树上的两条链 \((x_1, y_1 ...
随机推荐
- 使用Log4Net完成异常日志处理
1.在MVC的Modal文件夹建一个异常处理过滤器 public class MyExceptionAttribute:HandleErrorAttribute { public static Que ...
- 关于python文件操作
http://www.cnblogs.com/rollenholt/archive/2012/04/23/2466179.html 总是记不住API.昨晚写的时候用到了这些,但是没记住,于是就索性整理 ...
- Hadoop编程1:天气数据AWK & MapReduce
本文介绍通过AWK和MapReduce两种方式统计出每年温度到最高气温直.awk速度虽然快,而且简短,但是数据量巨大到时候,就遇到力瓶颈,及时分布式执行awk脚本,也会出现机器死掉等问题,需要容错机制 ...
- 机器学习实战-边学边读python代码(3)
程序清单2-3 归一化特征值: def autoNorm(dataSet): /* >>> barray([[ 1., 2., 3.], [ 2., 3., 4.], [ 10., ...
- Linux之grep命令详解
简介 grep (global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它 ...
- Python之反射
一.引言 有时候我们会碰到类似这样的需求,就是想要执行类的某个方法,或者需要对对象的某个参数赋值,而方法名或参数名已经包装在类中并不能去顶,需要通过参数传递字符串的形式输入.在这样的情况你会选择什么样 ...
- [OGRE]最小ogre程序的流程
总结一下最小ogre程序的流程: 1 创建Ogre::Root 2 用Ogre::Root加载插件,必须载入的是场景管理器和渲染器 3 调用Ogre::ResourceGroupManager::ge ...
- 简单了解.net
.NET是 Microsoft XML Web services 平台.XML Web services 允许应用程序通过 Internet 进行通讯和共享数据,而不管所采用的是哪种操作系统.设备或编 ...
- [问题2014A07] 解答
[问题2014A07] 解答 我们分三步进行证明. \(1^\circ\) 先证 \(\alpha_1,\alpha_2\) 线性无关. 用反证法, 设 \(\alpha_1,\alpha_2\) ...
- Key Figure、Exception Aggreagion、Non-Cumulative KeyFigure
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...