Line algorithim for python and numeric

Paul McGuire ptmcg at austin.rr._bogus_.com
Fri Oct 13 11:20:15 EDT 2006


"Theerasak Photha" <hanumizzle at gmail.com> wrote in message 
news:mailman.449.1160750510.11739.python-list at python.org...
> On 13 Oct 2006 07:33:17 -0700, bcdonovan at gmail.com <bcdonovan at gmail.com> 
> wrote:
>> Hi everyone,
>>
>>   I'm wondering if anyone knows of a way to extract data from a numeric
>> array along a line. I have a gridded dataset which I would like to be
>> able to choose two points and extract a 1-d array of the data values
>> along the line between the two points. Any ideas?
>
> Are these all 45 degree lines or what?
>
> If not, you'll have to use trigonometry and approximate the closest
> matching cell. (Don't worry, Python has maths functions!! :))
>
> -- Theerasak

No need for that messy trig stuff - simpler and faster would be to use 
linear interpolation, since you know the starting and ending cell 
coordinates, and how many values you want, compute the array of... oh, hell, 
here it is:

start = (4,10)
end = (304,310)
n = 11

dn = ((end[0]-start[0])/float(n),(end[1]-start[1])/float(n))
interpedNodes = [ (start[0]+dn[0]*x, start[1]+dn[1]*x) for x in range(n+1) ]

for nod in interpedNodes:
    print nod
print

cellNodes = map(lambda ii: (int(round(ii[0])), int(round(ii[1]))), 
interpedNodes)
for nod in cellNodes:
    print nod

Prints out:
(4.0, 10.0)
(31.272727272727273, 37.272727272727273)
(58.545454545454547, 64.545454545454547)
(85.818181818181813, 91.818181818181813)
(113.09090909090909, 119.09090909090909)
(140.36363636363637, 146.36363636363637)
(167.63636363636363, 173.63636363636363)
(194.90909090909091, 200.90909090909091)
(222.18181818181819, 228.18181818181819)
(249.45454545454547, 255.45454545454547)
(276.72727272727275, 282.72727272727275)
(304.0, 310.0)

(4, 10)
(31, 37)
(59, 65)
(86, 92)
(113, 119)
(140, 146)
(168, 174)
(195, 201)
(222, 228)
(249, 255)
(277, 283)
(304, 310) 





More information about the Python-list mailing list