简体   繁体   English

如何初始化二维对象

[英]How to initialize 2D object

I'm trying to figure out how to start a two dimensional array of an object.我试图弄清楚如何开始一个对象的二维数组。 I have point class and I want to create Rectangle class that build from 2D arrays of points.我有点类,我想创建从二维点数组构建的 Rectangle 类。 I want to initialize the public Point[,] StartPoint somting like this = new [,]Point();我想像这样初始化public Point[,] StartPoint somting = new [,]Point(); What the right way to do it?正确的方法是什么?

{
class Point
{
    public int X { get; set; }
    public int Y { get; set; }

    public Point()
    {
        X = 0;
        Y = 0;
    }
    public Point(int x , int y)
    {
        X = x;
        Y = y;
    }
}
}

Rectangele class :矩形类:

{
class SRectangle
{
    public Point[,] StartPoint;
    public int Rows ;
    public int Cols ;



    public SRectangle(Point start , int row, int col)
    {
        Rows = row;
        Cols = col;
        for (int i = 0; i <= Rows; i++)
        {
            for (int j = 0; j <= Cols; j++)
            {
                StartPoint[i, j] = new Point(Rows + i, Cols + j);
            }
        }
    }
}

}

You don't need a multidimensional array to keep a store of points, they themselves store x and y.您不需要多维数组来存储点,它们本身存储 x 和 y。 You only need a simple array f points for a rectangle, But I see your concern, what you seem to want is:你只需要一个矩形的简单数组 f 点,但我看到你的担心,你似乎想要的是:

but because it is class I can't write something like this 'public Point[,] StartPoint = new Point,;但因为它是类我不能写这样的'public Point[,] StartPoint = new Point,;

You want to access points using an index as if you were storing X and Y combinations.您希望使用索引访问点,就像存储 X 和 Y 组合一样。 But You aren't, you're storing an array of objects which have information about X and Y's however there is a way to get the point by (x,y)index但你不是,你正在存储一个对象数组,其中包含有关 X 和 Y 的信息,但是有一种方法可以通过 (x,y)index

  1. Insist on using a 2d array, in which Points[x,y] will have an object with the same X and Y ie you might as well have stored int[,] and it wouldn't have mattered.坚持使用 2d 数组,其中Points[x,y]将有一个具有相同 X 和 Y 的对象,即您可能已经存储了int[,]并且这无关紧要。

  2. Treat the array as a 2d but really having a 1d array.将数组视为 2d 但实际上具有 1d 数组。

//my rectangle class
class Rectangle
{
    public Point[,] Points;
    public int Rows ;
    public int Cols ;



    public Rectangle(Point start, int row, int col)
    {
        Rows = row;
        Cols = col;
        Points = new Point[Rows, Cols];
        for (int i = 0; i <= Rows; i++)
        {
            for (int j = 0; j <= Cols; j++)
            {
                Points[i, j] = new Point(Rows + i, Cols + j);
            }
        }
        //now use it like Points[x, y]
    }
}
//my rectangle class
class Rectangle
{
    public Point[] Points;
    public int Rows;
    public int Cols;

    public Rectangle(Point start, int row, int col)
    {
        Rows = row;
        Cols = col;
        Points = new Point[Rows * Cols];
        for (int i = 0; i <= Rows; i++)
        {
            for (int j = 0; j <= Cols; j++)
            {
                Points[(i * Cols) + j] = new Point(Rows + i, Cols + j);
            }
        }
    }
        
    //return the point if it exists else null
    public Point? GetPoint(int x, int y)
    {
       int index = (i * Cols) + j;
       if (index < Points.Length)
           return Points[index];
       return null;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM