TRANSFORMPOINT 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 %TRANSFORMPOINT 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 % E-mail: david.legland@inrae.fr 0031 % Created: 2004-04-06 0032 % Copyright 2004-2024 INRA - TPV URPOI - BIA IMASTE 0033 0034 % parse input arguments 0035 if length(varargin) == 2 0036 var = varargin{1}; 0037 px = var(:,1); 0038 py = var(:,2); 0039 trans = varargin{2}; 0040 elseif length(varargin) == 3 0041 px = varargin{1}; 0042 py = varargin{2}; 0043 trans = varargin{3}; 0044 else 0045 error('wrong number of arguments in "transformPoint"'); 0046 end 0047 0048 0049 % apply linear part of the transform 0050 px2 = px * trans(1,1) + py * trans(1,2); 0051 py2 = px * trans(2,1) + py * trans(2,2); 0052 0053 % add translation vector, if exist 0054 if size(trans, 2) > 2 0055 px2 = px2 + trans(1,3); 0056 py2 = py2 + trans(2,3); 0057 end 0058 0059 % format output arguments 0060 if nargout < 2 0061 varargout{1} = [px2 py2]; 0062 elseif nargout 0063 varargout{1} = px2; 0064 varargout{2} = py2; 0065 end