ios - Creating a hexagonal grid with multiplicative properties -
create hexagonal grid of ninteen circles properties:
it can divided tiles of 3 adjacent circles (with 1 circle left over). means, there 18 circles divided 6 tiles of 3 adjacent circles. tiles can of shape: straight line or in zig-zag order. first needs adjacent of second , second 1 needs adjacent of third 1 or can adjacent of 1 another.
each tile of 3 circles contains 3 numbers such x1, x2, , x3 (in order), x3 = x1*x2;
i have tried designing hexagonal structure above properties:
-(id)createhexagongrid{ // create hexagon grid array of 5x5 -1 non-existent elements nsmutablearray *hexagongrid = [nsmutablearray arraywithobjects: [nsmutablearray array], [nsmutablearray array], [nsmutablearray array], [nsmutablearray array], [nsmutablearray array], nil]; hexagongrid = @[@[@0,@0,@0,@-1,@-1], @[@0,@0,@0,@0,@-1], @[@0,@0,@0,@0,@0], @[@-1,@0,@0,@0,@0], @[@-1,@-1,@0,@0,@0]]; // adjacency dictionary different nodes nsmutabledictionary *adjacencydict = [self createadjacencydictionary:hexagongrid]; // create dictionary keep track of nodes properties nsmutabledictionary *graph = [nsmutabledictionary dictionary]; // create array keep track of visited nodes nsmutablearray *randomindexarray = [[nsmutablearray alloc] init]; (nsinteger i=0; i<5; ++i) { (nsinteger j=0; j<5; ++j) { if (![hexagongrid[i][j] isequal: @-1]){ nsnumber *temp = @(i*5 + j); [randomindexarray addobject:temp]; } } } // final algorithm (nsinteger i=0; i<6; ++i) { // first vertex int randidxfirst = arc4random_uniform((int)[randomindexarray count]); nsnumber *firstvertexidx = randomindexarray[randidxfirst]; [randomindexarray removeobjectatindex:randidxfirst]; // remove random index list (nsnumber * key in adjacencydict.allkeys) { adjacencydict[key] = [adjacencydict[key] filteredarrayusingpredicate:[nspredicate predicatewithformat:@"intvalue != %@",firstvertexidx]]; } // int rowfirst = [firstvertexidx integervalue]/5; // int colfirst = [firstvertexidx integervalue]%5; // todo: put value in hexagongrid[rowfirst][colfirst] // second vertex int randidxsecond = arc4random_uniform((int)[[adjacencydict objectforkey:firstvertexidx] count]); nsnumber *secondvertexidx = [[adjacencydict objectforkey:firstvertexidx] objectatindex:randidxsecond]; [randomindexarray removeobject:secondvertexidx]; //remove random index list (nsnumber * key in adjacencydict.allkeys) { adjacencydict[key] = [adjacencydict[key] filteredarrayusingpredicate:[nspredicate predicatewithformat:@"intvalue != %@",secondvertexidx]]; } // int rowsecond = [secondvertexidx integervalue]/5; // int colfirst = [secondvertexidx integervalue]%5; // third vertex int randidxthird = arc4random_uniform((int)[[adjacencydict objectforkey:secondvertexidx] count]); nsnumber *thirdvertexidx = [[adjacencydict objectforkey:secondvertexidx] objectatindex:randidxthird]; [randomindexarray removeobject:thirdvertexidx]; //remove random index list (nsnumber * key in adjacencydict.allkeys) { adjacencydict[key] = [adjacencydict[key] filteredarrayusingpredicate:[nspredicate predicatewithformat:@"intvalue != %@",thirdvertexidx]]; } [graph setobject:@[firstvertexidx,secondvertexidx,@(i)] forkey:thirdvertexidx]; // [graph setobject:@[firstvertexidx,secondvertexidx,@(i)] forkey:thirdvertexidx]; } return graph;
}
step one: found dictionary of adjacent elements each circle key circle , values adjacent.
step two: iterate 6 times since there 6 tiles , choose 3 vertices randomly. way works choose first 1 randomindexarray , remove can't used further in future. second 1 chosen adjacency dictionary random adjacent chosen key. third 1 chosen second one.
however, problem face runs out of adjacent vertices go through 6 iterations since adjacent dictionary cleared go. can suggest better way this?
Comments
Post a Comment