|
|
#1 |
|
For you math pros out there..
Not neccessarily Objective-C related, although the end goal is to have that, this is more of a brain teaser if you like! A virtual pint goes to the person who solves it!
![]() I'm working on an app that displays "tiles" (basically images on buttons 190x190 pixels) in a grid that is 4 tiles wide by infinite deep, on a UIScrollView with vertical scrolling only. Each tile needs to be created at runtime and added in a sequential order, ie row 0 1 thru 4, row 2 1 thru 4 etc. I have everything working great, no problems. Saying that, I now want to include tiles that are 2x2 tiles in size added in the same sequential order, but I need to calculate the correct x & y positions of the smaller tiles that are added around it so they don't overlap. So if the 2x2 tile lands at column 3 & 4 on row 1, then the next two tiles should be added to rows 2, column 1 & 2 then the next 9 thru 12. In some cases a large tile might only appear once, or not at all. And the large tiles can only appear starting in columns 1 or columns 3, not in the middle and they can appear side by side (as in example below). So I've been racking my brain on this for the past two days and have not come up with anything substantial without creating messy if..then logic, so ideally this needs to go into a method that I would call and it would just give me the x and y coordinates back of where to put the next small tile if a larger tile is occupying the space next to it. I was thinking by giving each tile a sequential number I can determine what numbers the larger ones take up and use that in an array or something. That way I can figure out what tile space is not occupied, but from there..... staring at ceiling....emptiness. I'm not a math expert so suspect there's some easy formula I can use, at least that's what I hope! ![]() ![]() Cheers. |
|
|
|
0
|
|
|
#2 |
|
I don't have a complete answer, but more of a suggestion. After you add a new square add it to an array of added squares. Then when you add another square to the next position loop through each square in the array and check if the new square overlaps one of the existings squares. If it does then go to the next position and check again until it finds an open spot. It could be done using a recursive function call. Also there is a function rectcontainsrect (I think that's what it's called) that should help you find out if squares are overlapping. Hope that helps.
__________________
Check out Marble Marathon on the App Store. It puts a whole new twist on the typical Labyrinth type game! http://bweberapps.wordpress.com |
|
|
|
0
|
|
|
#3 | |
|
Quote:
Thanks mate, that was exactly what I was looking for.CGRecotconstrainsrect led me to CGRectIntersectsRect which works great. With just a few lines of code I was able to quickly check if the tile fell on top of the larger one and if so just move it down the chain until it didn't intersect with anything. Cheers again! |
||
|
|
0
|
|
|
#4 | |
|
Quote:
|
||
|
|
0
|
|
|
#5 |
|
Hey holmesf, yeah you are right and I did think of that. So I have a counter in there that after 4 counts resets and no longer checks the intersection until another larger square pops up. Thanks for that though.
|
|
|
|
0
|
![]() |
|
«
Previous Thread
|
Next Thread
»
| Thread Tools | Search this Thread |
| Display Modes | |
|
|
Similar Threads
|
||||
| thread | Thread Starter | Forum | Replies | Last Post |
| School of Math's-Kids App for Mathametics | johnsonlongman | iPad Apps | 0 | Jul 27, 2011 09:13 AM |
| Two questions for you MBA owners out there. (Potential purchase over new MBP) | Sarngate | MacBook Air | 25 | Mar 5, 2011 06:02 AM |
| For all you LotRO fans out there! | jonnysods | MacBook | 11 | Sep 13, 2008 08:53 AM |
| For all you RPG fans out there... | chrisb3 | iPhone and iPod touch Apps | 37 | Jul 17, 2008 02:57 PM |
| For all you Mario lovers out there... | cpjakes | Community | 7 | Feb 25, 2005 12:48 PM |
All times are GMT -5. The time now is 01:19 AM.









Linear Mode

