Question

i have question about finding maximum x or y-value in text file. This is necessary for point in polygon algorithm.
Polygons are entered in text file together with point, that is in or out polygon and together with number of lines of polygon.

For example:

5.0 4.0 6 1.0 1.0 4.0 0.0 6.0 3.0 7.0 6.0 3.0 7.0 0.0 4.0 1.0 1.0  

The point is [5.0 4.0], polygon has 6 lines and next are written points of polygon:

[1.0 1.0][4.0 0.0][6.0 3.0][7.0 6.0][3.0 7.0][0.0 4.0][1.0 1.0].

First is x-coordinate, second is y-coordinate.

I have procedure that tests the position of 2 lines. Now I have to combine this procedure with this one algorithm.

What i have is:open txt a read line of txt, then split it and save the values to variables

pointinp = open ("pointinp.txt", "r")  
for line in pointinp.readlines():  
    riadok = line.split()  
    j=3  
    for i in range (0,riadok[2]):  
        x1=float(riadok[0])  
        y1=float(riadok[1])  
        k1=float(riadok[i+j])  
        l1=float(riadok[i+(j+1)])  
        k2=float(riadok[i+(j+2)])  
        l2=float(riadok[i+(j+3)])  
        j=j+1  

Point coordinates is in x1 and y1, coordinates of other points are in k1,l1,k2,l2. Now i have to find maximum x and y coordinate from text file to save it to x2,y2. So then I will have 2 lines [x1,y1][x2,y2] and [k1,l1][k2,l2] and then i will test position of these lines and this is necessary in point in polygon algorithm, where will be counted the intersection points.(intersection between created line containing point and each line of polygon)

Was it helpful?

Solution

Findind the maximum x and y in the line is straightforward enough, you just need to be able to separate them out and apply the max() function to them. Fortunately python list indexing supports a step size, so you can easily pull out every other value:

xvals=riadok[3::2]
yvals=riadok[4::2]
xmax=max(xvals)
ymax=max(yvals)

Finding the maximum in the file is a bit harder: for each line you can compare your local maximum to the largest figure found so far, and replace the latter with the former if it is greater:

xtruemax=max(xtruemax,xmax)
ytruemax=max(ytruemax,ymax)

but you will find these figures slowly growing as you go further through your file. If you want the genuine largest value of x and y available right from line one then you will have to process your file in two passes, once to find the maximum x and y and once to apply your algorithm.

OTHER TIPS

If you like powerfull code you would like this :) If you just have integer numbers you can substitute float by int.

file.txt

1 2 3 900000.029384 10000 6 8 900000.029383

maximum= max(map(float,(open('file.txt', 'r').readline()).split()))

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top