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 ...
随机推荐
- 【final】站立会议---11.27
名称:nice! 组长:李权 成员:于淼 刘芳芳韩媛媛 宫丽君 时间:11月27日 13:00 项目内容:约跑app(约吧) 地点:传媒西楼220室 内容: 新任务的分配 1.李权分配任务 2.韩媛 ...
- 学DIV+CSS技术,如何入门?(2)
http://www.zhangbin.in/a/jishuziliao/CSSjishu/2014/0730/13267_2.html
- Hadoop集群环境搭建
----------------------------------------------------------- 自学记录,交流学习请发送邮件至gxz1984@gmail.com ------- ...
- pip 添加trusted host 一劳永逸
继上一篇<Python virtualenv安装库报错SSL: CERTIFICATE_VERIFY_FAILED>出现的问题进行一个一劳永逸的操作 pip升级到7.0以后,在使用http ...
- ios设备相关
设备方向 typedef NS_OPTIONS(NSUInteger, UIInterfaceOrientationMask) { UIInterfaceOrientationMaskPortrait ...
- 配置org.springframework.scheduling.quartz.CronTriggerBean (转载)
在项目中又用到了定时器,对于定时器的应用总是模模糊糊的,今天结合网上找到的资料与自己在项目中写的简单地在此写一下,以备需要时查阅. 一个Quartz的CronTrigger表达式分为七项子表达式,其中 ...
- linux&win7双系统安装
linux&win7双系统安装 硬盘大小分配方案 按照顺序来建立分区 /swap 4G ==即交换分区,也是一种文件系统,它的作用是作为Linux的虚拟内存.在Windows下, ...
- java练习题(字符串类):显示4位验证码、输出年月日、从XML中抓取信息
1.显示4位验证码 注:大小写字母.数字混合 public static void main(String[] args) { String s="abcdefghijklmnopqrstu ...
- php null o false ''
php中很多还不懂php中0,"",null和false之间的区别,这些区别有时会影响到数据判断的正确性和安全性,给程序的测试运行造成很多麻烦.先看一个例子: <? $str ...
- Skyfree的毕业论文 《系统封装与部署的深入研究》
Skyfree的毕业论文 <系统封装与部署的深入研究> https://www.itsk.com/thread-197-1-4.html Skyfree 发表于 2007-9-13 07: ...