从+/-笛卡尔输入值计算屏幕坐标

时间:2016-01-29 14:42:31

标签: c# math cartesian-product

我有两个整数值需要在两个方向上转换为屏幕坐标(双精度)。得分值(-50到50)到屏幕(如下面的示例代码中,屏幕300 x 300点/像素)和屏幕到得分值。我已经制定了一个视觉示例:

enter image description here

我还创建了一个小型控制台应用程序,用于测试目前显然无法正常工作的计算,而且我真的在努力学习数学。

class Program
{
    public static void Main(string[] args)
    {
        double screenWidth = 300;
        double screenHeight = 300;

        double MaxX = 50;
        double MinX = -50;

        double MaxY = 50;
        double MinY = -50;

        // x = 150
        // y = 150
        // given these inputs
        CalcScoreToScreen(screenWidth, screenHeight, 0, 0);

        // x = 300
        // y = 0
        // given these inputs
        CalcScoreToScreen(screenWidth, screenHeight, 50, 0);

        // x = 225
        // y = 225
        // given these inputs
        CalcScoreToScreen(screenWidth, screenHeight, 25, -25);

        // x = 75
        // y = 75
        // given these inputs
        CalcScoreToScreen(screenWidth, screenHeight, -25, 25);

        // x = -25
        // y = 25
        // given these inputs
        CalcScreenToScore(screenWidth, screenHeight, 75, 75);

        // x = 25
        // y = -25
        // given these inputs
        CalcScreenToScore(screenWidth, screenHeight, 225, 225);

        Console.ReadLine();
    }

    private static void CalcScoreToScreen(double screenWidth, double screenHeight, int scoreX, int scoreY)
    {
        var screenX = scoreX + screenWidth / 2;

        var screenY = scoreY + screenHeight / 2;

        Console.WriteLine("Screen X: {0} - Score: {1}", screenX, scoreX);
        Console.WriteLine("Screen Y: {0} - Score: {1}", screenY, scoreY);
    }

    private static void CalcScreenToScore(double screenWidth, double screenHeight, double screenX, double screenY)
    {
        int scoreX = Convert.ToInt32(screenX + screenWidth / 2);
        int scoreY = Convert.ToInt32(screenY + screenHeight / 2);

        Console.WriteLine("Score X: {0} - Screen X: {1}", scoreX, screenX);
        Console.WriteLine("Score Y: {0} - Screen Y: {1}", scoreY, screenY);
    }
}

2 个答案:

答案 0 :(得分:1)

您可以将此功能添加到代码中,以将标记值转换为屏幕上的位置。

这是为了计算你的分数到屏幕。

使用screenheight作为最大屏幕高度和屏幕宽度作为用于以图形方式显示标记的最大屏幕宽度。然后,您可以将标记的最大/最小值或50替换为任何变量,因为我已将屏幕宽度/高度变量设为300并使其成为变量。

值扩展到100:-50到50。 对于X: 所以负值覆盖屏幕的前50%,正值覆盖50%

if(Xvalue == 0)
    screenXPos = Screenwidth/2;

else if(Xvalue < 0)
    screenXPos = Screenwidth/2 + Screenwidth/2*Xvalue/50; // this will be negative so subtract from half screenwidth.

else
    screenXPos = Screenwidth*Xvalue/50;

对于Y:

负值从最大屏幕高度到半屏高度正值从半屏高度到零。

if(Yvalue == 0)
    screenYPos = Screenheight/2;

else if(Yvalue < 0)
    screenYPos = screenheight/2 -Screenheight/2*Yvalue/50; // don't want negative value for screen position.

else
    screenYPos = screenheight/2*Yvalue/50;

答案 1 :(得分:1)

private static void CalcScoreToScreen(double screenWidth, double screenHeight, int scoreX, int scoreY, double minX, double maxX, double minY, double maxY)
{
    var screenX = (scoreX - minX)/(maxX-minX)*screenWidth;
    var screenY = screenHeight - (scoreY - minY)/(maxY-minY)*screenHeight;

    Console.WriteLine("Screen X: {0} - Score: {1}", screenX, scoreX);
    Console.WriteLine("Screen Y: {0} - Score: {1}", screenY, scoreY);
}

private static void CalcScreenToScore(double screenWidth, double screenHeight, double screenX, double screenY, double minX, double maxX, double minY, double maxY)
{
    int scoreX = Convert.ToInt32(minX + (maxX-minX)*screenX/screenWidth );
    int scoreY = Convert.ToInt32(minY + (maxY-minY)*(screenHeight - screenY)/screenHeight );

    Console.WriteLine("Score X: {0} - Screen X: {1}", scoreX, screenX);
    Console.WriteLine("Score Y: {0} - Screen Y: {1}", scoreY, screenY);
}