I'm trying to condense this by wrapping it in a loop:
if (pos.X + pixelRadius < 0) {
pos.X = bounds.Width - pixelRadius;
} else if (pos.X + pixelRadius > bounds.Width) {
pos.X = pixelRadius;
}
if (pos.Y + pixelRadius < 0) {
pos.Y = bounds.Heigh - pixelRadius;
} else if (pos.Y + pixelRadius > bounds.Height) {
pos.Y = pixelRadius;
}
My first instinct was to do something like this:
foreach (float coord in new float[] { pos.X, pos.Y }) {
float upperBound = (coord == pos.X ? bounds.Width : bounds.Height);
if (coord + pixelRadius < 0) {
coord = upperBound - pixelRadius;
} else if (coord + pixelRadius > upperBound) {
coord = pixelRadius;
}
}
But of course then I get the error message:
Cannot assign to 'coord' because it is a 'foreach iteration variable'
Is there any way I can wrap this code in a loop? Or maybe it's not worth the effort, and it's more readable to leave in the first form.
For those who are curious: yes, this is implementing wrap-around.