[Python.NET] Help please: Convert to Python
shery anderson
shery202007 at yahoo.com
Sun Dec 3 07:43:52 CET 2006
I have below code that needs to be recoded into Python for use in ESRI ArcGIS software.
I would greatly appreciate any assistance.
Thanks
Sheri
/* The Inner Loop of the Douglas-Peucker line generalization
algorithm is the process of finding, for a section of a polyline,
the vertex that is furthest from the line segment joining the
two endpoints. The method coded below in C (or C++) is the most
efficient, in terms of operation counts, that I have seen. */
/* _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ */
long FurthestFromSegment ( /* Return index of furthest point. */
long startindex, /* Index of start vertex in arrays. */
long endindex, /* Index of end vertex in arrays. */
double *x , /* Array, abscissae of polyline vertices. */
double *y , /* Array, ordinates of polyline vertices. */
double *distMaxSquare /* Return square of maximum distance. */
)
/*
This function, given a section of a polyline in arrays,
will return the index of the intermediate node that is furthest
from the segment joining the two endpoints of the section,
and the square of the distance from the segment.
If no intermediate point exists, then the returned index will
be the index of the start vertex and the returned distance
squared will be -1.0 . Caution: Do not calculate the square
root of this returned value without ruling out the possibility
that it may have defaulted to -1.0 . In a normal
Douglas-Peucker application, you should never have to calculate
the square root of this output value, and you should never
need to invoke this function without intermediate points.
*/
{
/*
The variable names below assume we find
the distance of point "A" from segment "BC" .
*/
long index, outindex ;
double distSquare, bcSquare ;
double cx, cy, bx, by, ax, ay ;
double bcx, bcy, bax, bay, cax, cay ;
*distMaxSquare = -1.0 ;
if ( endindex < startindex + 2 ) return startindex ;
outindex = startindex ;
bx = x[startindex] ;
by = y[startindex] ;
cx = x[endindex] ;
cy = y[endindex] ;
/* Find vector BC and the Square of its length. */
bcx = cx - bx ;
bcy = cy - by ;
bcSquare = bcx * bcx + bcy * bcy ;
/* The inner loop: */
for ( index = startindex + 1 ; index < endindex ; index++ )
{
/* Find vector BA . */
ax = x[index] ;
ay = y[index] ;
bax = ax - bx ;
bay = ay - by ;
/* Do scalar product and check sign. */
if ( bcx * bax + bcy * bay <= 0.0 )
{
/* Closest point on segment is B; */
/* find its distance (squared) from A . */
distSquare = bax * bax + bay * bay ;
}
else
{
/* Find vector CA . */
cax = ax - cx ;
cay = ay - cy ;
/* Do scalar product and check sign. */
if ( bcx * cax + bcy * cay >= 0.0 )
{
/* Closest point on segment is C; */
/* find its distance (squared) from A . */
distSquare = cax * cax + cay * cay ;
}
else
{
/* Closest point on segment is between B and C; */
/* Use perpendicular distance formula. */
distSquare = cax * bay - cay * bax ;
distSquare = distSquare * distSquare / bcSquare ;
/* Note that if bcSquare be zero, the first
of the three branches will be selected,
so division by zero will not occur here. */
}
}
if ( distSquare > *distMaxSquare )
{
outindex = index ;
*distMaxSquare = distSquare ;
}
}
/*
Note that in the inner loop above, if we follow
the most common path where the perpendicular
distance is the one to calculate, then for each
intermediate vertex the float operation count is
1 divide, 7 multiplies, 5 subtracts, 1 add, and 2 compares.
*/
return outindex ;
}
---------------------------------
Need a quick answer? Get one in minutes from people who know. Ask your question on Yahoo! Answers.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/pythondotnet/attachments/20061202/1787cda5/attachment.html
More information about the PythonDotNet
mailing list