【问题标题】:finding iteration formula for points arranged in a triangle查找三角形排列的点的迭代公式
【发布时间】:2013-04-25 11:56:55
【问题描述】:

我想使用 for 循环在以下位置排列我的精灵:

ccp(240.0, 160.0);
ccp(300.0, 120.0);
ccp(300.0, 200.0);
ccp(360.0, 80.0);
ccp(360.0, 160.0);
ccp(360.0, 240.0); 

我正在尝试使用 for 循环获取这些点的迭代公式。我已经有一段时间了。下面是点的视觉表示。请帮忙。

        *

    *

*       *

    *

        *

【问题讨论】:

  • 你想用for循环生成点吗?如果是这样,这是一个坏主意,不值得花时间。如果您只需要遍历点数组,则需要将点存储在数组中。

标签: objective-c loops cocos2d-iphone iteration


【解决方案1】:

你想要这样的东西吗? (它只是一个快速的草图)

假设初始参数:

initPoint (x0, y0)

initVector (vX,vY)

num of iteration c

index = c

while index

 for(j = index, currentPoint = initPoint+(c-index)*(0,2*vY); j;j--, currentPoint += initVector)
    draw currentPoint

index--

基本上,主要思想是,您从右上角开始,随着迭代保持(到左下角)并绘制点,使用初始向量多次移动绘图的上下文。然后将初始点沿 y 轴向下移动并重复负一次。

【讨论】:

    【解决方案2】:

    这是一种方法:

    我没有花几分钟的时间去想它,但如果你从最大的一排开始往下走会更容易:

    PatternTest.h

    #import "cocos2d.h"
    @interface PatternTest : CCLayer
    @end
    

    PatternTest.m

    @implementation PatternTest
    -(id) init
    {
        if( (self=[super init]))
        {
            CCNode *grid = [self generateArrowPatternWithBaseRowOfNumSprites:5 spacedApart:ccp(25.0f, 25.0f)];
            [grid setPosition:ccp(50.0f,50.0f)];
            [self addChild:grid];
    
        }
    
        return self;
    }
    
    -(CCNode *) generateArrowPatternWithBaseRowOfNumSprites:(float) numSprites spacedApart:(CGPoint) space
    {
        CCNode* patternNode = [CCNode node];
        CGPoint tempPos = ccp(0.0f, 0.0f);
        float offset = 0.0f;
        while (numSprites > 0)
        {
            for(int x=numSprites;x>0;x--)
            {
                CCSprite *patternSprite = [CCSprite spriteWithFile:@"Icon.png"];
                [patternSprite setScale: 0.3f];
                [patternSprite setPosition: tempPos];
                [patternNode addChild:patternSprite];
                tempPos = ccpAdd(tempPos, ccp(0.0f,space.y));
            }
    
            tempPos = ccp(tempPos.x, 0.0f);
            offset = offset + (space.y / 2.0f);
            tempPos = ccpAdd(tempPos, ccp(space.x, offset));
            numSprites -= 1;
        }
        return patternNode;
    }
    @end
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-16
      • 2019-09-08
      • 1970-01-01
      • 1970-01-01
      • 2012-06-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多