CanvasCommon.DensityClusteringModel.EstimateDc C# (CSharp) Method

EstimateDc() public method

neighborRate = average of number of elements of comb per row that are less than dc minus 1 divided by size
public EstimateDc ( double neighborRateLow = NeighborRateLow, double neighborRateHigh = NeighborRateHigh ) : double
neighborRateLow double
neighborRateHigh double
return double
        public double EstimateDc(double neighborRateLow = NeighborRateLow, double neighborRateHigh = NeighborRateHigh)
        {
            double tmpLow = Double.MaxValue;
            double tmpHigh = Double.MinValue;
            foreach (double? element in this.Distance)
            {
                if (element.HasValue && element < tmpLow && element > 0)
                    tmpLow = (double) element;
                else if (element.HasValue && element > tmpHigh)
                    tmpHigh = (double) element;
            }

            double neighborRate = 0;
            int segmentsLength = GetSegmentsForClustering(this.Segments);
            double distanceThreshold = 0;
            while (true)
            {
                double neighborRateTmp = 0;
                distanceThreshold = (tmpLow + tmpHigh) / 2;
                foreach (double? element in this.Distance)
                    if (element.HasValue && element < distanceThreshold)
                        neighborRateTmp++;
                if (distanceThreshold > 0)
                    neighborRateTmp = neighborRateTmp + segmentsLength;

                neighborRate = (neighborRateTmp * 2 / segmentsLength - 1) / segmentsLength;

                if (neighborRate >= neighborRateLow && neighborRate <= neighborRateHigh)
                    break;

                if (neighborRate < neighborRateLow)
                {
                    tmpLow = distanceThreshold;
                }
                else
                {
                    tmpHigh = distanceThreshold;
                }
                neighborRateTmp = 0;
            }
            return distanceThreshold;
        }