HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Interface for a transformable grid

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
interfacetransformableforgrid

Problem

I am working on an XNA project, and since procedural generation is likely to be used, I wanted to eventually create a helper class to work with data to facilitate that.

Design Goal:

Originally, I wanted to generate and modify a heightmap in a simple but efficient manner. The most straightforward approach to handle this would be a with a float[,].

Right now, I have a Grid which simply holds the data in a more convenient manner, but I want to introduce a way to transform it somehow - slicing, rotations, etc. The ability to add and multiply different heightmaps together would be beneficial as well.

My Code:

Knowing that C# is optimized for single-dimensional arrays, I decided to make a simple wrapper class that contains an internal jagged array, but have it look and behave like a 2-dimensional array.

I first made it into an interface because doing so allows me to implement it however I want, which allows for future optimizations which won't break any dependent code:

// Presents itself as a simplified 2-dimensional array
public interface IGrid : IEnumerable, IEnumerable
{
    Point Size { get; }
    T this[int x, int y] { get; set; }
}


And the current (simplified) implementation is:

// Functions as a jagged array under the hood
public class Grid : IGrid
{
    private readonly T[][] _values;

    // Implement IGrid
    public Point Size { get; private set; }
    public T this[int x, int y]
    {
        get { return _values[y][x]; }
        set { _values[y][x] = value; }
    }

    // Constructor
    public Grid(int x, int y)
    {
        Size = new Point(x, y);
        _values = new T[y][];
        for (int i = 0; i < y; i++)
            _values[i] = new T[x];
    }

    // Provide IEnumerator, override ToString(), etc...
}


My approach for the transformation bits is similar:

```
public interface ITransformable
{
T Slice(int x, int y, int width, int height);
T Slice(Rectangle value);
T Transpose();
T Flip(bool horizontal, b

Solution

I think you should use aggregation.

It's either:

class Mesh : ITransformable
{
    public Mesh(IGrid grid, ....)
    {
        Grid = grid;
        ......
    }

    public IGrid Grid { get; private set; }
    .....
}


or:

class Transformer : ITransformer
{
    public T Slice(IGrid, int x, int y, int width, int height)
    {
        ...
    }
    ....
}


It's hard to tell which is better without good understanding of your design (which I don't have). But generally I would prefer the second option.

Also, your IGrid implementation looks really weird. You are saying that you want to implement 2D array using 1D array, yet you use T[][] _values; in your implementation. So, what's the point? There is also a System.Windows.Size class, so I don't understand why would you want to use a Point.

Code Snippets

class Mesh<T> : ITransformable<T>
{
    public Mesh(IGrid<T> grid, ....)
    {
        Grid = grid;
        ......
    }

    public IGrid<T> Grid { get; private set; }
    .....
}
class Transformer<T> : ITransformer<T>
{
    public T Slice(IGrid<T>, int x, int y, int width, int height)
    {
        ...
    }
    ....
}

Context

StackExchange Code Review Q#55314, answer score: 3

Revisions (0)

No revisions yet.