user2837961
user2837961

Reputation: 1555

Which collection to use for fixed list of values

I have data as below which I want to populate in a collection depending on the direction of the angle. I want the collection to have the sum value of the quantity. Which collection should I use?

double angle = 20;
double quantity = 200;

double angle = 20;
double quantity = 250;

double angle = 30;
double quantity = 300;

double angle = 40;
double quantity = 400;

Till now I have attempted to create a dictionary with an enum as below. I am sure sure if this is a good idea. Is there a bettwer way

public enum Direction
{
    NorthToNortheast,
    NortheastToEast,
    EastToSoutheast,
    SoutheastToSouth,
    SouthToSouthwest,
    SouthwestToWest,
    WestToNorthwest,
    NorthwestToNorth
}

In my class

Dictionary<Direction, double> elements = new Dictionary<Direction, double>();

Then for each value I am populating the dictionary

if (angle >= 0 && angle < 45)
    elements[Direction.NorthToNortheast] += quantity;
else if (angle >= 45 && angle < 90)
    elements[Direction.NortheastToEast] += quantity;

Upvotes: 1

Views: 367

Answers (2)

vgru
vgru

Reputation: 51204

You could simplify the direction lookup by keeping the directions and angles in a list, something like:

static readonly IReadOnlyList<CardinalDirection> _directionsByAngle = InitDirections();

// map each starting angle to a direction,
// sorted descending (largest angle first)
static IReadOnlyList<CardinalDirection> InitDirections()
{
    return new List<CardinalDirection>
    {
        new CardinalDirection (0, Direction.NorthToNortheast),
        new CardinalDirection (45, Direction.NortheastToEast),
        ...
    }
    .OrderByDescending(d => d.StartAngle)
    .ToList();
}

// find the first (largest) d.StartAngle such that angle >= d.StartAngle
static CardinalDirection GetDirectionForAngle(double angle)
{
    return _directionsByAngle
        .Where(d => angle >= d.StartAngle)
        .FirstOrDefault();
}

Depending on what you want to do with the value, keeping the total in the dictionary like you are using now would work fine. With the snipped above, you would have:

var direction = GetDirectionForAngle(angle % 360);
elements[direction.Direction] += quantity;

Upvotes: 1

Tamerlan Rustambayli
Tamerlan Rustambayli

Reputation: 55

You can use each enum values as a Property in a collector class:

//The class is describing your items
class Item
    {
        public double Angle;
        public double Quantity;

        public Item(double angle, double quantity)
        {
            Angle = angle;
            Quantity = quantity;
        }       
    }

//Collector class
class Element
{
    public Element()
    {
        NorthToNortheast.Quantity = 200;
        NorthToNortheast.Angle = 250;

        NortheastToEast.Quantity = 30;
        NortheastToEast.Angle = 300;
    }
    public Item NorthToNortheast { set; get; }
    public Item NortheastToEast { set; get; }
}

Upvotes: 1

Related Questions