In my Angular application, I have an array that refers to the coordinates of a polygon. Eg:
[[-1,0], [0,1], [1,0], [0,-1], [-1,0]]
The important bit here is that the that the first and last points are repeated, and actually reference the same 2-length array. This is a result of a plugin I'm using. However, some times the arrays will be created in such a way that the first and last points, while having the same value, are not the same reference.
At a certain point in my Angular application, I need to create a new polygon with the same coordinates as the original, only flipped. My first attempt was this:
var newCoords = angular.copy(polygon.coordinates);
for (var i = 0; i < newCoords.length; i++) {
newCoords[i].reverse();
}
However, in those instances in which the first and last coordinates have the same reference, I was ending up with one of the points being reversed twice.
My understanding was that angular.copy()
creates a deep copy of whatever is passed in, and I shouldn't be experiencing this issue. Clearly this is incorrect, so why? Is there a way to do a truly deep copy of the coordinates array that eliminates that odd reference pairing? I've managed to get around it for now by adding in an additional angular.copy(newCoords[i])
before the reverse()
.