I have a rectangle texture that I want to resize by pinching on the screen.
I have been able to resize it by increasing the width and height at the same time, but I would like to do this based on the actual current horizontal and vertical deltas, so the result is not always a square. My math is not great and I get lost while trying to do those calculations. Could anybody help? Thanks.
This is my code:
while (TouchPanel.IsGestureAvailable)
{
GestureSample gesture = TouchPanel.ReadGesture();
if (gesture.GestureType == GestureType.Pinch)
{
// Current positions
Vector2 a = gesture.Position;
Vector2 b = gesture.Position2;
float dist = Vector2.Distance(a, b);
// Prior positions
Vector2 aOld = gesture.Position - gesture.Delta;
Vector2 bOld = gesture.Position2 - gesture.Delta2;
float distOld = Vector2.Distance(aOld, bOld);
if (dist > distOld)
{
width += (int)(dist - distOld);
heigth+= (int)(dist - distOld);
}
else
{
width -= (int)(distOld - dist);
heigth -= (int)(distOld - dist);
}
rect = new Texture2D(GraphicsDevice, width, heigth);
Color[] data = new Color[width * heigth];
for (int i = 0; i < data.Length; ++i) data[i] = Color.Chocolate;
rect.SetData(data);
}
}