pythonmathalgebra

Mapping a Matrix onto a Coordinate Plane


I am researching how to map a matrix onto a coordinate plane. For example, let's consider a 4x4 matrix. I want this matrix to be mapped onto the coordinate plane with points ranging from -2 to +2 on the x-axis and -2 to +2 on the y-axis. The center of the matrix should always be at the point (0,0). Therefore, the point (-1.5,1.7) should correspond to the matrix[0][0] point. Is there a technique for achieving this?

enter image description here


Solution

  • In your 4x4 case if you have the point at coordinates x,y:

    i = math.trunc(2+x)
    j = math.trunc(2-y)
    value = m[j,i]
    

    value will will your matrix value at x,y coordinates. For the more general case, if your matrix has size (height, width) the code is:

    i = math.trunc( width/2 + x )
    j = math.trunc( height/2 - y )
    value = m[j,i]
    

    This will make a mess if the x, y coordinates are out of bounds so you might need:

    if i<0: i=0
    if i>= width: i=width-1
    if j<0: j=0
    if j>= height: j=height-1
    

    before value = m[j,i].

    See How to limit a number to be within a specified range? (Python) for a better suggestion how to do that.