Example #1
0
 public object[]? ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
 {
     if (!(value is bool b))
     {
         return(null);
     }
     return(Array_.New <object>(targetTypes.Length, i => b));
 }
Example #2
0
 public Compound()
 {
     Elem1          = new Element();
     Elem2          = new Element();
     Count1         = 0;
     Count2         = 0;
     CommonName     = UnknownCompoundName;
     ScientificName = UnknownCompoundName;
     SymbolicName   = UnknownCompoundSymName;
     Bonds          = Array_.New <Bond>(EPerm.Perm2Count);
     Ionicity       = 0;
     Discovered     = false;
 }
Example #3
0
        /// <summary>Creates a bitmap containing a checker board where each 'checker' is 'sx' x 'sy'</summary>
        public static Image BitmapChecker(int sx, int sy, uint X = 0xFFFFFFFFU, uint O = 0x00000000U)
        {
            var w    = 2 * sx;
            var h    = 2 * sy;
            var bmp  = new Bitmap(w, h, PixelFormat.Format32bppArgb);
            var row0 = Array_.New(sx, unchecked ((int)O));
            var row1 = Array_.New(sx, unchecked ((int)X));

            using (var bits = bmp.LockBits(ImageLockMode.WriteOnly))
            {
                for (int j = 0; j != sy; ++j)
                {
                    Marshal.Copy(row0, 0, bits.Value.Scan0 + ((j + 0) * w) * sizeof(int), row0.Length);
                    Marshal.Copy(row1, 0, bits.Value.Scan0 + ((j + 0) * w + sx) * sizeof(int), row1.Length);
                    Marshal.Copy(row1, 0, bits.Value.Scan0 + ((j + sy) * w) * sizeof(int), row1.Length);
                    Marshal.Copy(row0, 0, bits.Value.Scan0 + ((j + sy) * w + sx) * sizeof(int), row0.Length);
                }
            }
            return(bmp);
        }
Example #4
0
        /// <summary>Sort 'list' into a KD Tree</summary>
        /// <param name="list">The list to be sorted into a KD tree</param>
        /// <param name="dimensions">The number of dimensions to sort on (typically 2 or 3)</param>
        /// <param name="AxisValueGet">Callback that returns the value of the given element on the given dimension</param>
        /// <param name="SortAxisSet">Callback that sets the axis to sort on for the given element</param>
        public static void Build <T>(IList <T> list, int dimensions, Func <T, int, double> AxisValueGet, Func <T, int, T> SortAxisSet)
        {
            BuildTree(0, list.Count);

            // Helpers
            void BuildTree(int beg, int end)
            {
                if (end - beg <= 1)
                {
                    return;
                }

                // Partition the range at the mid point of the longest axis
                var split_axis  = LongestAxis(beg, end);
                var split_point = MedianSplit(beg, end, split_axis);

                // SortAxisSet must return the updated element because it may be a struct.
                list[split_point] = SortAxisSet(list[split_point], split_axis);

                // Recursively build each half of the remaining data
                BuildTree(beg, split_point);
                BuildTree(split_point + 1, end);
            }

            int LongestAxis(int beg, int end)
            {
                // Found the bounds on each axes of the range [beg, end)
                var lower = Array_.New(dimensions, i => double.MaxValue);
                var upper = Array_.New(dimensions, i => double.MinValue);

                for (var i = beg; i != end; ++i)
                {
                    for (var a = 0; a != dimensions; ++a)
                    {
                        var value = AxisValueGet(list[i], a);
                        lower[a] = Math.Min(lower[a], value);
                        upper[a] = Math.Max(upper[a], value);
                    }
                }

                // Return the axis with the greatest range
                var largest = 0;

                for (var a = 1; a != dimensions; ++a)
                {
                    if (upper[a] - lower[a] < upper[largest] - lower[largest])
                    {
                        continue;
                    }
                    largest = a;
                }
                return(largest);
            }

            int MedianSplit(int first, int last, int split_axis)
            {
                // Ensure that the element at the centre of the range has only values less than it on
                // the left and values greater or equal than it on the right, where the values are the
                // component of the axis to split on.
                var split_point = first + (last - first) / 2;
                var cmp         = Cmp <T> .From((lhs, rhs) => AxisValueGet(lhs, split_axis) < AxisValueGet(rhs, split_axis)? -1 : 1);

                list.NthElement(split_point, first, last, cmp);
                return(split_point);
            }
        }
Example #5
0
 public ShipSpec()
 {
     Fuel         = Array_.New <FuelSpec>(2);
     HullCompound = new Compound();
 }