# 2.1.23.2.1 ocmath_check_linear

## Description

Find linear segment of a curve by piecewise linear fit.

## Syntax

int ocmath_check_linear( UINT lSize, const double * px, const double * py, int * pNonlinearPoint, double dMaxRatio, double dR = 1.0, UINT nOffset = 0, bool bDir = true, int nRange0 = 0, int nStep = 0 )

## Parameters

lSize
[input] size of px, py
px
[input] it contains curve's X coordinate's datas
py
[input] it contains curve's Y coordinate's datas
pNonlinearPoint
[output] pointer to the indice of first point that the continuous linear test failed
dMaxRatio
[input] maximum ratio of the curve to be tested, 0.5 means that half of the data will be checked
dR
[input] tolerance factor to multiply the slope/intercept error value for continuous linear test
nOffset
[input] offset of start, default value is 0
bDir
[input] true will inrement to do fit, false will decrement from end of curve to do fit
nRange0
[input] Number of points used to fit a line, if negative, use nStep*3 if take default value( = 0)
nStep
[input] The increment of the number of searched points.
if nStep<= 0, means internally use the (dMaxRatio * data size)/10;
if 0 < nStep < 1, means inernally take 1 points as step.

## Return

Return OE_NOERROR if succeed, otherwise, non-zero error code is returned.

## Examples

EX1

//Before running, make sure the active layer is a graph with the curve to check.
void ocmath_check_linear_ex1()
{
GraphLayer gl = Project.ActiveLayer();
if (!gl)
{
return;
}

DataPlot dp = gl.DataPlots(0);
DataRange dr;
vector vxData, vyData;
if(dp.GetDataRange(dr, 0, -1))
{
DWORD dwPlotID;
if(dr.GetData(DRR_GET_DEPENDENT | DRR_NO_FACTORS, 0, &dwPlotID, NULL, &vyData, &vxData) < 0)
printf("get_plot_data failed GetData");
return;
}

uint nDataSize = vxData.GetSize();
uint lSize = vxData.GetSize();

int pNonlinearPoint;
double dMaxRatio = 0.5;
int nRet = ocmath_check_linear(lSize, vxData, vyData, &pNonlinearPoint, dMaxRatio);
if( nRet < OE_NOERROR )
{
printf("error code: %d\n", nRet);
return;
}
}

## Remark

Find linear segment of a curve by piecewise linear fit. Test is to compare initial slope/intercept with subsequent values and using the error estimate from the linear fit as the base measure for tolerance. The continuous linear test is as follows:

abs(vn - v0) > vErr*dR

where vn is measure over a segment length of 3 increment size, while increment is 1/10 of the test data size. v0 is measure from the beginning up to and before the current test segment.