#Code improvement for viewpoint in 2d array based on coordinate

2 messages · Page 1 of 1 (latest)

naive stratus
#

**mapViewer.py📁 **
Looking for code quality improvements for this segment of code.

'''
coord: (X,Y) -> represents coordinates for viewpoint
range: ((VX0,VX1),(VY0,VY1)) -> vision range for each axis
'''

def Viewpoint(mapObj,coord:tuple,range:tuple) -> None:
    mapLayout = mapObj.map
    range = ((range[0],range[0]),(range[1],range[1]))

    #Fixing view range in both dimensions, Depending on boundaries of map
    #If coord + range = outofbounds: lower range until its within bounds

    if coord[0] - range[0][0] < 0:
        range[0][0] -= abs(coord[0] - range[0][0])
    if coord[0] - range[0][1] < 0:
        range[0][1] -= abs(coord[0] - range[0][1])
    if coord[1] - range[1][0] < 0:
        range[1][0] -= abs(coord[1] - range[1][0])
    if coord[1] - range[1][1] < 0:
        range[1][1] -= abs(coord[1] - range[1][1])

    #With range within bounds, We can start string formatting for visual layout relative to coordinates

    for j,y in enumerate(mapLayout[coord[1] - range[1][0]: (coord[1] + range[1][1])+1]):
        print(f"{"\n"}{"-"*(((range[0][0]+range[0][1]+1)*2)+1)}{"\n"}")
        output = ""
        for i,x in enumerate(y[coord[0] - range[0][0]: (coord[0] + range[0][1])+1]):
            if (i,j) == (0+range[0][0],0+range[1][0]):
                output+=f"|P"
            else:
                output+=f"|{x}"
        output+="|"
        print(output)
    print(f"{"\n"}{"-"*(((range[0][0]+range[0][1]+1)*2)+1)}{"\n"}")
frank pondBOT
#

Thanks for your question :clap:, if someone gives you an answer it would be great if you thanked them with a :white_check_mark: in response. This response will earn you both points for special roles on this server.