I have two sets (sourc
and target
) of points (x,y)
that I would like to align. What I did so far is:
- find the centroid of each set of points
- use the difference between the centroids translations the point in
x
andy
What I would like is to find the best rotation (in degrees) to align the points.
Any idea?
M code is below (with plots to visualize the changes):
# Raw data
## Source data
sourc = matrix(
c(712,960,968,1200,360,644,84,360), # the data elements
nrow=2, byrow = TRUE)
## Target data
target = matrix(
c(744,996,980,1220,364,644,68,336), # the data elements
nrow=2, byrow = TRUE)
# Get the centroids
sCentroid <- c(mean(sourc[1,]), mean(sourc[2,])) # Source centroid
tCentroid <- c(mean(target[1,]), mean(target[2,])) # Target centroid
# Visualize the points
par(mfrow=c(2,2))
plot(sourc[1,], sourc[2,], col="green", pch=20, main="Raw Data",
lwd=5, xlim=range(sourceX, targetX),
ylim=range(sourceY, targetY))
points(target[1,], target[2,], col="red", pch=20, lwd=5)
points(sCentroid[1], sCentroid[2], col="green", pch=4, lwd=2)
points(tCentroid[1], tCentroid[2], col="red", pch=4, lwd=2)
# Find the translation
translation <- tCentroid - sCentroid
target[1,] <- target[1,] - translation[1]
target[2,] <- target[2,] - translation[2]
# Get the translated centroids
tCentroid <- c(mean(target[1,]), mean(target[2,])) # Target centroid
# Visualize the translation
plot(sourc[1,], sourc[2,], col="green", pch=20, main="After Translation",
lwd=5, xlim=range(sourceX, targetX),
ylim=range(sourceY, targetY))
points(target[1,], target[2,], col="red", pch=20, lwd=5)
points(sCentroid[1], sCentroid[2], col="green", pch=4, lwd=2)
points(tCentroid[1], tCentroid[2], col="red", pch=4, lwd=2)
source
and capital letters for vectortarget
) I've not seen this possibility mentioned and explicitely allowed or disallowed. Are you sure that you don't want that better fit? $\endgroup$ – Gottfried Helms Sep 4 '16 at 14:05