public float GetMembership( float x )
{
// no values belong to the fuzzy set, if there are no points in the piecewise function
if ( points.Length == 0 )
return 0.0f;
// if X value is less than the first point, so first point's Y will be returned as membership
if ( x < points[0].X )
return points[0].Y;
// looking for the line that contais the X value
for ( int i = 1, n = points.Length; i < n; i++ )
{
// the line with X value starts in points[i-1].X and ends at points[i].X
if ( x < points[i].X )
{
// points to calculate line's equation
float y1 = points[i].Y;
float y0 = points[i - 1].Y;
float x1 = points[i].X;
float x0 = points[i - 1].X;
// angular coefficient
float m = ( y1 - y0 ) / ( x1 - x0 );
// returning the membership - the Y value for this X
return m * ( x - x0 ) + y0;
}
}
// X value is more than last point, so last point Y will be returned as membership
return points[points.Length - 1].Y;
}
}