Dear List, Several people contacted me asking for the formulae for determining the smallest distance between a point and a line, as well as the coordinates of the point (on the line) to which this distance is measured. I've posted two items ... an example using the formulae and notes showing how I determined the formulae ... to the www. An example of the use of the math involved is posted at the URL http://www.indra.com/~topsoil/nearplin.zip ... In the zip file is a MapBasic program (source & compiled) that finds (selects) the closest polyline (in a table of polylines) to a point that the user clicks on the mapper. This is a demonstration program I wrote several years ago just to prove the concept for myself ... it has serious limitations (but you may find the source code useful for formulae). To run the program, open the included data UNTITLED.TAB into a new mapper and make it editable. Then, run the .MBX. Choose the left hand button on the button pad, and click on a point on the mapper. The program will select the polyline nearest to the point you clicked. The program has to calculate slopes of lines. Because some lines (lines parallel to the y-axis) have undefined slopes, the program will crash whenever it encounters polyline segments that are y-axis parallel (or x-axis parallel, actually, because this program also looks at the slope of lines perpendicular to the polyline segments) -- this is due to a division by zero error. Hey, what can I say? This was just a demonstration program for myself. You'll notice that the example polyline data I provided (UNTITLED.TAB) has none of these problematic segments. In any kind of real product, I'd be testing for axis-parallel segments and doing some kind of end-around to avoid the division by zero error. Note also that the data table is lat/long, and MapBasic defaults to using lat/long coordinates. The .MBX deals with units of degrees, and is calculating Cartesian 'distances' (in degrees!). This is a ridiculous coordinate system to use if one wants to deal with any kind of real distances, since a degree doesn't equal a set distance (converging meridians of longitude as latitudes increase ...). But, like I say, I just wanted to demonstrate this for myself, and any coordinate system would do. For those who want the notes showing how to derive the formulae, I've posted them (as an Adobe Acrobat file) to the URL http://www.indra.com/~topsoil/point2line.pdf ... It's a 570K download, because it's a .PDF of my scanned handwritten notes. Sorry for the unwieldy size, but it was easier to handwrite and scan than to do the diagram and formulae (with all those brackets, fractions, and parentheses) in a word processor or graphics package. If you have email but not web access, let me know and I can attach the files to an email to you. Regards, Eric Johnson GIS Consultant [EMAIL PROTECTED] Notes: 1. These formulae deal in Cartesian distances only, not surface-of-the-Earth geoidal distances. Programmers should use SET COORDSYS in their MapBasic code to be sure they are working in the coordinate system of choice. MapBasic defaults to a lat/long system. 2. The formulae deal with line slopes of mutually perpendicular lines. Because some lines have a slope of 0, their perpendiculars have an undetermined slope (-1/0). Programmers will need to test for these cases and do some kind of end-around them in order to prevent their programs from crashing due to division by zero errors. 3. In using these formulae in MapInfo, I am often trying to find the closest point on a _polyline_ to a given point. This actually means checking the distance to each segment of the polyline, and keeping the shortest determined distance (a similar situation would exist for determining distance to regions). MB programmers can extract node coordinate pairs from polyline (or region) object variables; two adjacent nodes provide enough coordinate pairs to determine the equation of the line connecting them. However, the shortest distance to this _line_ (an _unending_geometric_line_ as determined by the equation derived from the node coordinates, _not_ a MapInfo line object) may not be the shortest distance to the segment of the MapInfo polyline ... i.e. the shortest distance to the _line_ may be to a part of the line that is not coincident with the MI polyline segment. If the shortest distance from the point to the _line_ is not the shortest distance from the point to the MI polyline segment, then the shortest distance to the MI polyline segment will be between the point and one of the segment endpoints (polyline nodes). Checking this distance is easy with the Cartesian distance formula. So, to find the distance from any point to a polyline, I check the distance to each segment ... and to do that, I sometimes also have to check the distance to segment endpoints (polyline nodes). ________________________________________________________________________ Get Your Private, Free E-mail from MSN Hotmail at http://www.hotmail.com ---------------------------------------------------------------------- To unsubscribe from this list, send e-mail to [EMAIL PROTECTED] and put "unsubscribe MAPINFO-L" in the message body, or contact [EMAIL PROTECTED]
