Table of Contents

Class MathRoutines

Namespace
LightningChartLib.WinForms.Charting
Assembly
LightningChart.WinForms.Charting.NET4.dll

Chart tools provides versatile static methods

public class MathRoutines
Inheritance
MathRoutines
Inherited Members

Constructors

MathRoutines()

LightningChartLib.WinForms.Charting.MathRoutines
public MathRoutines()

Methods

AngleDegInFirstFullCircle(double)

Retrieve the input angle value as positive angle of the first full rotation in degrees.

Note that the range contains 0, but not the "full angle" of 360 degrees.

public static double AngleDegInFirstFullCircle(double value)

Parameters

value double

Angle to find the equivalent angle in degrees.

Returns

double

Angle matching the input in first full positive round as degrees.

AngleDiffDeg(double, double)

Solve smallest absolute difference between two angles in degrees. This takes in account the periodic nature of the values, meaning that full circles do not affect the difference.

Useful in comparing the difference of angles, e.g. on testing if angles are the same.

Note that the type of angle unit affects the result. Use AngleDiffRad(double, double) for radians.

public static double AngleDiffDeg(double angleDeg1, double angleDeg2)

Parameters

angleDeg1 double

One of the angles to calculate the difference between in degrees. Order of parameter does not matter.

angleDeg2 double

One of the angles to calculate the difference between in degrees. Order of parameter does not matter.

Returns

double

Difference of provided angles in degrees. Always positive.

Remarks

Examples

350 to 10 = 20 degrees

10 to 350 = 20 degrees

-360 to 350 = 10 degrees

AngleDiffRad(double, double)

Solve smallest absolute difference between two angles in radians. This takes in account the periodic nature of the values, meaning that full circles do not affect the difference.

Useful in comparing the difference of angles, e.g. on testing if angles are the same.

Note that the type of angle unit affects the result. Use AngleDiffDeg(double, double) for degrees and examples.

public static double AngleDiffRad(double angleRad1, double angleRad2)

Parameters

angleRad1 double

One of the angles to calculate the difference between in radians. Order of parameter does not matter.

angleRad2 double

One of the angles to calculate the difference between in radians. Order of parameter does not matter.

Returns

double

Difference of provided angles in radians. Always positive.

AngleRadInFirstFullCircle(double)

Retrieve the input angle value as positive angle of the first full rotation in radians.

Note that the range contains 0, but not the "full angle" of PI radians.

public static double AngleRadInFirstFullCircle(double value)

Parameters

value double

Angle to find the equivalent angle in radians.

Returns

double

Angle matching the input in first full positive round as radians.

CalcAngleOfLineVectorDouble(PointFloat, PointFloat)

Calculates angle of line going from pointFrom to pointTo

public static double CalcAngleOfLineVectorDouble(PointFloat pointFrom, PointFloat pointTo)

Parameters

pointFrom PointFloat

From point

pointTo PointFloat

To point

Returns

double

RotateAngle in radians, in range 0...2*Pi

CalcAngleOfLineVectorFloat(float, float, float, float)

Calculates angle of line going from pointFrom to pointTo

public static float CalcAngleOfLineVectorFloat(float x1, float y1, float x2, float y2)

Parameters

x1 float

Point 1 X

y1 float

Point 1 Y

x2 float

Point 2 X

y2 float

Point 2 Y

Returns

float

RotateAngle in radians, in range 0...2*Pi

CalcTwoLinesIntersection(PointDouble2D, PointDouble2D, PointDouble2D, PointDouble2D, out PointDouble2D)

Calculates intersection point of two lines. Line 1: goes through A1 and A2 Line 2: goes through B1 and B2

public static bool CalcTwoLinesIntersection(PointDouble2D a1, PointDouble2D a2, PointDouble2D b1, PointDouble2D b2, out PointDouble2D intersection)

Parameters

a1 PointDouble2D

Line A point 1

a2 PointDouble2D

Line A point 2

b1 PointDouble2D

Line B point 1

b2 PointDouble2D

Line B point 2

intersection PointDouble2D

intersection point output

Returns

bool

Success status. Returns false if could not solve the intersection

CalcTwoLinesIntersection(PointFloat, PointFloat, PointFloat, PointFloat, out PointFloat)

Calculates intersection point of two lines. Line 1: goes through A1 and A2 Line 2: goes through B1 and B2

public static bool CalcTwoLinesIntersection(PointFloat a1, PointFloat a2, PointFloat b1, PointFloat b2, out PointFloat intersection)

Parameters

a1 PointFloat

Line A point 1

a2 PointFloat

Line A point 2

b1 PointFloat

Line B point 1

b2 PointFloat

Line B point 2

intersection PointFloat

intersection point output

Returns

bool

Success status. Returns false if could not solve the intersection

DegreesAsRadians(double)

Convert degrees to radians.

public static double DegreesAsRadians(double value)

Parameters

value double

Degrees value.

Returns

double

Radians value.

DistanceOfPoints(PointFloat, PointFloat)

Calculates distance between two points.

public static float DistanceOfPoints(PointFloat point1, PointFloat point2)

Parameters

point1 PointFloat

Point 1.

point2 PointFloat

Point 2.

Returns

float

Distance between two points.

Gaussian(double, double, double)

Gaussian function

public static double Gaussian(double input, double mean, double std)

Parameters

input double

argument of function

mean double

mean

std double

standard division

Returns

double
returns M:LightningChartLib.WinForms.Charting.MathRoutines.Gaussian(System.Double,System.Double,System.Double)

Exceptions

NotImplementedException

GradsAsRadians(double)

Convert gradians to radians.

public static double GradsAsRadians(double value)

Parameters

value double

Radians value.

Returns

double

Gradians value.

Hypotrochoid(int, double, double[,], double, double, double, double, int)

Hyptrochoid

public static void Hypotrochoid(int counter, double iStep, double[,] points, double _xMax, double _yMax, double _yMin, double _xMin, int _iDeliveredPoints)

Parameters

counter int
param counter M:LightningChartLib.WinForms.Charting.MathRoutines.Hypotrochoid(System.Int32,System.Double,System.Double[0:,0:],System.Double,System.Double,System.Double,System.Double,System.Int32)
iStep double
param iStep M:LightningChartLib.WinForms.Charting.MathRoutines.Hypotrochoid(System.Int32,System.Double,System.Double[0:,0:],System.Double,System.Double,System.Double,System.Double,System.Int32)
points double[,]
param points M:LightningChartLib.WinForms.Charting.MathRoutines.Hypotrochoid(System.Int32,System.Double,System.Double[0:,0:],System.Double,System.Double,System.Double,System.Double,System.Int32)
_xMax double
param _xMax M:LightningChartLib.WinForms.Charting.MathRoutines.Hypotrochoid(System.Int32,System.Double,System.Double[0:,0:],System.Double,System.Double,System.Double,System.Double,System.Int32)
_yMax double
param _yMax M:LightningChartLib.WinForms.Charting.MathRoutines.Hypotrochoid(System.Int32,System.Double,System.Double[0:,0:],System.Double,System.Double,System.Double,System.Double,System.Int32)
_yMin double
param _yMin M:LightningChartLib.WinForms.Charting.MathRoutines.Hypotrochoid(System.Int32,System.Double,System.Double[0:,0:],System.Double,System.Double,System.Double,System.Double,System.Int32)
_xMin double
param _xMin M:LightningChartLib.WinForms.Charting.MathRoutines.Hypotrochoid(System.Int32,System.Double,System.Double[0:,0:],System.Double,System.Double,System.Double,System.Double,System.Int32)
_iDeliveredPoints int
param _iDeliveredPoints M:LightningChartLib.WinForms.Charting.MathRoutines.Hypotrochoid(System.Int32,System.Double,System.Double[0:,0:],System.Double,System.Double,System.Double,System.Double,System.Int32)

LineIntersection(PointDouble2D, PointDouble2D, PointDouble2D, PointDouble2D, out PointDouble2D)

Calculates intersection point of two lines. Line 1: goes through A1 and A2. Line 2: goes through B1 and B2. Algorithm copied from http://local.wasp.uwa.edu.au/~pbourke/geometry/lineline2d/

public static bool LineIntersection(PointDouble2D a1, PointDouble2D a2, PointDouble2D b1, PointDouble2D b2, out PointDouble2D intersection)

Parameters

a1 PointDouble2D

Line A point 1.

a2 PointDouble2D

Line A point 2.

b1 PointDouble2D

Line B point 1.

b2 PointDouble2D

Line B point 2.

intersection PointDouble2D

Intersection point output

Returns

bool

Success status. Returns false if could not solve the intersection.

LineIntersection(PointFloat, PointFloat, PointFloat, PointFloat, out PointFloat)

Calculates intersection point of two lines. Line 1: goes through A1 and A2. Line 2: goes through B1 and B2. Algorithm copied from http://local.wasp.uwa.edu.au/~pbourke/geometry/lineline2d/

public static bool LineIntersection(PointFloat a1, PointFloat a2, PointFloat b1, PointFloat b2, out PointFloat intersection)

Parameters

a1 PointFloat

Line A point 1.

a2 PointFloat

Line A point 2.

b1 PointFloat

Line B point 1.

b2 PointFloat

Line B point 2.

intersection PointFloat

Intersection point output

Returns

bool

Success status. Returns false if could not solve the intersection.

LinearFitSolveFactors(double[], double[], out double, out double)

Calculate linear fit from given points. Makes best fit with Least Square regression method. A line equation is y = k*x + b, that is, y = gradient * x + coefficient

public static bool LinearFitSolveFactors(double[] x, double[] y, out double gradient, out double coefficient)

Parameters

x double[]

X values

y double[]

Y values

gradient double

Line gradient

coefficient double

Coefficient, Y value of intersection where X = 0

Returns

bool

True if success

Remarks

X values array length must be equal to Y values array length

LinearRegression(double[], double[], double[])

Calculate linear line fit for given points

public static double[] LinearRegression(double[] xInFactorization, double[] yInFactorization, double[] xValuesToFit)

Parameters

xInFactorization double[]

X values that are used to calculate the factors. This is the point set where the regression line is fitted to.

yInFactorization double[]

Y values that are used to calculate the factors. Length must be equal to xInFactorization length.

xValuesToFit double[]

X values whose Y values are to be solved, using the factors

Returns

double[]

Fitted Y values, the length equals to xValuesToFit length. Returns null if failed.

MysteryCurve(int, double, double[,], double, double, double, double, int)

Mystery curve

public static void MysteryCurve(int counter, double iStep, double[,] points, double _xMax, double _yMax, double _yMin, double _xMin, int _iDeliveredPoints)

Parameters

counter int
param counter M:LightningChartLib.WinForms.Charting.MathRoutines.MysteryCurve(System.Int32,System.Double,System.Double[0:,0:],System.Double,System.Double,System.Double,System.Double,System.Int32)
iStep double
param iStep M:LightningChartLib.WinForms.Charting.MathRoutines.MysteryCurve(System.Int32,System.Double,System.Double[0:,0:],System.Double,System.Double,System.Double,System.Double,System.Int32)
points double[,]
param points M:LightningChartLib.WinForms.Charting.MathRoutines.MysteryCurve(System.Int32,System.Double,System.Double[0:,0:],System.Double,System.Double,System.Double,System.Double,System.Int32)
_xMax double
param _xMax M:LightningChartLib.WinForms.Charting.MathRoutines.MysteryCurve(System.Int32,System.Double,System.Double[0:,0:],System.Double,System.Double,System.Double,System.Double,System.Int32)
_yMax double
param _yMax M:LightningChartLib.WinForms.Charting.MathRoutines.MysteryCurve(System.Int32,System.Double,System.Double[0:,0:],System.Double,System.Double,System.Double,System.Double,System.Int32)
_yMin double
param _yMin M:LightningChartLib.WinForms.Charting.MathRoutines.MysteryCurve(System.Int32,System.Double,System.Double[0:,0:],System.Double,System.Double,System.Double,System.Double,System.Int32)
_xMin double
param _xMin M:LightningChartLib.WinForms.Charting.MathRoutines.MysteryCurve(System.Int32,System.Double,System.Double[0:,0:],System.Double,System.Double,System.Double,System.Double,System.Int32)
_iDeliveredPoints int
param _iDeliveredPoints M:LightningChartLib.WinForms.Charting.MathRoutines.MysteryCurve(System.Int32,System.Double,System.Double[0:,0:],System.Double,System.Double,System.Double,System.Double,System.Int32)

PointDistanceFromLine(PointFloat, PointFloat, PointFloat)

Calculates distance from line to point.

public static float PointDistanceFromLine(PointFloat p1, PointFloat p2, PointFloat p0)

Parameters

p1 PointFloat

Line point 1.

p2 PointFloat

Line point 2.

p0 PointFloat

Point.

Returns

float

Distance from line to point.

PointDistanceFromLine(PointFloat, PointFloat, PointFloat, out PointFloat, out float)

Calculates distance from line to point.

public static float PointDistanceFromLine(PointFloat p1, PointFloat p2, PointFloat p3, out PointFloat linePoint, out float coeff)

Parameters

p1 PointFloat

Line point 1.

p2 PointFloat

Line point 2.

p3 PointFloat

Point.

linePoint PointFloat

Point at line, which is nearest to of reference point.

coeff float

Normalized distance from p1 to linePoint.

Returns

float

Distance from line to point.

PointDistanceFromLineSegment(PointFloat, PointFloat, PointFloat)

Calculates distance from line segment to point.

public static float PointDistanceFromLineSegment(PointFloat p1, PointFloat p2, PointFloat p0)

Parameters

p1 PointFloat

Line point 1.

p2 PointFloat

Line point 2.

p0 PointFloat

Point.

Returns

float

Distance from line segment to point.

PointInsidePolygon(PointFloat, PointFloat[])

Check whether point is inside polygon. Algorithm from: http://www.ecse.rpi.edu/Homepages/wrf/Research/Short_Notes/pnpoly.html Copyright (c) 1970-2003, Wm. Randolph Franklin. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimers.
  2. Redistributions in binary form must reproduce the above copyright notice in the documentation and/or other materials provided with the distribution.
  3. The name of W. Randolph Franklin may not be used to endorse or promote products derived from this Software without specific prior written permission. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
public static bool PointInsidePolygon(PointFloat point, PointFloat[] polygon)

Parameters

point PointFloat

Point.

polygon PointFloat[]

Polygon points.

Returns

bool

True, if point is inside polygon, false otherwise.

PolynomialRegression(double[], double[], double[], int)

Calculate polymial line fit. Calculates polynomial factors, and uses these factors to solve Y values based on given X value array.

public static double[] PolynomialRegression(double[] xInFactorization, double[] yInFactorization, double[] xValuesToFit, int order)

Parameters

xInFactorization double[]

X values that are used to calculate the factors. This is the point set where the regression line is fitted to.

yInFactorization double[]

Y values that are used to calculate the factors. Length must be equal to xInFactorization length.

xValuesToFit double[]

X values whose Y values are to be solved, using the factors

order int

Order of polynomial. Usually more than 3 shouldn't be used

Returns

double[]

Fitted Y values, the length equals to xValuesToFit length. Returns null if failed.

PolynomialRegression(double[], double[], double[], int, out double[])

Calculate polymial line fit. Calculates polynomial factors, and uses these factors to solve Y values based on given X value array.

public static double[] PolynomialRegression(double[] xInFactorization, double[] yInFactorization, double[] xValuesToFit, int order, out double[] factors)

Parameters

xInFactorization double[]

X values that are used to calculate the factors. This is the point set where the regression line is fitted to.

yInFactorization double[]

Y values that are used to calculate the factors. Length must be equal to xInFactorization length.

xValuesToFit double[]

X values whose Y values are to be solved, using the factors

order int

Order of polynomial. Usually more than 3 shouldn't be used

factors double[]

Polynomial result factors, length = order+1. For example, when having order 2, you can plot line with equation: Y = factors[0] + factors[1]*X + factors[2]*X^2

Returns

double[]

Fitted Y values, the length equals to xValuesToFit length. Returns null if failed.

RadiansAsDegrees(double)

Convert radians to degrees.

public static double RadiansAsDegrees(double value)

Parameters

value double

Radians value.

Returns

double

Degrees value.

RadiansAsDegreesString(double)

Make radians as degrees string.

public static string RadiansAsDegreesString(double value)

Parameters

value double

Radians value.

Returns

string

Radians as degrees string.

RadiansAsGrads(double)

Convert radians to gradians.

public static double RadiansAsGrads(double value)

Parameters

value double

Radians value.

Returns

double

Gradians value.

RadiansAsGradsString(double)

Make radians as gradians string.

public static string RadiansAsGradsString(double value)

Parameters

value double

Radians value.

Returns

string

Radians as gradians string.

RadiansAsString(double)

Make radians as string.

public static string RadiansAsString(double value)

Parameters

value double

Radians value.

Returns

string

Radians as string.

RelativeDistanceOfPoints(PointFloat, PointFloat)

Calculates relative distance between two points.

public static float RelativeDistanceOfPoints(PointFloat point1, PointFloat point2)

Parameters

point1 PointFloat

Point 1.

point2 PointFloat

Point 2.

Returns

float

Relative distance between two points.

ReplaceNaNs(double[], double)

Replace NaN numbers with given value

public static void ReplaceNaNs(double[] array, double value)

Parameters

array double[]

Array to check and replace

value double

Value to store when NaN is found

ReplaceNaNs(float[], float)

Replace NaN numbers with given value

public static void ReplaceNaNs(float[] array, float value)

Parameters

array float[]

Array to check and replace

value float

Value to store when NaN is found

Rotate(PointFloat, double)

Rotate point.

public static PointFloat Rotate(PointFloat point, double angleRad)

Parameters

point PointFloat

Point (in math coordinate system).

angleRad double

Angle in radians.

Returns

PointFloat

Point in pixel coordinate system.

RoundToInt(double)

Convert double value to nearest integer. The fast way.

public static int RoundToInt(double x)

Parameters

x double

Value to convert

Returns

int

Nearest integer

RoundToInt(float)

Convert float value to nearest integer. The fast way.

public static int RoundToInt(float x)

Parameters

x float

Value to convert

Returns

int

Nearest integer

StandardDeviation(double[])

Calculate standard deviation of values

public static double StandardDeviation(double[] values)

Parameters

values double[]

values

Returns

double

Stardard deviation