Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Procedure for locating nearest site on X & Y Axis
Message
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Divers
Thread ID:
00539725
Message ID:
00539736
Vues:
20
>I have a situation where I have X-Axis and Y-Axis coordinates for various sites in a geographical region. From one of those sites, I need to create a list of the other sites, beginning with the nearest one, and continuing out with the next nearest one, and so on, in that order.
>
>I am using a set of cursors, and manipulating them into a table that I actually display, but the final sort order is not quite there. If I manually measure some of the distances on the map as compared to the resulting list, they are still a little jumbled.
>
>Has anyone ever created a procedure that will do anything close to this? I am almost out of ideas, and need some help. ANY suggestions would be greatly appreciated.
>
>Thanks in advance...

* Find distances between sets of points.

* Create a 4 field cursor that looks like the following:
*
* Point   X     Y  Distance
*  A     80    10     0
*  B     90    30     0
*  C     85    40     0
*  D    120    30     0
*  E     55    60     0
*  F     15    22     0
*  G     40   110     0
*  H     60    60     0

CREATE CURSOR curTest ( cPoint C(1), nXValue N(3), nYValue N(3), nDistance N(5,2))
FOR iRow = 1 TO 8
   INSERT INTO curTest ;
      VALUES (CHR( 65 + RECCOUNT() ) ;
              , VAL( SUBSTR( " 80 90 85120 55 15 40 60", (iRow*3)-2, 3 )) ;
              , VAL( SUBSTR( " 10 30 40 30 60 22110 60", (iRow*3)-2, 3 ));
              , 0 )
NEXT iRow

fCalculateDistancesFrom( "D" )

INDEX ON nDistance TAG nDistance
LOCATE
BROWSE LAST NOWAIT
RETURN .T.

**************************************************
FUNCTION fCalculateDistancesFrom( cSpecificPoint )
**************************************************

   SELECT nXValue, nYValue ;
      FROM curTest ;
      INTO ARRAY aSpecificPoint ;
      WHERE cPoint = cSpecificPoint

   UPDATE curTest ;
      SET nDistance = 0

   UPDATE curTest ;
      SET nDistance = fCalcDistance( nXValue, nYValue ) ;
      WHERE cPoint # cSpecificPoint

   RETURN .T.
   ENDFUNC   && fCalculateDistancesFrom( cSpecificPoint ).

******************************************
FUNCTION fCalcDistance( nXValue, nYValue )
******************************************
   IF nXValue == aSpecificPoint[1] ;
      AND nYValue == aSpecificPoint[2]
      *- Points coincide.
      RETURN 0
   ENDIF
   
   IF nXValue == aSpecificPoint[1]
      *- Points have same x value.
      RETURN ABS( nYValue - aSpecificPoint[2] )
   ENDIF
   
   IF nYValue == aSpecificPoint[2]
      *- Points have same y value.
      RETURN ABS( nXValue - aSpecificPoint[1] )
   ENDIF

   *- Both X and Y differ.
   RETURN SQRT( ABS( nYValue - aSpecificPoint[2] )^2 ;
               + ABS( nXValue - aSpecificPoint[1] )^2 )
   ENDFUNC   && fCalcDistance( nXValue, nYValue ).
censored.
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform