Skip to content

Instantly share code, notes, and snippets.

@knedlsepp
Last active August 29, 2015 14:17
Show Gist options
  • Save knedlsepp/3e37f147cb4c94ae6223 to your computer and use it in GitHub Desktop.
Save knedlsepp/3e37f147cb4c94ae6223 to your computer and use it in GitHub Desktop.
Computing the distance of points and an affine space
function d = distancePointsAffineSpace(Points, Space)
lengths = @(X) sqrt(sum(X.^2,2));
d = lengths(Points - projectPointsOntoAffineSpace(Points, Space));
end
function [projectedPoints, coordinates] = projectPointsOntoAffineSpace(Points, Space)
%coordinates are with respect to an orthonormal base located in Space(1,:)
dirVecs = bsxfun(@minus, Space(2:end,:), Space(1,:));
% Orthonormalize the direction vectors;
dirVecs = orth(dirVecs.').';
% Compute coordinates with respect to (Space(1,:),dirVecs)-coordinates
coordinates = bsxfun(@minus, Points, Space(1,:))*dirVecs.';
% Compute projection
projectedPoints = bsxfun(@plus, coordinates*dirVecs, Space(1,:));
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment