Apply an affine transform to a point or a point set. PT2 = transformPoint(PT1, TRANSFO); Returns the result of the transformation TRANSFO applied to the point PT1. PT1 has the form [xp yp], and TRANSFO is either a 2-by-2, a 2-by-3, or a 3-by-3 matrix, Format of TRANSFO can be one of : [a b] , [a b c] , or [a b c] [d e] [d e f] [d e f] [0 0 1] PT2 = transformPoint(PT1, TRANSFO); Also works when PTA is a N-by-2 array representing point coordinates. In this case, the result PT2 has the same size as PT1. [X2, Y2] = transformPoint(X1, Y1, TRANS); Also works when PX1 and PY1 are two arrays the same size. The function transforms each pair (PX1, PY1), and returns the result in (X2, Y2), which has the same size as (PX1 PY1). See also: points2d, transforms2d, translation, rotation
0001 function varargout = transformPoint(varargin) 0002 % Apply an affine transform to a point or a point set. 0003 % 0004 % PT2 = transformPoint(PT1, TRANSFO); 0005 % Returns the result of the transformation TRANSFO applied to the point 0006 % PT1. PT1 has the form [xp yp], and TRANSFO is either a 2-by-2, a 0007 % 2-by-3, or a 3-by-3 matrix, 0008 % 0009 % Format of TRANSFO can be one of : 0010 % [a b] , [a b c] , or [a b c] 0011 % [d e] [d e f] [d e f] 0012 % [0 0 1] 0013 % 0014 % PT2 = transformPoint(PT1, TRANSFO); 0015 % Also works when PTA is a N-by-2 array representing point coordinates. 0016 % In this case, the result PT2 has the same size as PT1. 0017 % 0018 % [X2, Y2] = transformPoint(X1, Y1, TRANS); 0019 % Also works when PX1 and PY1 are two arrays the same size. The function 0020 % transforms each pair (PX1, PY1), and returns the result in (X2, Y2), 0021 % which has the same size as (PX1 PY1). 0022 % 0023 % 0024 % See also: 0025 % points2d, transforms2d, translation, rotation 0026 % 0027 0028 % --------- 0029 % author : David Legland 0030 % INRA - TPV URPOI - BIA IMASTE 0031 % created the 06/04/2004. 0032 % 0033 0034 % HISTORY 0035 % 25/04/2005 : support for 2D arrays of points (px, py, trans). 0036 0037 % parse input arguments 0038 if length(varargin) == 2 0039 var = varargin{1}; 0040 px = var(:,1); 0041 py = var(:,2); 0042 trans = varargin{2}; 0043 elseif length(varargin) == 3 0044 px = varargin{1}; 0045 py = varargin{2}; 0046 trans = varargin{3}; 0047 else 0048 error('wrong number of arguments in "transformPoint"'); 0049 end 0050 0051 0052 % apply linear part of the transform 0053 px2 = px * trans(1,1) + py * trans(1,2); 0054 py2 = px * trans(2,1) + py * trans(2,2); 0055 0056 % add translation vector, if exist 0057 if size(trans, 2) > 2 0058 px2 = px2 + trans(1,3); 0059 py2 = py2 + trans(2,3); 0060 end 0061 0062 % format output arguments 0063 if nargout < 2 0064 varargout{1} = [px2 py2]; 0065 elseif nargout 0066 varargout{1} = px2; 0067 varargout{2} = py2; 0068 end