在上图中我显示了两个矩形
-
矩形1其 x 的变化范围为 -900 到 13700,Y 的变化范围为 -600 到 6458
-
矩形2其坐标 X 可以在 0 到 3000 之间变化,y 可以在 0 到 2000 之间变化
Also: 矩形2其起点位于左上角位置(0,0),而矩形1有起点(宽度/2,高度/2)。
我需要做什么: 转换一个点矩形1到点矩形2使用缩放或平移。
那么,比例因子应该是多少x
and y
坐标以便变换坐标矩形1 to 矩形2?
If:
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);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)