【发布时间】:2015-09-26 06:33:27
【问题描述】:
我会在没有特定编程语言的情况下提出这个问题。
我有一个画布控件,可以进行缩放和平移视图(拖动)。我已经在画布上加载了一张图片,我的目标是在画布上单击鼠标时获取坐标。
当我点击画布时的事件给了我鼠标的坐标 (mouse_x,mouse_y)、平移量度 (delta_x, delta_y) 和缩放 (1.0 表示不缩放, x > 1.0 表示再缩放, x
当我单击画布而不移动图像并进行缩放时,图像坐标与鼠标坐标相同。好的。
real_x = mouse_x;
real_y = mouse_y;
当我在图像移动但没有缩放的情况下单击画布时,图像坐标是添加平移度量的鼠标坐标。
real_x = mouse_x + delta_x;
real_y = mouse_y + delta_y;
问题是当我进行缩放时,我不知道使用缩放值获取原始坐标的公式是什么。
编辑:
这 3 个解决方案不起作用。
代码:
void DrawLineOnDoubleClick(object sender, RoutedEventArgs e)
{
var mouse_x = this._mouseDownPos.X;
var mouse_y = this._mouseDownPos.Y;
var delta_x = this.TranslateX;
var delta_y = this.TranslateY;
var scale = this.Zoom;
var windowwidth = 1000;
var windowheight = 500;
// OPTION 1. IT DOES NOT WORK
var real_x = windowwidth / 2 - (windowwidth / 2 + delta_x) * scale + mouse_x;
var real_y = windowheight/ 2 - (windowheight/ 2 + delta_y) * scale + mouse_y;
// OPTION 2. IT DOES NOT WORK
var real_x = windowwidth / 2 - windowwidth / 2 * scale + delta_x * scale + mouse_x;
var real_y = windowheight / 2 - windowheight / 2 * scale + delta_y * scale + mouse_y;
// OPTION 3. IT DOES NOT WORK
var real_x = windowwidth / 2 - windowwidth / 2 * scale + delta_x * scale + mouse_x * scale;
var real_y = windowheight / 2 - windowheight / 2 * scale + delta_y * scale + mouse_y * scale;
// Draw line from last clicked to new clicked position
...
}
例如,使用解决方案 2,当我以 0.4 的比例(缩放)单击图像的第一个角到图像的对角时,线被绘制在远离鼠标位置的位置(当我没有缩放 (1.0) 且无需平移(拖动)即可正确绘制线条。
【问题讨论】:
标签: c# canvas coordinate-transformation