C#一维空间简单操作

1、第一步,创建一维空间实体类:

public class XRange
{
    public double Max;
    public double Min;

    /// 
    /// 构造函数
    /// 
    /// 
    /// 
    public XRange(double a, double b)
    {

        if (a < b)
        {
            this.Min = a;
            this.Max = b;
        }
        else
        {
            this.Min = b;
            this.Max = a;
        }
    }

    /// 
    /// 中位数
    /// 
    /// 
    public double Mid()
    {
        return this.Min + 0.5 * (this.Max - this.Min);
    }

    /// 
    /// 长度
    /// 
    /// 
    public double Length()
    {
        return this.Max - this.Min;
    }

    /// 
    /// 字符串
    /// 
    /// 
    public override string ToString()
    {
        return "[" + this.Min + "," + this.Max + "]";
    }
    
    /// 
    /// 当前区间是否包含指定值
    /// 
    /// 目标值
    /// 容差值
    /// 
    public bool In(double value, double tol = 0)
    {
        if (value >= this.Min - tol && value <= this.Max + tol)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    /// 
    /// 区间合并,相交区间才合并
    /// 
    /// 
    /// 不相交区间返回false,相交区间合并后返回true
    public bool Merge(XRange range)
    {
        if (range.Max < this.Min || range.Min > this.Max)
        {
            return false;
        }
        else
        {
            this.Min = Math.Min(this.Min, range.Min);
            this.Max = Math.Max(this.Max, range.Max);
            return true;
        }
    }

    /// 
    /// 一维区间加值
    /// 
    /// 
    /// 
    /// 
    public static XRange operator +(XRange range, double a)
    {
        XRange p = new XRange(range.Min + a, range.Max + a);
        return p;
    }

    /// 
    /// 一维区间减值
    /// 
    /// 
    /// 
    /// 
    public static XRange operator -(XRange range, double a)
    {
        XRange p = new XRange(range.Min - a, range.Max - a);
        return p;
    }

    /// 
    /// 一维区间乘值
    /// 
    /// 
    /// 
    /// 
    public static XRange operator *(XRange range, double a)
    {
        XRange p = new XRange(range.Min * a, range.Max * a);
        return p;
    }

    /// 
    /// 计算跃迁值(最大间隔范围的上限值)
    /// 
    /// 
    /// 
    public static int GetMaxStepValue(int[] nums)
    {
        if (nums.Length > 0)
        {
            XSort.QuickSort(nums);
            int value = nums[0];
            int sect = 0;
            for (int i = 1; i < nums.Length; i++)
            {
                XRange range = new XRange(nums[i - 1], nums[i]);
                if (nums[i] - nums[i - 1] >= sect)
                {
                    sect = nums[i] - nums[i - 1];
                    value = nums[i];
                }
            }
            return value;
        }
        return int.MinValue;
    }

    /// 
    /// 排序规则
    /// 
    /// 排序元素a
    /// 排序元素b
    /// 
    public static bool CompareByMin(XRange a, XRange b)
    {
        return a.Min > b.Min;
    }
}

2、第二步,一维空间集合操作类:

public class XRangeList
{
    /// 
    /// 构造函数
    /// 
    public XRangeList()
    {
        _values = new List();
    }

    private List _values;
    /// 
    /// 区间集合
    /// 
    public List List
    {
        get
        {
            return _values;
        }
        set { _values = value; }
    }

    /// 
    /// 指定值是否在集合中的某个区间内
    /// 
    /// 指定值
    /// 容差值
    /// 
    public bool In(double value, double tol = 0)
    {
        foreach (XRange xRange in _values)
        {
            if (xRange.In(value, tol))
            {
                return true;
            }
        }
        return false;
    }

    /// 
    /// 排序
    /// 
    public void Sort()
    {
        XSort.Sort(_values, XRange.CompareByMin);
    }

    /// 
    /// 反序
    /// 
    public void Reverse()
    {
        _values.Reverse();
    }

    /// 
    /// 范围合并
    /// 
    /// 
    /// 返回合并类型
    public int Merge(XRange range)
    {
        foreach (XRange xRange in _values)
        {
            if (xRange.Merge(range))
            {
                return 1;
            }
        }
        _values.Add(range);
        return 2;
    }

    /// 
    /// 深度合并
    /// 
    /// 区间集合
    /// 容差值
    /// 
    public static List Merge(List intervals, double tol = 0)
    {
        if (intervals.Count <= 1)
        {
            return intervals;
        }

        //对intervals排序
        XSort.Sort(intervals, XRange.CompareByMin);

        List resultList = new List();
        XRange cur_interval = intervals[0];
        XRange next_interval = null;
        for (int i = 1; i < intervals.Count; i++)
        {
            next_interval = intervals[i];
            if (cur_interval.Max < next_interval.Min - tol)//增加容差
            {
                //不相交,不能合并
                resultList.Add(cur_interval);
                cur_interval = next_interval;
            }
            else if (cur_interval.Max >= next_interval.Min - tol && cur_interval.Max <= next_interval.Max)//增加容差
            {
                cur_interval.Max = next_interval.Max;
            }
        }
        resultList.Add(cur_interval);
        return resultList;
    }
}

3、第三步,通用排序算法帮助类:

public static class XSort
{
    #region 系统的快速排序
    /// 
    /// 系统的快速排序算法
    /// 
    /// 
    public static void QuickSortSystem(int[] array)
    {
        Array.Sort(array);
    }

    public static void QuickSort(int[] array)
    {
        QuickSort(array, 0, array.Length - 1);
    }

    public static void QuickSort(int[] array, int left, int right)
    {
        if (left < right)
        {
            int piv = Partition(array, left, right);
            QuickSort(array, left, piv - 1);
            QuickSort(array, piv + 1, right);
        }
    }

    private static int Partition(int[] array, int low, int high)
    {
        int piv = low; int pivalue = array[low]; //将第一个对象作为种子对象
        for (int i = low + 1; i <= high; i++)
        {
            if (array[i] < pivalue && ++piv != i)
                Swap(ref array[piv], ref array[i]);
        }
        Swap(ref array[low], ref array[piv]);

        return piv;
    }

    private static void Swap(ref int a, ref int b)
    {
        int c = a;
        a = b;
        b = c;
    }
    #endregion

    /// 
    /// 泛型比较委托
    /// 
    /// 
    /// 
    /// 
    /// 
    public delegate bool Compare(T t1, T t2);

    /// 
    /// 泛型冒泡排序
    /// 
    /// 
    /// 
    /// 
    public static void Sort(IList items, XSort.Compare compare)
    {
        if (items == null)
            return;
        for (int i = 0; i < items.Count; i++)
        {
            for (int j = i + 1; j < items.Count; j++)
            {
                if (compare(items[i], items[j]))
                {
                    var temp = items[i];
                    items[i] = items[j];
                    items[j] = (T)temp;
                }
                //compare > 则顺序,<则逆序   www.Rsion.com
            }
        }
    }
}

4、第四步,简单的应用示例:

public class XLine
{
    public XPoint StartPt { get; set; }

    public XPoint EndPt { get; set; }
}

public class XPoint
{
    public double X { get; set; }

    public double Y { get; set; }
}

public class RangeTest
{
    public void Example()
    {
        List lineList = new List();
        //集合赋值不做展示....

        XRangeList xRangeList = new XRangeList();
        foreach (XLine xLine in lineList)
        {
            XRange xRange = new XRange(xLine.StartPt.X, xLine.EndPt.X);
            xRangeList.Merge(xRange);
        }
        xRangeList.List = XRangeList.Merge(xRangeList.List);
        xRangeList.Sort();
    }
}

你可能感兴趣的:(c#,算法)