Esempio n. 1
0
        public Quaternion(ref Matrix3 matrix)
        {
            var   scale = Math.Pow(matrix.Determinant, 1.0d / 3.0d);
            float x, y, z;

            w = (float)(Math.Sqrt(Math.Max(0, scale + matrix[0, 0] + matrix[1, 1] + matrix[2, 2])) / 2);
            x = (float)(Math.Sqrt(Math.Max(0, scale + matrix[0, 0] - matrix[1, 1] - matrix[2, 2])) / 2);
            y = (float)(Math.Sqrt(Math.Max(0, scale - matrix[0, 0] + matrix[1, 1] - matrix[2, 2])) / 2);
            z = (float)(Math.Sqrt(Math.Max(0, scale - matrix[0, 0] - matrix[1, 1] + matrix[2, 2])) / 2);

            xyz = new Vector3(x, y, z);

            if (matrix[2, 1] - matrix[1, 2] < 0)
            {
                X = -X;
            }
            if (matrix[0, 2] - matrix[2, 0] < 0)
            {
                Y = -Y;
            }
            if (matrix[1, 0] - matrix[0, 1] < 0)
            {
                Z = -Z;
            }
        }
Esempio n. 2
0
        public static bool CloseTo(double a, double b, double tolerance = .00001)
        {
            var epsilon =
                Math.Max(Math.Max(Math.Abs(a), Math.Abs(b)) * tolerance,
                         tolerance); // .001% of the smaller value for the epsilon check as per MSDN reference suggestion

            return(Math.Abs(a - b) <= epsilon);
        }
Esempio n. 3
0
 public int GetHeatResistance()
 {
     if (Owner.GetComponent <InventoryComponent>().TryGetSlotItem(EquipmentSlotDefines.Slots.GLOVES, itemComponent: out ClothingComponent gloves)
         | Owner.TryGetComponent(out SpeciesComponent speciesComponent))
     {
         return(Math.Max(gloves?.HeatResistance ?? int.MinValue, speciesComponent?.HeatResistance ?? int.MinValue));
     }
     return(int.MinValue);
 }
Esempio n. 4
0
        protected override void LayoutUpdateOverride()
        {
            var separation = (int)(ActualSeparation * UIScale);

            // Step one: figure out the sizes of all our children and whether they want to stretch.
            var sizeList          = new List <(Control control, int minSize, int finalSize, bool stretch)>(ChildCount);
            var totalStretchRatio = 0f;
            // Amount of space not available for stretching.
            var stretchMin = 0;

            foreach (var child in Children)
            {
                if (!child.Visible)
                {
                    continue;
                }
                var(minX, minY) = child.CombinedPixelMinimumSize;
                int  minSize;
                bool stretch;

                if (Vertical)
                {
                    minSize = minY;
                    stretch = (child.SizeFlagsVertical & SizeFlags.Expand) == SizeFlags.Expand;
                }
                else
                {
                    minSize = minX;
                    stretch = (child.SizeFlagsHorizontal & SizeFlags.Expand) == SizeFlags.Expand;
                }

                if (!stretch)
                {
                    stretchMin += minSize;
                }
                else
                {
                    totalStretchRatio += child.SizeFlagsStretchRatio;
                }

                sizeList.Add((child, minSize, minSize, stretch));
            }

            var stretchMax = Vertical ? PixelHeight : PixelWidth;

            stretchMax -= separation * (ChildCount - 1);
            // This is the amount of space allocated for stretchable children.
            var stretchAvail = Math.Max(0, stretchMax - stretchMin);

            // Step two: figure out which that want to stretch need to suck it,
            // because due to their stretch ratio they would be smaller than minSize.
            // Treat those as non-stretching.
            for (var i = 0; i < sizeList.Count; i++)
            {
                var(control, minSize, _, stretch) = sizeList[i];
                if (!stretch)
                {
                    continue;
                }

                var share = (int)(stretchAvail * (control.SizeFlagsStretchRatio / totalStretchRatio));
                if (share < minSize)
                {
                    sizeList[i]        = (control, minSize, minSize, false);
                    stretchAvail      -= minSize;
                    totalStretchRatio -= control.SizeFlagsStretchRatio;
                }
            }

            // Step three: allocate space for all the stretchable children.
            var stretchingAtAll = false;

            for (var i = 0; i < sizeList.Count; i++)
            {
                var(control, minSize, _, stretch) = sizeList[i];
                if (!stretch)
                {
                    continue;
                }

                stretchingAtAll = true;

                var share = (int)(stretchAvail * (control.SizeFlagsStretchRatio / totalStretchRatio));
                sizeList[i] = (control, minSize, share, false);
            }

            // Step four: actually lay them out one by one.
            var offset = 0;

            if (!stretchingAtAll)
            {
                switch (Align)
                {
                case AlignMode.Begin:
                    break;

                case AlignMode.Center:
                    offset = stretchAvail / 2;
                    break;

                case AlignMode.End:
                    offset = stretchAvail;
                    break;

                default:
                    throw new ArgumentOutOfRangeException();
                }
            }

            var first = true;

            foreach (var(control, _, size, _) in sizeList)
            {
                if (!first)
                {
                    offset += separation;
                }

                first = false;

                UIBox2i targetBox;
                if (Vertical)
                {
                    targetBox = new UIBox2i(0, offset, PixelWidth, offset + size);
                }
                else
                {
                    targetBox = new UIBox2i(offset, 0, offset + size, PixelHeight);
                }

                FitChildInPixelBox(control, targetBox);

                offset += size;
            }
        }