Class PointExtensions
Provides extension methods for manipulating points.
Inherited Members
Namespace: OpenSvg
Assembly: OpenSvg.dll
Syntax
public static class PointExtensions
Methods
| Edit this page View SourceCompareTo(Vector2, Vector2)
Compares a current point with another point, primarily based on their Y coordinates, followed by their X coordinates. Ordering is similar to how text is read in an English document: left to right, then top to bottom.
Declaration
public static int CompareTo(this Vector2 p1, Vector2 p2)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector2 | p1 | |
| Vector2 | p2 |
Returns
| Type | Description |
|---|---|
| int | A signed integer that indicates the relative order of the points being compared. The return value has these meanings: Less than zero: This instance precedes 'other' in the sort order. Zero: This instance occurs in the same position in the sort order as 'other'. Greater than zero: This instance follows 'other' in the sort order. |
Remarks
The comparison is first made using the Y coordinates. If the Y coordinates are equal, the comparison is then made using the X coordinates.
GetAngle(Vector2, Vector2, Vector2)
Calculates the angle formed by three points in 2D space, normalized to the range ]-180, 180].
Declaration
public static float GetAngle(Vector2 a, Vector2 b, Vector2 c)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector2 | a | The first point in the sequence, forming vector v1 with point b. |
| Vector2 | b | The second point in the sequence, forming vectors v1 with point a and v2 with point c. |
| Vector2 | c | The third point in the sequence, forming vector v2 with point b. |
Returns
| Type | Description |
|---|---|
| float | The normalized angle in degrees formed by the three points, within the range ]-180, 180]. Returns 0 degrees if any two points are identical. |
Remarks
This method computes the angle in degrees between two vectors, v1 and v2, formed by the points a, b, and c. Points a, b, and c are represented as 2D vectors (Point/Vector2).
- If any two of the points are identical (resulting in zero-length vectors), the method returns 0 degrees.
- The angle is normalized using NormalizeAngle to fall within the range ]-180, 180].
- The method is robust against floating-point inaccuracies and avoids returning NaN by clamping the cosine of the angle within the range [-1, 1].
Note: The method assumes a coordinate system where positive angles indicate a clockwise rotation and negative angles an anticlockwise rotation.
IsOnLineSegment(Vector2, Vector2, Vector2)
Determines whether the point is on a line segment defined by two points.
Declaration
public static bool IsOnLineSegment(this Vector2 p, Vector2 a, Vector2 b)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector2 | p | The point to check. |
| Vector2 | a | The start point of the line segment. |
| Vector2 | b | The end point of the line segment. |
Returns
| Type | Description |
|---|---|
| bool | True if the point is on the line segment; otherwise, false. |
IsWithinDistance(Vector2, Vector2, double)
Determines whether the specified point is within a certain distance from the current point.
Declaration
public static bool IsWithinDistance(this Vector2 point, Vector2 other, double distance)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector2 | point | The current point. |
| Vector2 | other | The point to compare with the current point. |
| double | distance | The distance threshold. |
Returns
| Type | Description |
|---|---|
| bool |
|
Remarks
This method is significantly faster than Distance(Vector2, Vector2) since it avoids computing the actual distance.
Examples
Point p1 = new Point(0, 0);
Point p2 = new Point(3, 4);
double distance = 5;
bool isWithin = p1.IsWithinDistance(p2, distance);
Console.WriteLine(isWithin);
//Output: True (since p2 is exactly 5 units away from p1)
|
Edit this page
View Source
NormalizeAngle(float)
Normalizes an angle in degrees to the range ]-180, 180].
Declaration
public static float NormalizeAngle(float degrees)
Parameters
| Type | Name | Description |
|---|---|---|
| float | degrees | The angle in degrees to normalize. |
Returns
| Type | Description |
|---|---|
| float | The normalized angle. |
Remarks
This is useful for using angles to denote turns, where 0 means no turning. A negative value means a left turn and a positive value means a right turn. The higher the absolute value, the sharper the turn.
Transform(Vector2, Transform)
Transforms the point by the specified transform.
Declaration
public static Vector2 Transform(this Vector2 point, Transform transform)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector2 | point | The point to transform. |
| Transform | transform | The transform to apply to the point. |
Returns
| Type | Description |
|---|---|
| Vector2 | The transformed point. |