CImage:CoordOf


The CoordOf method returns a lua table containing the 1-based pixel coordinates of a linear position in the data array. The first pixel in the data array is at position 1, corresponding to pixel coordinates {1,1,...}. Also see the inverse method, Pos.

Syntax

tCoord[] = CImage:CoordOf( nPos )

where

    nPos is the linear position in the data array. The first element is at position 1.

    tCoord[] is a Lua array containing the 1-based coordinates of the linear position.

Examples

The following script creates a 3-D image and then returns the coordinates of a linear offset into the image array.

I = CImage:new( {100,200,50} "double" )

-- create a CImage of 100 x 200 x 50 pixels

t = I:CoordOf( 783810 )

-- specify the position

Printf( "x,y,z = %d,%d,%d\n", t )

-- result: x,y,z = 10,20,40

In the next example, suppose the image dimensions are not immediately known. The following script returns the coordinate for each dimension of the data array. Note that the number of array axes could be determined directly by using the Axes method.

t = I:CoordOf( 783810 )

-- specify the offset

for i = 1,#t do

-- use #t to get the number of table elements

  Printf( "x[%d] = %d\n", i, t[i] )

-- result: x[1] = 10 x[2] = 20 x[3] = 40

end

 

Another way to do the same thing is to use the ipairs operator to iterate over the coordinate table:

t = I:CoordOf( 783810 )

-- specify the offset

for i,coord in ipairs(t) do

-- use #t to get the number of table elements

  Printf( "x[%d] = %d\n", i, coord )

-- result: x[1] = 10 x[2] = 20 x[3] = 40

end

 

Related Topics

Pos

Axes

PtInside

Pixel Coordinate Definition

CImage class