Class MathHelper
Utility math functions and constants.
Inheritance
Inherited Members
Namespace: netDxf
Assembly: CADability.dll
Syntax
public static class MathHelper
Fields
| Improve this Doc View SourceDegToGrad
Constant to transform an angle between degrees and gradians.
Declaration
public const double DegToGrad = 1.1111111111111112
Field Value
| Type | Description |
|---|---|
| System.Double |
DegToRad
Constant to transform an angle between degrees and radians.
Declaration
public const double DegToRad = 0.017453292519943295
Field Value
| Type | Description |
|---|---|
| System.Double |
GradToDeg
Constant to transform an angle between degrees and gradians.
Declaration
public const double GradToDeg = 0.9
Field Value
| Type | Description |
|---|---|
| System.Double |
HalfPI
PI/2 (90 degrees)
Declaration
public const double HalfPI = 1.5707963267948966
Field Value
| Type | Description |
|---|---|
| System.Double |
PI
PI (180 degrees)
Declaration
public const double PI = 3.1415926535897931
Field Value
| Type | Description |
|---|---|
| System.Double |
RadToDeg
Constant to transform an angle between degrees and radians.
Declaration
public const double RadToDeg = 57.295779513082323
Field Value
| Type | Description |
|---|---|
| System.Double |
ThreeHalfPI
3*PI/2 (270 degrees)
Declaration
public const double ThreeHalfPI = 4.71238898038469
Field Value
| Type | Description |
|---|---|
| System.Double |
TwoPI
2*PI (360 degrees)
Declaration
public const double TwoPI = 6.2831853071795862
Field Value
| Type | Description |
|---|---|
| System.Double |
Properties
| Improve this Doc View SourceEpsilon
Represents the smallest number used for comparison purposes.
Declaration
public static double Epsilon { get; set; }
Property Value
| Type | Description |
|---|---|
| System.Double |
Remarks
The epsilon value must be a positive number greater than zero.
Methods
| Improve this Doc View SourceArbitraryAxis(Vector3)
Gets the rotation matrix from the normal vector (extrusion direction) of an entity.
Declaration
public static Matrix3 ArbitraryAxis(Vector3 zAxis)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector3 | zAxis | Normal vector. |
Returns
| Type | Description |
|---|---|
| Matrix3 | Rotation matrix. |
FindIntersection(Vector2, Vector2, Vector2, Vector2)
Calculates the intersection point of two lines.
Declaration
public static Vector2 FindIntersection(Vector2 point0, Vector2 dir0, Vector2 point1, Vector2 dir1)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector2 | point0 | First line origin point. |
| Vector2 | dir0 | First line direction. |
| Vector2 | point1 | Second line origin point. |
| Vector2 | dir1 | Second line direction. |
Returns
| Type | Description |
|---|---|
| Vector2 | The intersection point between the two lines. |
Remarks
If the lines are parallel the method will return a NaN.
FindIntersection(Vector2, Vector2, Vector2, Vector2, Double)
Calculates the intersection point of two lines.
Declaration
public static Vector2 FindIntersection(Vector2 point0, Vector2 dir0, Vector2 point1, Vector2 dir1, double threshold)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector2 | point0 | First line origin point. |
| Vector2 | dir0 | First line direction. |
| Vector2 | point1 | Second line origin point. |
| Vector2 | dir1 | Second line direction. |
| System.Double | threshold | Tolerance. |
Returns
| Type | Description |
|---|---|
| Vector2 | The intersection point between the two lines. |
Remarks
If the lines are parallel the method will return a NaN.
IsEqual(Double, Double)
Checks if a number is equal to another.
Declaration
public static bool IsEqual(double a, double b)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Double | a | Double precision number. |
| System.Double | b | Double precision number. |
Returns
| Type | Description |
|---|---|
| System.Boolean | True if its close to one or false in any other case. |
IsEqual(Double, Double, Double)
Checks if a number is equal to another.
Declaration
public static bool IsEqual(double a, double b, double threshold)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Double | a | Double precision number. |
| System.Double | b | Double precision number. |
| System.Double | threshold | Tolerance. |
Returns
| Type | Description |
|---|---|
| System.Boolean | True if its close to one or false in any other case. |
IsOne(Double)
Checks if a number is close to one.
Declaration
public static bool IsOne(double number)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Double | number | Double precision number. |
Returns
| Type | Description |
|---|---|
| System.Boolean | True if its close to one or false in any other case. |
IsOne(Double, Double)
Checks if a number is close to one.
Declaration
public static bool IsOne(double number, double threshold)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Double | number | Double precision number. |
| System.Double | threshold | Tolerance. |
Returns
| Type | Description |
|---|---|
| System.Boolean | True if its close to one or false in any other case. |
IsZero(Double)
Checks if a number is close to zero.
Declaration
public static bool IsZero(double number)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Double | number | Double precision number. |
Returns
| Type | Description |
|---|---|
| System.Boolean | True if its close to one or false in any other case. |
IsZero(Double, Double)
Checks if a number is close to zero.
Declaration
public static bool IsZero(double number, double threshold)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Double | number | Double precision number. |
| System.Double | threshold | Tolerance. |
Returns
| Type | Description |
|---|---|
| System.Boolean | True if its close to one or false in any other case. |
NormalizeAngle(Double)
Normalizes the value of an angle in degrees between [0, 360[.
Declaration
public static double NormalizeAngle(double angle)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Double | angle | Angle in degrees. |
Returns
| Type | Description |
|---|---|
| System.Double | The equivalent angle in the range [0, 360[. |
Remarks
Negative angles will be converted to its positive equivalent.
PointInSegment(Vector2, Vector2, Vector2)
Checks if a point is inside a line segment.
Declaration
public static int PointInSegment(Vector2 p, Vector2 start, Vector2 end)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector2 | p | A point. |
| Vector2 | start | Segment start point. |
| Vector2 | end | Segment end point. |
Returns
| Type | Description |
|---|---|
| System.Int32 | Zero if the point is inside the segment, 1 if the point is after the end point, and -1 if the point is before the start point. |
Remarks
For testing purposes a point is considered inside a segment, if it falls inside the area from start to end of the segment that extends infinitely perpendicularly to its direction. Later, if needed, you can use the PointLineDistance method, if the distance is zero the point is along the line defined by the start and end points.
PointInSegment(Vector3, Vector3, Vector3)
Checks if a point is inside a line segment.
Declaration
public static int PointInSegment(Vector3 p, Vector3 start, Vector3 end)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector3 | p | A point. |
| Vector3 | start | Segment start point. |
| Vector3 | end | Segment end point. |
Returns
| Type | Description |
|---|---|
| System.Int32 | Zero if the point is inside the segment, 1 if the point is after the end point, and -1 if the point is before the start point. |
Remarks
For testing purposes a point is considered inside a segment, if it falls inside the volume from start to end of the segment that extends infinitely perpendicularly to its direction. Later, if needed, you can use the PointLineDistance method, if the distance is zero the point is along the line defined by the start and end points.
PointLineDistance(Vector2, Vector2, Vector2)
Calculates the minimum distance between a point and a line.
Declaration
public static double PointLineDistance(Vector2 p, Vector2 origin, Vector2 dir)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector2 | p | A point. |
| Vector2 | origin | Line origin point. |
| Vector2 | dir | Line direction. |
Returns
| Type | Description |
|---|---|
| System.Double | The minimum distance between the point and the line. |
PointLineDistance(Vector3, Vector3, Vector3)
Calculates the minimum distance between a point and a line.
Declaration
public static double PointLineDistance(Vector3 p, Vector3 origin, Vector3 dir)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector3 | p | A point. |
| Vector3 | origin | Line origin point. |
| Vector3 | dir | Line direction. |
Returns
| Type | Description |
|---|---|
| System.Double | The minimum distance between the point and the line. |
RotateAboutAxis(Vector3, Vector3, Double)
Rotate given vector of angle in radians about a specified axis.
Declaration
public static Vector3 RotateAboutAxis(Vector3 v, Vector3 axis, double angle)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector3 | v | Vector to rotate. |
| Vector3 | axis | Rotation axis. This vector should be normalized. |
| System.Double | angle | Rotation angle in radians. |
Returns
| Type | Description |
|---|---|
| Vector3 | A copy of the vector, rotated. |
Remarks
Method provided by: Idelana. Original Author: Paul Bourke ( http://paulbourke.net/geometry/rotate/ )
RoundToNearest(Double, Double)
Round off a numeric value to the nearest of another value.
Declaration
public static double RoundToNearest(double number, double roundTo)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Double | number | Number to round off. |
| System.Double | roundTo | The number will be rounded to the nearest of this value. |
Returns
| Type | Description |
|---|---|
| System.Double | The number rounded to the nearest value. |
Sign(Double)
Returns a value indicating the sign of a double-precision floating-point number.
Declaration
public static int Sign(double number)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Double | number | Double precision number. |
Returns
| Type | Description |
|---|---|
| System.Int32 | A number that indicates the sign of value. Return value Meaning -1 value is less than zero. 0 value is equal to zero. 1 value is greater than zero. |
Remarks
This method will test for values of numbers very close to zero.
Sign(Double, Double)
Returns a value indicating the sign of a double-precision floating-point number.
Declaration
public static int Sign(double number, double threshold)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Double | number | Double precision number. Tolerance. |
| System.Double | threshold |
Returns
| Type | Description |
|---|---|
| System.Int32 | A number that indicates the sign of value. Return value Meaning -1 value is less than zero. 0 value is equal to zero. 1 value is greater than zero. |
Remarks
This method will test for values of numbers very close to zero.
Swap<T>(ref T, ref T)
Swaps two variables.
Declaration
public static void Swap<T>(ref T obj1, ref T obj2)
Parameters
| Type | Name | Description |
|---|---|---|
| T | obj1 | An object of type T. |
| T | obj2 | An object of type T. |
Type Parameters
| Name | Description |
|---|---|
| T | Variable type. |
Transform(Vector2, Double, CoordinateSystem, CoordinateSystem)
Transforms a point between coordinate systems.
Declaration
public static Vector2 Transform(Vector2 point, double rotation, CoordinateSystem from, CoordinateSystem to)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector2 | point | Point to transform. |
| System.Double | rotation | Rotation angle in radians. |
| CoordinateSystem | from | Point coordinate system. |
| CoordinateSystem | to | Coordinate system of the transformed point. |
Returns
| Type | Description |
|---|---|
| Vector2 | Transformed point. |
Transform(Vector3, Vector3, CoordinateSystem, CoordinateSystem)
Transforms a point between coordinate systems.
Declaration
public static Vector3 Transform(Vector3 point, Vector3 zAxis, CoordinateSystem from, CoordinateSystem to)
Parameters
| Type | Name | Description |
|---|---|---|
| Vector3 | point | Point to transform. |
| Vector3 | zAxis | Object normal vector. |
| CoordinateSystem | from | Point coordinate system. |
| CoordinateSystem | to | Coordinate system of the transformed point. |
Returns
| Type | Description |
|---|---|
| Vector3 | Transformed point. |
Transform(IEnumerable<Vector2>, Double, CoordinateSystem, CoordinateSystem)
Transforms a point list between coordinate systems.
Declaration
public static List<Vector2> Transform(IEnumerable<Vector2> points, double rotation, CoordinateSystem from, CoordinateSystem to)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<Vector2> | points | Point list to transform. |
| System.Double | rotation | Rotation angle in radians. |
| CoordinateSystem | from | Point coordinate system. |
| CoordinateSystem | to | Coordinate system of the transformed point. |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.List<Vector2> | Transformed point list. |
Transform(IEnumerable<Vector3>, Vector3, CoordinateSystem, CoordinateSystem)
Transforms a point list between coordinate systems.
Declaration
public static List<Vector3> Transform(IEnumerable<Vector3> points, Vector3 zAxis, CoordinateSystem from, CoordinateSystem to)
Parameters
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<Vector3> | points | Points to transform. |
| Vector3 | zAxis | Object normal vector. |
| CoordinateSystem | from | Points coordinate system. |
| CoordinateSystem | to | Coordinate system of the transformed points. |
Returns
| Type | Description |
|---|---|
| System.Collections.Generic.List<Vector3> | Transformed point list. |