Reputation: 391
I need to save a bunch 2d shapes (defined as positioned vertices) in 2d space (x y coordinates).
What is the best way to do it in c#?
Right now i'm saving them as lists of vertices:
new Shape()
{
X = 20, //Position of the shape's top left corner on screen
Y = 50, //Position of the shape's top left corner on screen
Vertices = new List<Tuple<float, float>>() //This is a square
{
new Tuple<float, float>(0, 0),//Top left corner
new Tuple<float, float>(0, 100), //Bottom left corner
new Tuple<float, float>(100, 100),//Bottom right corner
new Tuple<float, float>(100, 0)//Top right corner
}
}
public class Shape
{
public float X;
public float Y;
public List<Tuple<float, float>> Vertices;
}
Is there some better way to do that?
I need to be able to achieve two things with the saved shape:
Display it on the screen on the given x y (basically like absolute positioning in css) coordinates And know the coordinates of each vertex in the shape
Upvotes: 1
Views: 421
Reputation: 892
You may want to save your points as 'actual' points, but apart from that I don't think this could be 'saved' in any better way (That is, if by saving you mean storing them in memory).
Take a look at:
System.Windows.Point
(For WPF).
or
System.Drawing.Point
(For WinForms).
If you need to save them to disk, you may want to look in to some serialization, which technology you choose there is mostly up to taste and what you need, Performance? Space? etc.
ProtoBuf can be used to save relatively small files while still retaining pretty decent performance: https://code.google.com/p/protobuf-net/
Or you may want to use the built-in .NET XML serializer, or Binary serializer (Do note though that those are usually quite slow in comparison to other alternatives).
Also, if you want to save one line of code:
public class Shape
{
public Point Position;
public List<Point> Vertices;
}
Upvotes: 1