Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 6th, 2006, 12:32 PM   #1
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 2,013
Rep Power: 6 Sane will become famous soon enough
Send a message via MSN to Sane
Recursive Function For Testing A Connection?

I have a two dimensional grid, and need to test if any given point on the grid is connected to any side of the grid, through any other combination of points. Would a recursive function be the best way to do this? If so, how would I do that without ending up with infinite recursion?

If recursion isn't the best option, my other thought was some straight loops, but I'm getting tired of that crap.


Edit: Wrong forum. I meant to put this in "Other Scripting Languages", not "Other Web Development". Move please. :o
Sane is offline   Reply With Quote
Old May 6th, 2006, 2:03 PM   #2
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
I think recursion would be the best way to go about this, as you say. To avoid infinite recursion, which I presume would occur when connections loop back on themselves, just log where you've been.

This sounds like fun. Time to break out Python.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old May 6th, 2006, 7:35 PM   #3
Sane
Programming Guru
 
Sane's Avatar
 
Join Date: Apr 2005
Location: Waterloo, Ontario
Posts: 2,013
Rep Power: 6 Sane will become famous soon enough
Send a message via MSN to Sane
All right. I took a buddy's suggestion and just did iteration from bottom left to top right. Any point that's connected to a wall is deemed connected, and any point that's connected to a point deemed connected... is connected. Then an outside loop goes around that to work itself backwards, and bingo. It worked perfectly. I don't think recursion would have worked as well in this instance.
Sane is offline   Reply With Quote
Old May 6th, 2006, 7:44 PM   #4
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
Makes sense to me. I didn't realise "connected" was defined as being attached to a wall - should've re-read your post. For some reason, I thought you wanted a function to check if a node was connected to another node.
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Old May 6th, 2006, 8:15 PM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
I thought you meant Beej-y connections. Go to your room. I get your dessert.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 6:38 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC