I'm trying to implement a shift+resize feature which just scales the shape instead of changing different amounts of x and y. I'm not really sure how to do it.
if (Control.ModifierKeys == Keys.Shift)
ptCur = this.Controller.View.SnapPointToGrid(e.X, e.Y);
this.curBounds = MoveAnchorPoint(this.startBounds, this.handleHit, ptCur);

This code would check if the shift key is pressed and then snap the coordinates of the mouse to the nearest grid. I need to make it preserve a ratio though. so I tried replacing (e.X, e.Y) with (e.X, e.X-this.curBounds.X + e.Y) but that didn't work as nicely.

Re: Visual C# General more of a math question

Pradeep C

Sorry but I didn't understand you quite clearly on what you are trying to do.

Re: Visual C# General more of a math question

Jeremy Filburn

It is a simple ratio. If we make X the dominate axis, and we wanted a 1:2 ratio, then whatever X is, Y is double, or X is half Y.

Is that what your after

Re: Visual C# General more of a math question


No because, those are coordinates not scaling factors. I want things to be preserve their current aspect ratio. I can't just double the Y coordinates.

Re: Visual C# General more of a math question


It depends on how you have it set up. Can the user shift-drag any one of the four corners You may need to do special cases for each corner. Also, do you want a dominant axis (x or y), or would you rather have it use the smaller or larger of the two, or perhaps some other formula (distance)

Here's an example (uncompiled/untested), assuming the user is dragging the lower-right corner, using a dominant x-axis:

Code Snippet

RectangleF originalBounds; // the bounds before dragging started

PointF currentMousePoint;

float newWidth = currentMousePoint.X - originalBounds.Left;

float newHeight = (newWidth / originalBounds.Width) * originalBounds.Height;

I'm assuming it's a formula like the newHeight one there that is what you're looking for. It's pretty simple, just look at how the ratio that the width has changed by, and apply it to the height as well.

Edit: oh, and if you don't want newWidth and newHeight as outputs, you can easily get them into X and Y coordinates as :

Code Snippet

float newX = originalBounds.Left + newWidth;

float newY = originalBounds.Top + newHeight;

Re: Visual C# General more of a math question

Jeremy Filburn

I am not implying that you use a 1:2 ratio, it is an example.

The key here is to understand what an aspect ratio is. For an image, it is measured by X:Y or X/Y.

So when the box is drawn, you know X & Y, divide X by Y to get the ratio in decimal form.

Example: a 4x6 picture is expressed as a 4:6 (2:3) or 4/6 ratio. .666666 or .67 or whatever precision you want.

Letí»s assume that X=4 and Y=6, then user holds the shift key and makes your box bigger.

Now letí»s assume the new Y size is 10. To figure out what X is with an aspect ratio of 4:6, multiply 10 by .667 and the new X size (6.667) will be the result.

If X = 4, Y = 6, new Y = 12, new X = (12 * .666666667) = 8.00000004 (round to 8). This new size is still a 4:6 or 2:3 ratio.

All you really need to do is figure out which axis is growing or shrinking, and adjust the other accordingly! It does not matter if it is X or Y, either one should make the other change!

Re: Visual C# General more of a math question

Peter N Roth

Filburn is on the right track

perhaps easier to see if the ratio is expressed (x2 - x1) / (y2 - y1)