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

在上图中我展示了两个矩形
另外: 矩形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;
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)