将一个矩形的坐标转换为另一个矩形
在上图中我展示了两个矩形
- 矩形1的x可以在-900到13700之间变化,Y可以在-600到6458之间变化
- 矩形2的坐标X可以在0到3000之间变化,y可以在0到2000之间变化
另外: 矩形2的起始点位于左上角位置(0,0),而矩形1的起始点(宽度/ 2,高度/ 2)。
我需要做的是 :使用缩放或平移将矩形1的点转换为矩形2的点。
那么,为了将矩形1的坐标变换为矩形2 , x
和y
坐标应该是什么缩放因子?
如果:
Rectangle 1 has (x1, y1) origin and (w1, h1) for width and height, and Rectangle 2 has (x2, y2) origin and (w2, h2) for width and height, then Given point (x, y) in terms of Rectangle 1 coords, to convert it to Rectangle 2 coords: xNew = ((x-x1)/w1)*w2 + x2; yNew = ((y-y1)/h1)*h2 + y2;
以浮点计算并在之后转换回整数,以避免可能的溢出。
在C#中,上面的内容类似于:
PointF TransformPoint(RectangleF source, RectangleF destination, PointF point) { return new PointF( ((point.X - source.X) / source.Width) * destination.Width + destination.X, ((point.Y - source.Y) / source.Height) * destination.Height + destination.Y); }
试试这个:
rectangelOne.x = rectangelTwo.x + rectangelTwo.width / 2; rectangelOne.y = rectangelTwo.y + rectangleTwo.height / 2;
如果您的rectangleOne的枢轴是正确的,它应该自动居中,其他人这样添加:
rectangleOne.x -= rectangleOne.width / 2; rectangleTwo.y -= rectangleOne.height / 2;
希望这可以帮助。
float transformValue (float x,float A,float B, float C, float D){ return C + ((xA) * (DC)/(BA)); }