ISPERPENDICULAR Check orthogonality of two vectors. B = isPerpendicular(V1, V2) where V1 and V2 are two 1-by-2 row arrays, returns 1 if the vectors are perpendicular, and 0 otherwise. Also works when V1 and V2 are two N-by-2 arrays with same number of rows. In this case, return a N-by-1 array containing 1 at the positions of perpendicular vectors. Also works when one of V1 or V2 is 1-by-2 and the other one is a N-by-2 array. In this case the result has size N-by-1. B = isPerpendicular(V1, V2, ACCURACY) specifies accuracy of numerical tests, default is 1e-14. Example isPerpendicular([1 2 1], [2 4 2]) ans = 1 isPerpendicular([1 2 1], [1 3 2]) ans = 0 See also vectors2d, isParallel, lines2d
0001 function b = isPerpendicular(v1, v2, varargin) 0002 %ISPERPENDICULAR Check orthogonality of two vectors. 0003 % 0004 % B = isPerpendicular(V1, V2) 0005 % where V1 and V2 are two 1-by-2 row arrays, returns 1 if the vectors are 0006 % perpendicular, and 0 otherwise. 0007 % 0008 % Also works when V1 and V2 are two N-by-2 arrays with same number of 0009 % rows. In this case, return a N-by-1 array containing 1 at the positions 0010 % of perpendicular vectors. 0011 % 0012 % Also works when one of V1 or V2 is 1-by-2 and the other one is a N-by-2 0013 % array. In this case the result has size N-by-1. 0014 % 0015 % B = isPerpendicular(V1, V2, ACCURACY) 0016 % specifies accuracy of numerical tests, default is 1e-14. 0017 % 0018 % 0019 % Example 0020 % isPerpendicular([1 2 1], [2 4 2]) 0021 % ans = 0022 % 1 0023 % 0024 % isPerpendicular([1 2 1], [1 3 2]) 0025 % ans = 0026 % 0 0027 % 0028 % See also 0029 % vectors2d, isParallel, lines2d 0030 % 0031 0032 % ------ 0033 % Author: David Legland 0034 % E-mail: david.legland@inrae.fr 0035 % Created: 2006-04-25 0036 % Copyright 2006-2024 INRA - CEPIA Nantes - MIAJ (Jouy-en-Josas) 0037 0038 % default accuracy 0039 acc = 1e-14; 0040 if ~isempty(varargin) 0041 acc = abs(varargin{1}); 0042 end 0043 0044 % normalize vectors 0045 v1 = normalizeVector(v1); 0046 v2 = normalizeVector(v2); 0047 0048 % adapt size of inputs 0049 n1 = size(v1, 1); 0050 n2 = size(v2, 1); 0051 if n1 ~= n2 0052 if n1 == 1 0053 v1 = v1(ones(n2, 1), :); 0054 elseif n2==1 0055 v2 = v2(ones(n1, 1), :); 0056 else 0057 error('Inputs must either have same size, or one must be scalar'); 0058 end 0059 end 0060 0061 % performs test 0062 b = abs(dot(v1, v2, 2)) < acc;