Table of Contents

Class Border

Namespace
CADability.Shapes
Assembly
CADability.dll

A simple border composed of one ore more ICurve2D objects. A Border is always invariant, i.e. you annot change it (like System.String). If a border is closed, then it is oriented counterclockwise. A border may be produced by the BorderBuilder object (or by its constructors).

public class Border : ISerializable, IDeserializationCallback, IJsonSerialize, IJsonSerializeDone
Inheritance
Border
Implements
Inherited Members

Constructors

Border()

Creates an empty border without segments. It only assigns the internal id; it exists for deserialization and for the other constructors, which call it first, and is not usable on its own.

protected Border()

Border(ICurve2D)

Creates a border from a single curve, e.g. a closed Circle2D or BSpline2D, or an open curve for which parallels or intersections are needed. A Path2D or Polyline2D is split into its sub curves, which become the segments. The border is closed when the curve is closed and is then oriented counterclockwise (the curve itself is reversed if necessary).

public Border(ICurve2D onlySegment)

Parameters

onlySegment ICurve2D

The curve making up the border

Border(ICurve2D[])

Constructs a Border from a list of ICurve2D objects. The objects must be in the correct order and must be continous, i.e. EndPoint of ICurve2D[i] must be equal to Startpoint of ICurve2D[i+1]. Equality refers to Precision.IsEqual(). It will be checked automatically, whether the border is closed and if so, it will be oriented counterclockwise.

public Border(ICurve2D[] segments)

Parameters

segments ICurve2D[]

list of curves to make the border

Border(ICurve2D[], bool, bool)

Create a border with the provided segments assumed to be in correct order and orientation. If forceConnected is true, the border will be closed.

public Border(ICurve2D[] segments, bool forceConnected, bool flatten = true)

Parameters

segments ICurve2D[]

The oriented and ordered segments

forceConnected bool

True: border will be closed even if segments aren't connected at the end

flatten bool

True: segments which are Path2D objects are replaced by their sub curves

Border(GeoPoint2D[])

Creates a closed polygonal border through the given vertices. The closing edge from the last vertex back to the first one is added automatically, each edge becomes a Line2D segment, and the border is oriented counterclockwise regardless of the order of the vertices.

public Border(GeoPoint2D[] polyline)

Parameters

polyline GeoPoint2D[]

The vertices of the polygon in order

Border(out bool, ICurve2D[])

Same as Border(ICurve2D[]), but additionally reports whether the segments had to be reversed. The segments must be ordered and connected (the end point of each segment is the start point of the next). If they form a closed loop running clockwise, the order of the segments and the direction of each segment are reversed so that the border runs counterclockwise; reversed is then true. The curves in the array are used (and possibly reversed) by the border, they are not cloned.

public Border(out bool reversed, ICurve2D[] segments)

Parameters

reversed bool

true if the segments were reversed to orient the border counterclockwise

segments ICurve2D[]

Ordered, connected curves making up the border

Border(SerializationInfo, StreamingContext)

Constructor required by deserialization

protected Border(SerializationInfo info, StreamingContext context)

Parameters

info SerializationInfo

SerializationInfo

context StreamingContext

StreamingContext

Properties

Area

The area enclosed by this border, positive since closed borders run counterclockwise. The value is computed on first access and cached. An open border whose start and end point are very close (relative to its extent) is closed first; for any other open border a BorderException with AreaOpen is thrown.

public double Area { get; }

Property Value

double

Count

The number of segments of this border. The parameter of a point on the border (see GetParameter(GeoPoint2D) and PointAt(double)) runs from 0 to this value: the integer part is the index of the segment, the fractional part the position on that segment.

public int Count { get; }

Property Value

int

EndPoint

The end point of the last segment, i.e. the point with parameter Count. For a closed border it coincides with StartPoint.

public GeoPoint2D EndPoint { get; }

Property Value

GeoPoint2D

Extent

The axis aligned bounding rectangle of all segments of this border.

public BoundingRect Extent { get; }

Property Value

BoundingRect

IsClosed

true if the end point of the last segment coincides with the start point of the first segment, i.e. the border encloses an area. Only closed borders can be the outline or a hole of a SimpleShape; open borders occur as cutting curves (e.g. in Split(Border)) and as parallels of open curves.

public bool IsClosed { get; }

Property Value

bool

IsEmpty

true if this border encloses no area, e.g. because all segments lie on a line or the border degenerated to a point. Uses a fast estimate of the area (splines are replaced by the polygon through their knots), so it is cheap even for borders with many splines. Does not require the border to be closed.

public bool IsEmpty { get; }

Property Value

bool

this[int]

Returns the segment with the given index. This is the original curve of the border, not a copy: do not modify it, the border would become inconsistent. Use GetClonedSegments() for curves you want to change.

public ICurve2D this[int index] { get; }

Parameters

index int

Index of the segment, 0 to Count-1

Property Value

ICurve2D

The segment

Length

The total length of all segments of this border (the perimeter of a closed border). Computed on each access.

public double Length { get; }

Property Value

double

SomeInnerPoint

Some point strictly inside this closed border, found by clipping the diagonal of the extent (and, if that fails, random lines) with the border and taking the middle of a piece inside. Useful as a test point, e.g. to decide on which side of another border this one lies. Throws a BorderException if the border has no area.

public GeoPoint2D SomeInnerPoint { get; }

Property Value

GeoPoint2D

StartPoint

The start point of the first segment, i.e. the point with parameter 0. For a closed border it is also the end point of the last segment.

public GeoPoint2D StartPoint { get; }

Property Value

GeoPoint2D

UnsplittedOutline

In case of a border with a single outline like circles or closed bsplines this property will contain a cloned unsplitted outline. While GetClonedSegments() will return the splitted one.

public ICurve2D UnsplittedOutline { get; }

Property Value

ICurve2D

Methods

AddToGraphicsPath(GraphicsPath, bool)

Appends the segments of this border to a GDI+ GraphicsPath, e.g. for filling or clipping with System.Drawing. With counterClockWise false the segments are added in reverse order and direction, which is how SimpleShape adds its holes so that they stay unfilled.

public void AddToGraphicsPath(GraphicsPath path, bool counterClockWise)

Parameters

path GraphicsPath

The graphics path to append to

counterClockWise bool

true: add in the direction of the border, false: add reversed

AsPath()

Returns this Border as a Path2D object.

public Path2D AsPath()

Returns

Path2D

ChangeCyclicalStart(int)

Rotates the segments of this closed border so that the segment with index newStartIndex becomes the first segment. The geometry and the orientation stay the same, only StartPoint and the origin of the border parameter move. This is one of the few operations that modify a border in place.

public void ChangeCyclicalStart(int newStartIndex)

Parameters

newStartIndex int

Index (0 to Count-1) of the segment that becomes the first one

Clip(ICurve2D, bool)

Clips a curve with this closed border and returns the parts of the curve inside (or outside) the border as parameter intervals on the curve. The curve is intersected with the border and each piece between consecutive intersection points is classified by the position of its midpoint. The result holds pairs of curve parameters (0 to 1), start and end of each wanted piece, sorted along the curve; a curve lying completely inside yields {0, 1}. Pieces running along the outline are included in both cases. Use Trim(double, double) to cut the pieces out of the curve.

public double[] Clip(ICurve2D toClip, bool returnInsideParts)

Parameters

toClip ICurve2D

The curve to clip

returnInsideParts bool

true: return the parts inside the border, false: the parts outside

Returns

double[]

Start and end parameters of the pieces, an even number of values; empty if there is no such piece

Clone()

Returns a deep copy of this border: the segments are cloned, so the copy can be modified (e.g. with RemoveSmallSegments(double)) without affecting this border. UnsplittedOutline is cloned too.

public Border Clone()

Returns

Border

The cloned border

ConvexHull()

Returns the convex hull of this Border

public Border ConvexHull()

Returns

Border

CounterClockwise(ICurve2D[])

Determines whether the given closed loop of curves runs counterclockwise. The curves must be ordered and connected (the end point of each curve is the start point of the next, the last one ending at the start of the first) and must not intersect themselves. The result is computed from the accumulated turning angle, which is fast but unreliable for self intersecting loops.

public static bool CounterClockwise(ICurve2D[] curves)

Parameters

curves ICurve2D[]

The ordered, connected curves of the loop

Returns

bool

true if the loop runs counterclockwise, false if it runs clockwise

CounterClockwise(List<ICurve2D>)

Same as CounterClockwise(ICurve2D[]) for a list of curves.

public static bool CounterClockwise(List<ICurve2D> curves)

Parameters

curves List<ICurve2D>

The ordered, connected curves of the loop

Returns

bool

true if the loop runs counterclockwise, false if it runs clockwise

DirectionAt(double)

Returns the direction of the border at the given parameter (see PointAt(double) for the meaning of the parameter). Exactly at a vertex between two segments the mean of the end direction of the previous and the start direction of the following segment is returned, which gives a usable direction at a corner.

public GeoVector2D DirectionAt(double par)

Parameters

par double

Parameter on the border, 0 to Count

Returns

GeoVector2D

The direction at that parameter

DistanceAtDirection(Border, GeoVector2D)

Calculates the distance between this Border and the other Border respecting the given direction dir. Or in other words: how far can you move this Border in the direction dir until it touches the other border. The result may be double.MaxValue, which means they will never touch each other or negative, if you would have to move this border in the opposite direction of dir.

public double DistanceAtDirection(Border other, GeoVector2D dir)

Parameters

other Border

The border to meassure the distance to

dir GeoVector2D

Direction for the distance

Returns

double

Distance

FromUnorientedList(ICurve2D[], bool)

segments contains the curves in the correct order, but possibly in the wrong direction. So they are reoriented here, if necessary, before the border is created.

public static Border FromUnorientedList(ICurve2D[] segments, bool forceClosed)

Parameters

segments ICurve2D[]
forceClosed bool

True: the start point of the first segment is moved onto the end point of the last segment, so that the resulting border is closed

Returns

Border

GetClonedSegments()

Returns copies of the segments of this border in their order and orientation. Modifying the returned curves does not affect this border. Use the indexer (this[int]) to access the original segments without copying.

public ICurve2D[] GetClonedSegments()

Returns

ICurve2D[]

Cloned segments

GetIntersectionPoints(ICurve2D)

Returns a list of all intersectionpoints of this border with the given curve. Some intersectionpoints may be found twice, if the Curve passes through a vertex of this border. The par1 member of the intersectionpoint is the parameter of this border (0.0<=par1<=this.Count) the par2 member is the parameter of the curve (0.0<=par1<=1.0).

public GeoPoint2DWithParameter[] GetIntersectionPoints(ICurve2D IntersectWith)

Parameters

IntersectWith ICurve2D

curve to intersect this border with

Returns

GeoPoint2DWithParameter[]

array of intersection points

GetIntersectionPoints(ICurve2D, double)

Returns all intersection points of this border with the given curve, see GetIntersectionPoints(ICurve2D). par1 of each result is the parameter on this border (segment index plus position on the segment, 0 to Count), par2 the parameter on the curve (0 to 1). When a segment and the curve do not properly intersect, their end points are checked: an end point of one lying closer than precision to the other is reported as an intersection point too, so curves that start or end exactly on the border are found.

public GeoPoint2DWithParameter[] GetIntersectionPoints(ICurve2D IntersectWith, double precision)

Parameters

IntersectWith ICurve2D

Curve to intersect this border with

precision double

Maximum distance for an end point to count as touching

Returns

GeoPoint2DWithParameter[]

The intersection points, empty if there are none

GetIntersectionPoints(Border)

Returns all intersection points of this border with another border, using a precision derived from the extents of both borders, see GetIntersectionPoints(Border, double).

public GeoPoint2DWithParameter[] GetIntersectionPoints(Border IntersectWith)

Parameters

IntersectWith Border

The border to intersect with

Returns

GeoPoint2DWithParameter[]

The intersection points, empty if there are none

GetIntersectionPoints(Border, double)

Returns all intersection points of this border with another border. par1 of each result is the parameter on this border, par2 the parameter on intersectWith; both are the segment index plus the position on that segment. End points of segments that lie closer than precision to the other border are reported as intersection points too, and a point at a common vertex may be reported more than once.

public GeoPoint2DWithParameter[] GetIntersectionPoints(Border intersectWith, double precision)

Parameters

intersectWith Border

The border to intersect with

precision double

Maximum distance for an end point to count as touching

Returns

GeoPoint2DWithParameter[]

The intersection points, empty if there are none

GetMinDistance(GeoPoint2D)

Returns the smallest distance between p and the segments of this border, i.e. the distance from the point to the outline itself (see MinDistance(GeoPoint2D)). The result is never negative and does not tell whether the point lies inside or outside a closed border; use GetPosition(GeoPoint2D) for that. All segments are tested.

public double GetMinDistance(GeoPoint2D p)

Parameters

p GeoPoint2D

the point to measure the distance to

Returns

double

the minimum distance, 0 when the point lies on the border

GetMinDistance(Border)

Returns the smallest distance between any segment of this border and any segment of Other. The result is 0 when the borders touch or intersect; whether one border lies inside the other is not considered. All pairs of segments are tested, so this is slow for borders with many segments.

public double GetMinDistance(Border Other)

Parameters

Other Border

The border to measure the distance to

Returns

double

The minimum distance

GetModified(ModOp2D)

Returns a copy of this border transformed by m; this border stays unchanged. For a reflection (negative determinant) the segments are reversed, so a closed border remains counterclockwise. The UnsplittedOutline is transformed along with the segments.

public Border GetModified(ModOp2D m)

Parameters

m ModOp2D

The transformation to apply

Returns

Border

The transformed border

GetObjectData(IJsonWriteData)

Writes the segments (and the UnsplittedOutline, if any) for the JSON serialization (IJsonSerialize).

public void GetObjectData(IJsonWriteData data)

Parameters

data IJsonWriteData

Receives the properties of this border

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 void GetObjectData(SerializationInfo info, StreamingContext context)

Parameters

info SerializationInfo

receives the data

context StreamingContext

the destination of the serialization

GetParallel(double, bool, double, double)

Yields a set of borders that are parallel to this border with a given distance. Positive distance yields a border to the right side (or outside if closed) of this border, negative to the left side or inside. In some cases there is no soltuion (e.g. an inside parallel border of a closed border must have a distance less than half of the diameter of the border) In other cases there may be a set of solutions, e.g. if the border is not convex.

public Border[] GetParallel(double dist, bool approxSpline, double precision, double roundAngle)

Parameters

dist double

the distance of the desired parallel border

approxSpline bool

true: BSpline segments are approximated by lines and arcs (with precision) before the parallel is computed; false: segments without an exact parallel of their own (splines, ellipses) are approximated by lines and arcs with dist*precision

precision double

precision relative to dist: segments shorter than dist*precision are ignored and points closer than that are considered equal; 0.0 means 1e-3

roundAngle double

inner angle (in radians) at a corner below which the parallel segments are connected by an arc instead of being extended to their intersection

Returns

Border[]

array of parallel borders (maybe empty)

GetParameter(GeoPoint2D)

Returns the position of this point on the border. The position is the index of the closest segment plus the position on this segment

public double GetParameter(GeoPoint2D p)

Parameters

p GeoPoint2D

the point for which the position is returned

Returns

double

the position between 0 and the number of segments

GetPart(double, double, bool)

Returns copies of the part of this border between two parameters (see PointAt(double) for the meaning of a parameter): the trimmed pieces of the segments at both ends and clones of the segments in between. With forward true the part runs in the direction of the border from startParam to endParam, passing the start of a closed border when the end parameter is the smaller one. With forward false the part runs against the direction of the border; the curves are then reversed, so the result always starts at startParam and ends at endParam.

public ICurve2D[] GetPart(double startParam, double endParam, bool forward)

Parameters

startParam double

Parameter where the part starts

endParam double

Parameter where the part ends

forward bool

true: walk in the direction of the border, false: walk against it

Returns

ICurve2D[]

The curves of the part in order, empty if both parameters are equal

GetPosition(BoundingRect)

Returns the Border.Position of the center of the given rectangle relative to this Border. If the outline of the border interferes with the rectangle, the result will be OnCurve.

public Border.Position GetPosition(BoundingRect rect)

Parameters

rect BoundingRect

The rectangle to test, its center is the point whose position is wanted

Returns

Border.Position

Inside, Outside or OnCurve (on the outline of this border)

GetPosition(GeoPoint2D)

Returns the Border.Position of the given Point relative to this Border.

public Border.Position GetPosition(GeoPoint2D p)

Parameters

p GeoPoint2D

The Point to test

Returns

Border.Position

Inside, Outside or OnCurve (on the outline of this border)

GetSelfIntersection(double)

Finds the points where this border intersects itself, i.e. where two non adjacent segments cross (end points touching the other segment closer than precision count as well). The result holds three values per intersection: the parameter on the first segment, the parameter on the second segment (both as segment index plus position, see PointAt(double)) and the orientation of the crossing (the cross product of the two directions), which tells which of the two loops formed by the crossing runs counterclockwise. Together with GetPart(double, double, bool) this splits a self intersecting border into simple loops.

public double[] GetSelfIntersection(double precision)

Parameters

precision double

Maximum distance for a touching end point to count as an intersection

Returns

double[]

Three values per self intersection, empty if the border does not intersect itself

GetSmallestEnclosingRectangle()

Finds the rectangle with the smallest area enclosing this Border.

public SmallestEnclosingRect GetSmallestEnclosingRectangle()

Returns

SmallestEnclosingRect

IsInside(ICurve2D[], GeoPoint2D)

Returnes true, when the provided point is inside the loop of curves. When the curves are oriented clockwise, true means outside (the hole)

public static bool IsInside(ICurve2D[] curves, GeoPoint2D toTest)

Parameters

curves ICurve2D[]
toTest GeoPoint2D

Returns

bool

IsPointOnOutline(ICurve2D[], GeoPoint2D, double)

Tests whether a point lies on one of the given curves, i.e. whether its distance to some curve is less than precision. The curves need not be connected or form a border.

public static bool IsPointOnOutline(ICurve2D[] curves, GeoPoint2D toTest, double precision)

Parameters

curves ICurve2D[]

The curves to test against

toTest GeoPoint2D

The point to test

precision double

Maximum distance for the point to count as being on a curve

Returns

bool

true if the point is closer than precision to one of the curves

MakeCircle(GeoPoint2D, double)

Creates a closed border consisting of a single counterclockwise Circle2D.

public static Border MakeCircle(GeoPoint2D center, double radius)

Parameters

center GeoPoint2D

Center of the circle

radius double

Radius of the circle

Returns

Border

The circular border

MakeLongHole(ICurve2D, double)

Creates the closed border of a slot (long hole) around an open curve: the set of all points closer than radius to centerCurve. It consists of the two parallel curves at distance radius on both sides and two half circles around the start and the end point of the curve. Intended for lines and for arcs whose radius is greater than radius; see MakeLongHole(ICurve2D, double, double) for arbitrary curves and paths.

public static Border MakeLongHole(ICurve2D centerCurve, double radius)

Parameters

centerCurve ICurve2D

The open center curve of the slot

radius double

Half the width of the slot

Returns

Border

The border of the slot

MakeLongHole(GeoPoint2D, GeoPoint2D, double, double)

Creates the closed border of a slot between two circles with different radii: the convex hull of the circle around startPoint with startRadius and the circle around endPoint with endRadius, i.e. two arcs connected by the two outer tangent lines. If one of the radii is 0 or less, that end degenerates to the point itself, giving a drop shaped border.

public static Border MakeLongHole(GeoPoint2D startPoint, GeoPoint2D endPoint, double startRadius, double endRadius)

Parameters

startPoint GeoPoint2D

Center of the first circle

endPoint GeoPoint2D

Center of the second circle

startRadius double

Radius at startPoint, 0 or negative for a sharp end

endRadius double

Radius at endPoint, 0 or negative for a sharp end

Returns

Border

The border of the slot

MakePolygon(GeoPoint2D, double, int)

Creates the border of a regular polygon inscribed in the circle with the given center and radius, composed of Line2D segments. The first vertex lies in the direction of the x axis from the center, the vertices follow counterclockwise. Mostly used through MakePolygon(GeoPoint2D, double, int).

public static Border MakePolygon(GeoPoint2D center, double radius, int numberOfSides)

Parameters

center GeoPoint2D

Center of the polygon

radius double

Distance of the vertices from the center

numberOfSides int

Number of sides (and vertices), at least 3

Returns

Border

The polygonal border

MakeRectangle(double, double, double, double)

Creates a closed, counterclockwise rectangular border with axis parallel sides, composed of four Line2D segments starting at the lower left corner.

public static Border MakeRectangle(double left, double right, double bottom, double top)

Parameters

left double

x coordinate of the left side

right double

x coordinate of the right side

bottom double

y coordinate of the bottom side

top double

y coordinate of the top side

Returns

Border

The rectangular border

PointAt(double)

Returns the point at the given border parameter: the integer part of par is the index of the segment, the fractional part the position on that segment (0 to 1). Parameters below 0 yield StartPoint, parameters of Count or more yield EndPoint.

public GeoPoint2D PointAt(double par)

Parameters

par double

Parameter on the border, 0 to Count

Returns

GeoPoint2D

The point on the border

Project(Plane, Plane)

Projects this border, which is assumed to lie in fromPlane, perpendicularly onto toPlane and returns the result in the 2d coordinates of that plane. If the normals of the planes point in opposite directions, the segments are reversed so that a closed border stays counterclockwise.

public Border Project(Plane fromPlane, Plane toPlane)

Parameters

fromPlane Plane

The plane this border lies in

toPlane Plane

The plane to project onto; must not be perpendicular to fromPlane

Returns

Border

The projected border, or null if the projected segments do not form a valid border

Recalc(out bool)

Recomputes the derived data after the segments have been set: consecutive segments are connected exactly (the end point of a segment is moved onto the start point of the next one when they only agree within IsEqual(GeoPoint2D, GeoPoint2D)), Extent is computed, it is decided whether the border is closed and, for a closed border running clockwise, the order of the segments and the direction of each segment are reversed so that the border runs counterclockwise. The quad tree used for spatial queries is discarded and rebuilt on demand.

protected void Recalc(out bool reversed)

Parameters

reversed bool

true if the segments were reversed to orient the border counterclockwise

RecalcArea()

Computes the signed area enclosed by the segments as the sum of the areas of the segments (GetArea()): positive for a counterclockwise, negative for a clockwise border. Exact, but slow for splines, which is why Area caches the result.

protected double RecalcArea()

Returns

double

The signed area

RemoveSmallSegments(double)

Removes small segments which are smaller than minLength and connects the remaining segments.

public void RemoveSmallSegments(double minLength)

Parameters

minLength double

Segments smaller than this value are beeing removed

SetObjectData(IJsonReadData)

Reads the segments written by GetObjectData(IJsonWriteData) for the JSON deserialization. The derived data (extent, orientation, whether the border is closed) is recomputed after all objects have been read.

public void SetObjectData(IJsonReadData data)

Parameters

data IJsonReadData

The properties of the border being read

Split(double[])

Returns an array of Border objects by breaking this border at the given positions. Parameter must be an ordered list of double values. Each value must be greater 0.0 and less the number of iCurve2D objects in this Border.

public Border[] Split(double[] Parameter)

Parameters

Parameter double[]

Returns

Border[]

getRoughArea()

Fast estimate of the signed area: like RecalcArea(), but a BSpline2D segment is replaced by the polygon through its knot points. Used by IsEmpty to decide whether the border encloses any area at all.

protected double getRoughArea()

Returns

double

The approximate signed area

Operators

operator +(Border, Border)

Concatenates two border objects. Both borders must be open (not closed) and the endpoint of the first border must be equal to the startpoint of the second border (as defined by Precision.IsEqual).

public static Border operator +(Border first, Border second)

Parameters

first Border
second Border

Returns

Border