将一个矩形的坐标转换为另一个矩形

Yog*_*esh 5 c# geometry transformation computational-geometry coordinate-transformation

在此输入图像描述

在上图中我展示了两个矩形

  • 矩形1,其x可以在-900到13700之间变化,Y可以在-600到6458之间变化
  • 矩形2的坐标X可以在0到3000之间变化,y可以在0到2000之间变化

另外: 矩形2的起点位于左上角位置(0,0),而矩形1的起点(宽度/ 2,高度/ 2).

我需要做的是:使用缩放或平移将矩形1的点转换为矩形2的点.

那么,为了将矩形1的坐标变换为矩形2,应该使用比例因子xy坐标?

Mat*_*son 7

如果:

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;
Run Code Online (Sandbox Code Playgroud)

以浮点计算并在之后转换回整数,以避免可能的溢出.


在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);
}
Run Code Online (Sandbox Code Playgroud)