Reputation: 51
I have been working on this for a few days and have managed to totally confuse myself. I get a mouse click from a game ( not in windows forms ) , I have the desktop size, source window size and origin the mouse click in pixels of the source and the actual click coordinates . two things I am trying to do
1-since the source window is arbitrary I need to normalize that point from the smallest the window can be to the largest so that point will be relatively the same place when the window is a different size ..that I think I have done with normalization. So I end up with a relative point for that game window and a normalized x,y. 2- Transpose the same point to different screen locations/windows ( this is where I am confusing myself or not getting it ) 3- while I am at it the ability to transform back and forth
I have attached a picture how can I given a point with known transfer that point to another window different size and get a real mouse coordinate from it for the new window not just the relative size from the normalization.
I thought I could use the matrix class but reading through it my eyes just bled it has been too many years since college. But it looks like the easiest ..
So looking at the pic how do i get relative and actual mouse clicks for those 2 windows on the left that are the exact same spot or you know what I mean ? and yes before all the police chime in I have read about a dozen articles in here on this and still am not getting it ..One thing to note have been very careful to keep the aspect ratio the same in the game and all the windows involved i/e game is some fraction of 1920x1080 same as my monitors. here is my normalization formula for x and y with the min and max of the possible game window :
normalizedX = ((double)valX - (double)minX) / ((double)maxX - (double)minX);
normalizedY = ((double)valY - (double)minY) / ((double)maxY - (double)minY);
denormalizedX = normalizedX * (maxX - minX) + minX;
denormalizedY = normalizedY * (maxY - minY) + minY;
Upvotes: 0
Views: 86
Reputation: 51
This was Easy once I ultimately got my head around it thanks for the direction I was making this way to hard ..
//Scales a point between two window sizes
//where both sizes have the same WxH ratio i.e. 16:9
//windows as points just to avoid long constructor
public Point ScalePoint(Point oldClick, Point oldWin, Point newWin)//oldClick is relative to oldWin 0x0
var ratioX = (double)newWin.X / oldWin.X;
var ratioY = (double)newWin.Y / oldWin.Y;
var ratio = Math.Min(ratioX, ratioY);
int newX = (int)(Math.Round((oldClick.X * ratio), 0, MidpointRounding.ToEven)); //rounding to get exact
int newY = (int)(Math.Round((oldClick.Y * ratio), 0, MidpointRounding.ToEven));
Point newPoint = new Point(newX, newY);
return newPoint; ..new point is relative to newWin 0x0
}
Thanks to : efundies.com for getting me past the scale windows portion. This is just a variation on that . To get the screen coords is just add and subtract from this point using the new window's position.
Upvotes: 0