Class Polyline2D
- Namespace
- CADability.Curve2D
- Assembly
- CADability.dll
Implements a polyline in 2D space. By implementing the ICurve2D interface this polyline can be handled as any 2D curve.
public class Polyline2D : GeneralCurve2D, ICurve2D, IQuadTreeInsertable, ISerializable
- Inheritance
-
Polyline2D
- Implements
- Inherited Members
Constructors
Polyline2D(GeoPoint2D[])
Constructs the polyline through the given vertices in their order. Consecutive vertices closer than eps are merged into one. The polyline is closed when the last vertex coincides with the first, so a closed outline must repeat its first vertex at the end.
public Polyline2D(GeoPoint2D[] vertex)
Parameters
vertexGeoPoint2D[]the vertices, at least two distinct ones
Exceptions
- Polyline2DException
when fewer than two distinct vertices remain
Polyline2D(SerializationInfo, StreamingContext)
Constructor required by deserialization
protected Polyline2D(SerializationInfo info, StreamingContext context)
Parameters
infoSerializationInfoSerializationInfo
contextStreamingContextStreamingContext
Properties
EndDirection
The direction of the curve at the end point (see EndDirection). This base implementation returns the normalized direction at the last base point of the triangulation.
public override GeoVector2D EndDirection { get; }
Property Value
EndPoint
The end point of the curve (see EndPoint). This base implementation takes it from the last base point of the triangulation; setting it only replaces that base point and leaves the underlying geometry unchanged, which is acceptable for small corrections only. Curves with their own data override it.
public override GeoPoint2D EndPoint { get; set; }
Property Value
Length
The arc length of the curve, from its points. Derived classes, which know a closed form or the derivative, should override this. The length used to be taken from an approximation by arcs, which is short by up to some tenths of a percent (e.g. a sine curve, the unrolled miter cut of a tube).
public override double Length { get; }
Property Value
MiddleDirection
The direction of the curve at the parameter 0.5 (see MiddleDirection), computed by DirectionAt(double).
public override GeoVector2D MiddleDirection { get; }
Property Value
StartDirection
The direction of the curve at the start point (see StartDirection). This base implementation returns the normalized direction at the first base point of the triangulation.
public override GeoVector2D StartDirection { get; }
Property Value
StartPoint
The start point of the curve (see StartPoint). This base implementation takes it from the first base point of the triangulation; setting it only replaces that base point and leaves the underlying geometry unchanged, which is acceptable for small corrections only. Curves with their own data override it.
public override GeoPoint2D StartPoint { get; set; }
Property Value
Sweep
The total signed change of direction along the curve in radians (see Sweep). This base implementation computes it from an approximation of the curve by circular arcs (Approximate(bool, double)), which is cached; curves with a closed form override it.
public override double Sweep { get; }
Property Value
Vertex
The vertices in their order, as a copy of the internal array; use SetVertices(GeoPoint2D[]) to change them.
public GeoPoint2D[] Vertex { get; }
Property Value
VertexCount
The number of vertices. For a closed polyline the repeated first vertex at the end counts as well.
public int VertexCount { get; }
Property Value
Methods
AddToGraphicsPath(GraphicsPath, bool)
Appends this curve to a GDI+ GraphicsPath, which is used for drawing and printing with GDI+.
public override void AddToGraphicsPath(GraphicsPath path, bool forward)
Parameters
pathGraphicsPaththe path to append to
forwardbooltrue to append the curve in its own direction, false to append it reversed (from the end point to the start point)
Approximate(bool, double)
Approximate this curve and return the approximation. maxError specifies the maximal allowed error, i.e. the maximal deviation of the returned curve from this curve. Is linesOnly ist true, only lines are returned, if linesOnly is false, lines and circular arcs are returned. The result is usually a path consiting of lines and arcs or lines only.
public override ICurve2D Approximate(bool linesOnly, double maxError)
Parameters
Returns
Clone()
Returns an independent copy of this curve of the same type. Modifying the copy does not affect this curve.
public override ICurve2D Clone()
Returns
- ICurve2D
the copy
CloneReverse(bool)
Returns a copy of this curve (see Clone()) which is reversed when reverse is true, i.e.
start point and end point are exchanged and the parameter runs the other way round.
public override ICurve2D CloneReverse(bool reverse)
Parameters
reversebooltrue to reverse the copy, false to copy it as it is
Returns
- ICurve2D
the copy
Copy(ICurve2D)
Copies the data of the given object to this object. Both objects must be of the same type.
public override void Copy(ICurve2D toCopyFrom)
Parameters
toCopyFromICurve2DCopies the data of this object
DirectionAt(double)
Returns the direction of the curve at the provided normalized position.
public override GeoVector2D DirectionAt(double Position)
Parameters
PositiondoubleWhere to get the direction
Returns
- GeoVector2D
The direction
Distance(GeoPoint2D)
Returns the distance of the specified point to this curve. If this curve can be extended (e.g. a line or an arc) this method will return the distance to the extended curve. If you need the distance to the unextended curve use MinDistance(GeoPoint2D).
public override double Distance(GeoPoint2D p)
Parameters
pGeoPoint2DPoint to compute the distance to
Returns
- double
the distance
GetArea()
Returns the signed area swept by the line from the origin to the moving point of the curve, i.e. GetAreaFromPoint(GeoPoint2D) with the origin as reference point. For a closed curve this is the enclosed area, positive when the curve runs counterclockwise, negative when it runs clockwise.
public override double GetArea()
Returns
- double
the signed area
GetAreaFromPoint(GeoPoint2D)
Returns the signed area swept by the line from p to the moving point of the curve as it runs from the
start point to the end point, i.e. the area enclosed by the segment from p to StartPoint,
the curve and the segment from EndPoint back to p. The area is positive when this loop
runs counterclockwise. Summed over the segments of a closed loop it gives the enclosed area, independent of
p, with the sign telling the orientation of the loop; Border and
Face use it this way.
public override double GetAreaFromPoint(GeoPoint2D p)
Parameters
pGeoPoint2Dthe reference point
Returns
- double
the signed area
GetExtendedHitTest()
Returns an IQuadTreeInsertable interface. The HitTest Method of this interface returns true if the rectangle coincides with the infinite extension of this curve (unbounded curve). Most curves are not extendable and therefore simply return "this".
public override IQuadTreeInsertable GetExtendedHitTest()
Returns
GetExtent()
Returns the bounding rectangle of the curve (see GetExtent()). This base implementation takes the base points of the triangulation and adds the points where the direction of the curve changes its sign in x or y between two base points, found by a simple interpolation; the result may therefore be slightly imprecise.
public override BoundingRect GetExtent()
Returns
- BoundingRect
the extent
GetFused(ICurve2D, double)
Returnes a new curve of the same type which is the fusion of this curve with the provided curve, e.g. two overlapping lines build a longer line. Returnes null if the fusion is not possible (e.g. a line and an arc or two lines with different direction)
public override ICurve2D GetFused(ICurve2D toFuseWith, double precision)
Parameters
Returns
GetModified(ModOp2D)
Returns a ICurve which is this curve modified by m. The curve can be of different type, e.g. a Circle may return an Ellpse
public override ICurve2D GetModified(ModOp2D m)
Parameters
mModOp2D
Returns
GetObjectData(SerializationInfo, StreamingContext)
Writes the data of this object for the legacy binary serialization (ISerializable). Files are written in the JSON format (IJsonSerialize) now; the binary format is still read.
public override void GetObjectData(SerializationInfo info, StreamingContext context)
Parameters
infoSerializationInforeceives the data
contextStreamingContextthe destination of the serialization
GetSelfIntersections()
Returns pairs of double values. Each pair defines the two parameters of the curve of a self intersection. The result is either empty or has an even number of double values.
public override double[] GetSelfIntersections()
Returns
- double[]
self intersection parameters
GetSubCurves()
Returns the segments of the polyline as individual Line2D objects, one for each pair of consecutive vertices.
public ICurve2D[] GetSubCurves()
Returns
- ICurve2D[]
the lines in the order of the polyline
GetVertex(int)
Returns the vertex with the given index.
public GeoPoint2D GetVertex(int index)
Parameters
indexintindex from 0 to VertexCount-1
Returns
- GeoPoint2D
the vertex
HitTest(ref BoundingRect, bool)
Tests whether the curve touches or crosses the rectangle (see HitTest(ref BoundingRect, bool)). This base implementation tests the triangles of the triangulation and subdivides a hit triangle until the decision is certain.
public override bool HitTest(ref BoundingRect Rect, bool IncludeControlPoints)
Parameters
RectBoundingRectIncludeControlPointsbool
Returns
- bool
true if the curve and the rectangle have a point in common
Intersect(ICurve2D)
Calculates the intersection points of this curve with another curve. Each result carries the point and its parameters
on both curves (par1 on this curve, par2 on
IntersectWith). Depending on the types involved the result may also contain intersections on the
extensions of the curves (e.g. of two lines, or of the full circles of two arcs), recognizable by parameters outside
0.0 to 1.0; use IsParameterOnCurve(double) to filter them. The result is not sorted.
public override GeoPoint2DWithParameter[] Intersect(ICurve2D IntersectWith)
Parameters
IntersectWithICurve2Dthe other curve
Returns
- GeoPoint2DWithParameter[]
the intersection points, an empty array when there are none
Intersect(GeoPoint2D, GeoPoint2D)
Calculates the intersection points of this curve with the line from StartPoint to EndPoint.
Same as Intersect(ICurve2D) with a Line2D: par2 is the
parameter on that line (0.0 at StartPoint, 1.0 at EndPoint), and points on the
extension of the line or of this curve may be included.
public override GeoPoint2DWithParameter[] Intersect(GeoPoint2D StartPoint, GeoPoint2D EndPoint)
Parameters
StartPointGeoPoint2Dstart point of the line
EndPointGeoPoint2Dend point of the line
Returns
- GeoPoint2DWithParameter[]
the intersection points, an empty array when there are none
IsParameterOnCurve(double)
Determines, whether the given parameter denotes a point inside the bounds of the curve. i.e. the parameter must be greater or equal to 0.0 and less than or equal 1.0. The actual interval is a little bit extended according to the eps value.
public override bool IsParameterOnCurve(double par)
Parameters
pardoublethe parameter to test
Returns
- bool
true if on curve
MakeGeoObject(Plane)
Creates the 3d object that corresponds to this curve placed in the plane p: the 2d coordinates are
taken as coordinates in the coordinate system of the plane (see ToGlobal(GeoPoint2D)). A
Line2D yields a Line, a circle, arc or ellipse an Ellipse, a spline a
BSpline, a path a Path and so on.
public override IGeoObject MakeGeoObject(Plane p)
Parameters
pPlanethe plane to place the curve in
Returns
- IGeoObject
the 3d curve as an IGeoObject
MakePolygon(GeoPoint2D, GeoPoint2D, int)
Creates a closed regular polygon with numberOfVertices corners: vertex is the first
corner, the others are obtained by rotating it about center counterclockwise in steps of
360/numberOfVertices degrees; the last vertex repeats the first.
public static Polyline2D MakePolygon(GeoPoint2D center, GeoPoint2D vertex, int numberOfVertices)
Parameters
centerGeoPoint2Dcenter of the polygon
vertexGeoPoint2Dthe first corner; its distance from the center is the circumradius
numberOfVerticesintnumber of corners, at least 3
Returns
- Polyline2D
the polygon
MakePolyline2D(GeoPoint2D[])
Creates the polyline through the given vertices like the constructor, but returns null instead of throwing when fewer than two distinct vertices are given.
public static Polyline2D MakePolyline2D(GeoPoint2D[] vertex)
Parameters
vertexGeoPoint2D[]the vertices
Returns
- Polyline2D
the polyline, or null
MakeRectangle(GeoPoint2D, double, double, Angle)
Creates a closed rectangle of the given width and height around center,
rotated by angle about that center. The last vertex repeats the first one.
public static Polyline2D MakeRectangle(GeoPoint2D center, double width, double height, Angle angle)
Parameters
centerGeoPoint2DwidthdoubleheightdoubleangleAngle
Returns
MakeRegularPolygon(GeoPoint2D, double, double, int)
Creates a closed regular polygon with numVertices corners on the circle
around center with the radius outerRadius. The first
corner sits at offsetAngle, the polygon runs counterclockwise and its last
vertex repeats the first one.
public static Polyline2D MakeRegularPolygon(GeoPoint2D center, double outerRadius, double offsetAngle, int numVertices)
Parameters
centerGeoPoint2DouterRadiusdoubleoffsetAngledoublenumVerticesint
Returns
MinDistance(ICurve2D)
Returns the minimal distance between this curve and the other curve given in the parameter. If the two curves intersect, the minimal distance ist 0.
public override double MinDistance(ICurve2D Other)
Parameters
OtherICurve2Dcurve to compute the distance to
Returns
Modify(ModOp2D)
Applies the modification (translation, rotation, scaling, reflection...) to all vertices in place. Unlike GetModified(ModOp2D) it changes this polyline instead of returning a modified copy.
public void Modify(ModOp2D m)
Parameters
mModOp2Dthe modification to apply
Move(double, double)
Move the curve by the given offset
public override void Move(double dx, double dy)
Parameters
Parallel(double, bool, double, double)
Returns the parallel (offset) curve at the distance Dist. A positive distance offsets to the right of
the direction of the curve (for a counterclockwise arc: outward), a negative distance to the left. Curves which have no exact
parallel of their own type (e.g. BSpline2D) are first approximated by lines and arcs when
approxSpline is true, and throw an exception otherwise. For composed curves (Path2D,
Polyline2D) the offset segments at a convex corner are joined by an arc when the inner angle of the corner
is less than roundAngle, and are extended to their intersection otherwise. A line of zero length
throws a Curve2DException.
public override ICurve2D Parallel(double Dist, bool approxSpline, double precision, double roundAngle)
Parameters
Distdoublethe offset distance, positive to the right of the curve
approxSplinebooltrue to allow replacing the curve by an approximation of lines and arcs
precisiondoublethe maximal deviation of that approximation
roundAngledoublecorners with an inner angle (in radians) below this value are rounded by an arc, others are extended to a sharp corner; 0.0 never rounds. A negative value is used with its absolute value and additionally removes offset segments that fold back onto their predecessor
Returns
- ICurve2D
the parallel curve
PerpendicularFoot(GeoPoint2D)
Calculates the foot points of the perpendicular projection of the given point on the curve. Perpendicular foot points are points, where the direction of the curve ist perpendicular to the line that connects foot-point to the given point.
public override GeoPoint2D[] PerpendicularFoot(GeoPoint2D FromHere)
Parameters
FromHereGeoPoint2Dfrom this point
Returns
- GeoPoint2D[]
array of foot points
PointAt(double)
Returns the point of the curve at the provided normalized position
public override GeoPoint2D PointAt(double Position)
Parameters
PositiondoubleWhere to get the direction
Returns
- GeoPoint2D
The point
PositionOf(GeoPoint2D)
Returns the position of point p on the curve: 0.0 corresponds to the StartPoint 1.0 to the EndPoint
public override double PositionOf(GeoPoint2D p)
Parameters
Returns
Project(Plane, Plane)
The 2d-curve is assumed to reside in plane "fromPlane". It will be projected perpendicular onto the plane "toPlane".
public override ICurve2D Project(Plane fromPlane, Plane toPlane)
Parameters
Returns
- ICurve2D
the projected curve
Reverse()
Reverses the direction of this curve
public override void Reverse()
SetVertices(GeoPoint2D[])
Replaces all vertices. The array is used directly (not copied) and is not checked for duplicate points; the cached length is reset.
public void SetVertices(GeoPoint2D[] vertices)
Parameters
verticesGeoPoint2D[]the new vertices, at least two
Split(double)
Splits the curve at the given normalized parameter. The curve itself remains unchanged.
public override ICurve2D[] Split(double Position)
Parameters
Positiondoublethe parameter where to split
Returns
- ICurve2D[]
two new curves, the part from 0.0 to
Positionand the part fromPositionto 1.0; a single copy of the whole curve whenPositionis not strictly between 0.0 and 1.0
TangentPoints(GeoPoint2D, GeoPoint2D)
Calculates the points where the direction of the curve is parallel to the direction from the given point to the calculated point. For circles and ellipses it reveals all possible solutions, for other curves (e.g. BSpline) it reveals only the closest solution to the point "CloseTo".
public override GeoPoint2D[] TangentPoints(GeoPoint2D FromHere, GeoPoint2D CloseTo)
Parameters
FromHereGeoPoint2DFrom this point
CloseToGeoPoint2DFind the solution close to this point
Returns
- GeoPoint2D[]
array of tangential points
TangentPointsToAngle(Angle, GeoPoint2D)
Calculates the points where the direction of the curve is parallel to the direction of the given angle. For circles and ellipses it reveals all possible solutions, for other curves (e.g. BSpline) it reveals only the closest solution to the point "CloseTo".
public override GeoPoint2D[] TangentPointsToAngle(Angle ang, GeoPoint2D CloseTo)
Parameters
angAnglethe angle of the tangent
CloseToGeoPoint2DFind the solution close to this point
Returns
- GeoPoint2D[]
array of tangential points
Trim(double, double)
Returns a new curve of the same type which is the part of this curve between the two normalized parameters. The curve
itself remains unchanged. Normally StartPos is less than EndPos; for a closed curve
(Circle2D, Ellipse2D) reversed parameters select the part that runs across the start point of
the curve, for other curves the behaviour depends on the type (a Line2D or Arc2D returns the
part in reversed direction).
public override ICurve2D Trim(double StartPos, double EndPos)
Parameters
Returns
- ICurve2D
the trimmed curve, or null when the result would be degenerate (e.g. an arc with zero sweep)