【问题标题】:convert Graphical coordinates To Mathematical coordinates in c#在c#中将图形坐标转换为数学坐标
【发布时间】:2014-02-28 12:00:00
【问题描述】:

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

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

e事件得到的图形x和图形y

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

它们在表单中通过两个标签显示只是应该在表单上移动鼠标

现在将图形 x,y 转换为数学 x,y 的公式是这样的:

图形 x = 数学 x + Alfa

图形 y = - 数学 y + Beta

现在Alfa和Beta通过这个获得:

你得到你的电脑分辨率: 样品矿是:1600 * 800

阿尔法 = 1600 /2 = 800

测试版 = 800/2 = 450

最后: 阿尔法 = 800 贝塔 = 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();

    }

Form 1 属性:

窗口状态 = 最大化

FormBorderStyle = 无

【问题讨论】:

标签: c# math graphical-programming


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    要根据图像和通常的约定有一些测试用例,角和中点应该满足对应关系:

    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
    

    【讨论】:

    • 哇!感谢@lutzl,它运行良好,但只有 3 个部分(TopRight,DownRight,DownLeft),但在 TOP LEFT x 应该是正数,但它是负数,你可以在这里看到程序:up2www.com/uploads/139359228131.zip
    • 你从哪里得到左上角的要求?在笛卡尔坐标系的第二和第三象限中,水平x坐标为负数。
    • 哦,是的,你是对的! ,我错了,在第二象限中,x为负,y为正
    • 请问另一个问题:如何在程序中绘制数学坐标轴??
    • 在普通的 winform 中,只需在绘图事件中绘制相应的线条即可。也许还添加一些刻度线,...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多