Algorithm with muliple Point structures
Almost every time when I work on graphical applications I found that I have to apply an algorithm that I wrote in the past or found on the net that fit conceptually perfectly in what I need, but differ from data structures. For example I can have sometimes the "Point" expressed as a Vector2
, or as a PointF
, a Point
and so on. Reusing such a code in different projects force sometimes to add undesired references, and alw开发者_开发技巧ays to do some boring conversion from-to the datatypes, so we evantually rewrite the algo for the new data structure. So the question: is there a (smart )way on abstracting the Point accessor in algo ? I think to provide something like a tuple of:
Func<TCoord,TPoint> Xaccessor
Func<TCoord,TPoint> Yaccessor
...
Func<TCoord,TPoint> ...accessor
but I have to do the same for setting the coordinates values, so I end with such a mess that no one neither me want use my algorithm implementation. How concisely specify the point coordinate accessor? Plus we have to take in account too that sometimes the point can be expresse by a float[]. I guess something could be done by using expression trees, but I'm not agile on that so any advice would be really appreciated! Thanks a lot
EDIT Implementing an interface is not an option, since point classes is not written by me, and reflection is not an option for performance reason.
Maybe something like this?
interface ICoordinate<T> { T GetX(int idx); T GetY(int idx); }
class PointRepresentation : ICoordinate<T> {
PointTypeArrayOrWhatever a;
T GetX(int idx) { return a.GetX; }
}
class MyAlgorithm {
ICoordinate accessor;
void Compute() {
// do something with accessor.GetX();
}
}
Or did I misread you? I don't really get your Xaccessor stuff... And you should also keep in mind that this is probably going to slow down your algorithm..
精彩评论