背景中用到了一个自定义的类 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 --线段数据的更多相关文章

  1. ArcMap 导入 CGCS2000 线段数据

    1. 先确定数据的经纬度和X.Y列是否正确, 2. ArcToolBox ---> DataManagement Tools ---> XY to line 3. 选择数据,选择对应起始点 ...

  2. My Game --文件读取数据

    My Game --线段数据 中说到背景的绘制由贝赛尔曲线生成线段,用 DrawNode 画多边形,同时一张背景有两座山,一座山有两条以上贝赛尔曲线保存,用了嵌套的数据类:Bezier,LineLay ...

  3. OpenCASCADE Hidden Line Removal

    OpenCASCADE Hidden Line Removal eryar@163.com Abstract. To provide the precision required in industr ...

  4. (转)用AGG实现高质量图形输出(二)

    本文上接<用AGG实现高质量图形输出(一)>,分别介绍了AGG显示流程中的各个环节. 上次讲了AGG的显示原理并举了一个简单的例子,这一篇文章开始讲AGG工作流程里的每个环节.为了方便对照 ...

  5. [CF787D]遗产(Legacy)-线段树-优化Dijkstra(内含数据生成器)

    Problem 遗产 题目大意 给出一个带权有向图,有三种操作: 1.u->v添加一条权值为w的边 2.区间[l,r]->v添加权值为w的边 3.v->区间[l,r]添加权值为w的边 ...

  6. hihoCoder #1079 : 离散化 (线段树,数据离散化)

    题意:有一块宣传栏,高一定,给出长度,再给出多张海报的张贴位置,问还能见到几张海报(哪怕有一点被看到)?假设海报的高于宣传栏同高. 思路:问题转成“给出x轴上长为L的一条线段,再用n条线段进行覆盖上去 ...

  7. POJ 2528(线段树+离散化+特殊离散化)网上博客很少有人真正写对!!! 是POJ数据太水...

    Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral electio ...

  8. 好题 线段树对数据的保存+离线的逆向插入 POJ 2887

    题目大意:给一个字符串,有插入和询问操作,每次往一个位置插入一个字符或者询问第p个位置的字符是什么. 思路:我们离线询问,逆向把所有的字符都插入给线段树,然后再查询就好了,每次都要记得插入线段树的最后 ...

  9. BZOJ4732. [清华集训2016]数据交互(树链剖分+线段树+multiset)

    题目链接 https://www.lydsy.com/JudgeOnline/problem.php?id=4732 题解 首先,一个比较显然的结论是:对于一棵有根树上的两条链 \((x_1, y_1 ...

随机推荐

  1. jQuery文字特效制作文字鼠标滑过多彩色变色显示

    <!DOCTYPE html><head> <meta http-equiv="Content-Type" content="text/ht ...

  2. Python爬虫--简单爬取图片

    今天晚上弄了一个简单的爬虫,可以爬取网页的图片,现在现在做一下准备工作. 需要的库:urllib 和 re urllib库可以理解为是一个url下载器,其中有三个重要的方法 urllib.urlope ...

  3. Servlet-cookies机制

    通过cookies,可以保存用户的使用习惯,优化用户体验,同时能减轻服务端压力.下面说下在Servlet中cookies机制的使用 就用保存用户登录数据来举例子: 打开网页的处理Servlet: pa ...

  4. CentOS 更改yum源与更新系统

    FROM:http://www.cnblogs.com/lightnear/archive/2012/10/03/2710952.html [1] 首先备份/etc/yum.repos.d/CentO ...

  5. jquery简单动画

    自定义滑入滑出动画 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Defaul ...

  6. GMF中,删除节点和连线的另一种实现

    问题 在GMF中,如果需要programmatically删除节点或连线,在google中我们很容易搜索到<GMF中,删除节点和连线的实现>一文(我并不确定这是原创作者的原始链接),很多人 ...

  7. jQuery 效果 —— 滑动

    jQuery 效果 -- 滑动 1.向下滑动元素 (1)使用slideDown()方法可以用于向下滑动元素 $("button").click(function(){ $(&quo ...

  8. didFinishLaunchingWithOptions

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launc ...

  9. 怎么修改git提交过的内容

    git修改历史提交   Git使用amend选项提供了最后一次commit的反悔.但是对于历史提交呢,就必须使用rebase了. git rebase -i HEAD~3 表示要修改当前版本的倒数第三 ...

  10. 怎样让webservice在浏览器远程浏览时像在本地浏览一样有参数输入框

    从远程客户端访问服务器上的WebService能够显示,但点击调用相关的方法时显示“只能用于来自本地计算机的请求”,这时提醒我们还需要在服务器进行相关的配置才能让其他机器正常访问该WebService ...