将图形坐标转换为c#中的数学坐标

时间:2014-02-28 12:00:01

标签: c# math graphical-programming

我想将图形x,y坐标x,y转换为数学协调

(在这张图片中,您可以看到图形x,y和数学x,y之间的差异

sketch of the situation

通过电子事件

获得的图形x和图形y
         int graphicalx;
        int graphicaly;   
        graphicalx = e.X;
        graphicaly = e.Y;

并且他们在表单中显示两个标签只是应该在表单上移动鼠标

现在将graphx,y转换为数学x,y的公式为:

图形x =数学x + Alfa

图形y = - 数学y + Beta

现在Alfa和Beta获得了这个:

您获得了计算机分辨率: 我的样本是: 1600 * 800

alfa = 1600/2 = 800

beta = 800/2 = 450

最后: alfa = 800 beta = 450

现在我的程序运行良好,问题出在哪里?

    private void Form1_MouseMove(object sender, MouseEventArgs e)
    {
        int graphicalx;
        int graphicaly;
        int mathematicalx;
        int mathematicaly;


        graphicalx = e.X;
        graphicaly = e.Y;


             if (graphicalx > 0)
        {
            graphicalx = graphicalx * -1; //if graphicalX was positive do it negative

        }
        if (graphicaly > 0)
        {
            graphicaly = graphicaly * -1; //if it graphicalY was positive do it negative

            }

        if (graphicalx < 0)
        {
            graphicalx = graphicalx * +1; // if graphicalX was negative do it positive
        }
        if (graphicaly < 0)
        {
            graphicaly = graphicaly * +1; // if graphicalY was negative do it positive
        }


       mathematicalx = graphicalx + 800; // the formula for obtain the mathematical x 
       mathematicaly = graphicaly * -1 + 450; // the formula for obtain the mathematical y 


        label1.Text = "X = " +mathematicalx.ToString();
        label3.Text = "Y = " + mathematicaly.ToString();

    }

表单1属性:

Windows状态=最大化

FormBorderStyle =无

2 个答案:

答案 0 :(得分:2)

第一个突出的问题是你的反向方程式实际上不是反向的,你需要减去这些值,而不是添加它们。试试这个:

mathematicalx = graphicalx - 800; // the formula for obtain the mathematical x 
mathematicaly = (graphicaly - 450) * -1; // the formula for obtain the mathematical y 

答案 1 :(得分:0)

根据图像和惯例,有一些测试用例,角点和中点应满足对应关系:

graphical     |   mathematical
-------------------------------
 (   0,   0)  |    (-800,  450)
 (   0, 900)  |    (-800, -450)
 (1600,   0)  |    ( 800,  450)
 (1600, 900)  |    ( 800, -450)
 ( 800, 450)  |    (   0,    0)

使它成为

mathematical.x = graphical.x - 800
mathematical.y = 450 - graphical.y