Hi I’m creating a geometry library in C#…
I have an abstract class shape.
I have defined a class vector (also representing (x,y) points).
I would like to use a variety of geometrical objects, curves, lines, arcs, paths etc
To do this I’ve defined an abstract Segment class and derived a number of classes e.g. LineSegment (see below), CurveSegment, ArcCircleSegment, BezierCurveSegment, HalfInfiniteLine etc.
I’ve also defined a class Path (NOT abstract) which is intended to represent a number of segments joined together (like what you might get from a drawing application). In this i include a List member of Segments (List<Segment>).
Then i wish to derive classes from Path, the key example being LinePath which should contain only LineSegments. The problem i have is i’d like to be able to call the get property on a LinePath object assuming it’ll return a LineSegment. Is this possible without explicitly casting each time?
I want to avoid making Path abstract as i might have a path of multiple Segment types.
public class LineSegment : Segment
{
private vector m_start;
private vector m_end;
private vector m_vector;
public vector Start
{
get { return m_start; }
set { m_start = value; }
}
public vector End
{
get { return m_end; }
set { m_end = value; }
}
public vector Vec
{
get { return m_vector; }
set { m_vector = value; }
}
public double Length()
{
return Vec.length();
}
public LineSegment(vector v0, vector v1):base()
{
this.Start.x = v0.x;
this.Start.y = v0.y;
this.End.x = v1.x;
this.End.y = v1.y;
this.Vec = this.End - this.Start;
}
}
If I understand what you want correctly, you can do something like this:
Make your path class generic
You can then create your LinePath object
that way you can ensure that all segments in your
LinePath.SegmentsareLineSegmentswhile still being able to re-use thePathclass for any operations which act against aSegment.