计算坐标

时间:2015-08-18 08:58:21

标签: c# coordinates

我的任务是坐标计算,其定义如下:

  1. 创建以下数据结构(classstruct):
    cartCoordinate { double X; double Y, double Z }
  2. 从1创建4个随机点。
  3. 计算以下内容:

    • 2点之间的3D距离
    • 四边形的范围
  4. 我已经编写了代码,但仍然没有得到正确的结果。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication2
    {
        class cartCoordinate
        {
            double x, y, z;
            cartCoordinate(double a ,double b ,double c)
            {
                x = a;
                y = b;
                z = c;
            }
            static void Main(string[] args)
            {
                double X=1, Y=2, Z=3;   // Randome numbers
                Console.WriteLine("First random points are    " + X + " " + Y + " " + Z);
               cartCoordinate obj1 = new cartCoordinate(X,Y,Z);
    
               X = 8; Y = 12; Z = 5;   // Randome numbers
               Console.WriteLine("Second random points are   " + X + " " + Y + " " + Z);
               cartCoordinate obj2 = new cartCoordinate(X, Y, Z);
    
               X = 10; Y = 2 ; Z = 6;   // Randome numbers
               Console.WriteLine("Third random points are    " + X + " " + Y + " " + Z);
                cartCoordinate obj3 = new cartCoordinate(X, Y, Z);
    
                X = 19; Y = 12; Z = 9;   // Randome numbers
               Console.WriteLine("Forth random points are    " + X + " " + Y + " " + Z);
               cartCoordinate obj4 = new cartCoordinate(X, Y, Z);
    
               Console.WriteLine("Choose any two point");
               int a, b;
               a = int.Parse(Console.ReadLine());
               b = int.Parse(Console.ReadLine());
               switch ((a * 10) + b)
               {
                   case 12: Console.WriteLine("Distance is " + Math.Sqrt(Math.Pow((obj2.x - obj1.x), 2) + Math.Pow((obj2.y - obj1.y), 2) + Math.Pow((obj2.z - obj1.z), 2)));
                       break;
                   case 13: Console.WriteLine("Distance is " + Math.Sqrt(Math.Pow((obj3.x - obj1.x), 2) + Math.Pow((obj3.y - obj1.y), 2) + Math.Pow((obj3.z - obj1.z), 2)));
                       break;
                   case 23: Console.WriteLine("Distance is " + Math.Sqrt(Math.Pow((obj3.x - obj2.x), 2) + Math.Pow((obj3.y - obj2.y), 2) + Math.Pow((obj3.z - obj2.z), 2)));
                       break;
               }
               Console.Read();
            }
        }
    }
    

2 个答案:

答案 0 :(得分:0)

也许您应该创建自己的类/结构Vector3D并定义方法float GetDistance(Vector3D va, Vector3D va),然后创建此类/结构的实例并使用此方法。

class Vector3D
{
    public float X;
    public float Y;
    public float Z;

    public static float GetDistance(Vector3D va, Vector3D vb)
    {
        if ((va == null) || (vb == null))
        {
            Console.WriteLine("Params are null.");
            return 0f;
        }

        return Math.Sqrt((va.X * va.X  - vb.X * vb.X) + (va.Y * va.Y  - vb.Y * vb.Y) + (va.Z * va.Z  - vb.Z * vb.Z));
    }
}

答案 1 :(得分:0)

问题在于问题。用这个替换你的开关

switch ((a * 10) + b) {
    case 12:
        Console.WriteLine("Distance is " + Math.Sqrt(Math.Pow((obj2.x - obj1.x), 2) + Math.Pow((obj2.y - obj1.y), 2) + Math.Pow((obj2.z - obj1.z), 2)));
        break;
    case 13:
        Console.WriteLine("Distance is " + Math.Sqrt(Math.Pow((obj3.x - obj1.x), 2) + Math.Pow((obj3.y - obj1.y), 2) + Math.Pow((obj3.z - obj1.z), 2)));
        break;
    case 23:
        Console.WriteLine("Distance is " + Math.Sqrt(Math.Pow((obj3.x - obj2.x), 2) + Math.Pow((obj3.y - obj2.y), 2) + Math.Pow((obj3.z - obj2.z), 2)));
        break;
    default:
        Console.WriteLine("None of them");
}