Nearest 2d coordinates

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • destiny007
    New Member
    • Feb 2008
    • 8

    Nearest 2d coordinates

    Hi,

    Can anyone help me to write code for finding nearest coordinates from aset of coordinates.
  • MACKTEK
    New Member
    • Mar 2008
    • 40

    #2
    Lets do this using the Socratic method.

    First, do you know whether or not you are supposed to use cartesian coordinates? or polar coordinates or some other type?

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      Please read the posting guidelines.


      I can help with specific programming and design issues but I cannot provide complete solutions or act as a tutor.

      Comment

      • whodgson
        Contributor
        • Jan 2007
        • 542

        #4
        I'm not sure if you`ve lost interest but what about this for a start:
        Supposing you had 2 arrays int x[100] and int y [100] which held your sets of x and y coordinates. And suppose that int variables m and n were the 2 coordinates for which you wanted the closest match from x[i] and y[i].
        You could compare the abs value of m with each element in x[ ] using a for loop
        like for(int i =0;i<100;i++)
        { min=abs(m-x[i];
        if(x[i]<min)min=x[i];} //if x[i]>min it would be ignored
        min now holds the least difference between m and x[i] between i=0 and i=99.
        If you traversed the x array again you could identify the element which held the value = m+ or - min and its i th position in the array. Repeat for y [] array and variable n.
        If you could turn this dog`s breakfast into something vaguely representing an algorithm perhaps the code would follow.

        Comment

        • whodgson
          Contributor
          • Jan 2007
          • 542

          #5
          Using 'abs' in the above is not required and prevents you from calculating the desired value of x[i] by simply adding min to m. This then avoids the need for a second traverse of the array.

          Comment

          Working...