No, this is indeed mathematically the fastest way.
But I can't see this exercise being a problem with a single point in a not to big region so I think your problem revolves from performance problems on large regions or a large set of points. What you might consider is first doing radius select to decrease the number of nodes tested. For instance, do a radius search with a diameter of 1 mile, if the count of the objects returned is larger then zero, do your calculation, otherwise increase the radius with certain steps. (Think big, start small) This is a matter of fine tuning for your specific situation. For objects that are near the center of a big region this will always be a problem. If instead of the nearest node you need to find the nearest 45 degree angle intersection (in dutch this is called loodlijn) with either a node or one of the sides of the region you might consider using real math-techniques. A good site for information on these topics is: http://mathworld.wolfram.com/topics/Geometry.html Good luck and kind regards, Milo van der Linden -----Original Message----- From: Bill Thoen [mailto:[EMAIL PROTECTED] Sent: woensdag 10 november 2004 22:34 To: MapInfo-L Subject: MI-L Fast node search on an object Given an X,Y coordinate and a region object, is there a faster way to find the segment and node number of the node nearest to that coordinate than the brute force method of looping through all the segments and all the nodes comparing distances of each? This would be for one-shot searches with probably different objects each time, so indexing all nodes by writing them out as points would not be faster. (However, this might be a solution for cases where you need to search a given object several times.) Anyone have a faster method than this that you'd care to share? Sub FindNearestNode ( ByVal objFeature As Object, ' The object to search ByVal x As Float, ' X coordinate ByVal y As Float, ' Y coordinate nSegment As Integer, ' Returned segment id nNode As Integer) ' Returned node id ' Finds the node nearest to an X,Y location Dim d0, d1 As Float Dim i, j As Integer Dim x1, y1 As Float 'Find the node nearest to the point clicked on the object. d0 = 10E+31 'Start with an "infinite" distance For j = 1 To ObjectInfo (objFeature, OBJ_INFO_NPOLYGONS) For i = 1 To ObjectInfo (objFeature, OBJ_INFO_NPOLYGONS+j) x1 = ObjectNodeX (objFeature, j, i) y1 = ObjectNodeY (objFeature, j, i) d1 = Distance (x, y, x1, y1, "ft") If d1 < d0 Then d0 = d1 nSegment = j nNode = i End If Next Next End Sub - Bill Thoen --------------------------------------------------------------------- List hosting provided by Directions Magazine | www.directionsmag.com | To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] Message number: 13986 --------------------------------------------------------------------- List hosting provided by Directions Magazine | www.directionsmag.com | To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] Message number: 13989
