-1

I have a

Rectangle rect = new Rectangle(
    (int)(someArray[z].rectangleObject.X +
    ((double)widthOfCell / 2) -
    ((double)targetWidth / 2)),

    (int)(someArray[z].rectangleObject.Y +
    (double)(heightOfCell / 2) -
    (double)(targetHeight / 2)),

    targetWidth,

    targetHeight);

If targetWidth and targetHeight increase by 20 per cent, ie

targetWidth *= 1.2
targetHeight *= 1.2

How does

(int)(someArray[z].rectangleObject.X +
    ((double)widthOfCell / 2) - ((double)targetWidth / 2))

and

(int)(someArray[z].rectangleObject.Y +
    (double)(heightOfCell / 2) - (double)(targetHeight / 2))

have to change for the center to stay in the same place?

Note that I am not changing any of the other values in the calculation. someArray[z].rectangleObject.X, someArray[z].rectangleObject.Y, widthOfCell and heightOfCell all remain the same.

Vitalis Hommel
  • 990
  • 2
  • 8
  • 20

1 Answers1

0

The formulae you're using to compute the X and Y for the location appear to already include each expression needed to compute the center of the rectangle. I.e.

(int)(someArray[z].rectangleObject.X + ((double)widthOfCell / 2)

and

(int)(someArray[z].rectangleObject.Y + (double)(heightOfCell / 2)

for the X and Y of the center, respectively.

Those formulae go on to subtract half the width and height of the rectangle, to produce the actual location for the rectangle.

If you change only the width and height, the computed value of the above two expressions will remain the same, and so the center of the computed rectangle will remain the same as well.

In other words, if all you're changing is the width and height of the rectangle, all you need to do is calculate the entire location expressions you've already got. Your original code to calculate the rectangle works for that purpose:

Rectangle rect = new Rectangle(
    (int)(someArray[z].rectangleObject.X + ((double)widthOfCell / 2) -
        ((double)targetWidth / 2)),
    (int)(someArray[z].rectangleObject.Y + (double)(heightOfCell / 2) -
        (double)(targetHeight / 2)),
    targetWidth,
    targetHeight);

Based on your comment, it seems that this does in fact address your question.

Community
  • 1
  • 1
Peter Duniho
  • 68,759
  • 7
  • 102
  • 136