public float GetNoiseTiled(Vector2 vec) { var s = vec.X / _periodX; var t = vec.Y / _periodY; const float x1 = 0; const float x2 = 1; const float y1 = 0; const float y2 = 1; const float dx = x2 - x1; const float dy = y2 - y1; const float tau = (float)Math.TAU; //(float)Math.PI * 2; const float dxTau = dx / tau; const float dyTau = dy / tau; var nx = x1 + (float)Math.Cos(s * tau) * dxTau; var ny = y1 + (float)Math.Cos(t * tau) * dyTau; var nz = x1 + (float)Math.Sin(s * tau) * dxTau; var nw = y1 + (float)Math.Sin(t * tau) * dyTau; return(GetNoise(nx, ny, nz, nw)); }
/// <summary> /// Converts this angle to a unit direction vector. /// </summary> /// <returns>Unit Direction Vector</returns> public Vector2 ToVec() { var x = Math.Cos(Theta); var y = Math.Sin(Theta); return(new Vector2((float)x, (float)y)); }
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; } }
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); }
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); }
private const double CardinalOffset = CardinalSegment / 2.0; // offset the pieces by 1/2 their size public Direction GetCardinalDir() { var ang = Theta % (2 * Math.PI); if (ang < 0.0f) // convert -PI > PI to 0 > 2PI { ang += 2 * (float)Math.PI; } return((Direction)(Math.Floor((ang + CardinalOffset) / CardinalSegment) * 2 % 8)); }
public static Matrix3 CreateRotation(Angle angle) { var cos = (float)Math.Cos(angle); var sin = (float)Math.Sin(angle); var result = Identity; /* column major * cos -sin 0 * sin cos 0 * 0 0 1 */ result.R0C0 = cos; result.R1C0 = sin; result.R0C1 = -sin; result.R1C1 = cos; return(result); }
public static double NextPowerOfTwo(double n) { if (double.IsNaN(n) || double.IsInfinity(n)) { throw new ArgumentOutOfRangeException(nameof(n), "Must be a number."); } if (n <= 0) { throw new ArgumentOutOfRangeException(nameof(n), "Must be positive."); } // Don't return negative powers of two, that's nonsense. if (n < 1) { return(1.0); } return(Math.Pow(2, Math.Floor(Math.Log(n, 2)) + 1)); }
/// <summary> /// calculates the smallest AABB that will encompass the rotated box. The AABB is in local space. /// </summary> public Box2 CalcBoundingBox() { // https://stackoverflow.com/a/19830964 var(X0, Y0) = Box.BottomLeft; var(X1, Y1) = Box.TopRight; var Fi = Rotation.Theta; var CX = (X0 + X1) / 2; //Center point var CY = (Y0 + Y1) / 2; var WX = (X1 - X0) / 2; //Half-width var WY = (Y1 - Y0) / 2; var SF = Math.Sin(Fi); var CF = Math.Cos(Fi); var NH = Math.Abs(WX * SF) + Math.Abs(WY * CF); //boundrect half-height var NW = Math.Abs(WX * CF) + Math.Abs(WY * SF); //boundrect half-width return(new Box2((float)(CX - NW), (float)(CY - NH), (float)(CX + NW), (float)(CY + NH))); //draw bound rectangle }
public static double Mod(double n, double d) { return(n - Math.Floor(n / d) * d); }
public static double Lerp(double a, double b, double blend) { //return a + (b - a) * blend; return(Math.Interpolate(a, b, blend)); }
public static double Clamp(this double val, double min, double max) { return(Math.Clamp(val, min, max)); }
public static float Floor(float f) => (float)Math.Floor(f);
public static double InterpolateCubic(double preA, double a, double b, double postB, double t) { //return a + 0.5 * t * (b - preA + t * (2.0 * preA - 5.0 * a + 4.0 * b - postB + t * (3.0 * (a - b) + postB - preA))); return(Math.CubicInterpolate(preA, a, b, postB, t)); }
/// <summary> /// Constructs an instance of an angle from an (un)normalized direction vector. /// </summary> /// <param name="dir"></param> public Angle(Vector2 dir) { dir = dir.Normalized; Theta = Math.Atan2(dir.Y, dir.X); }
/// <summary> /// Converts a direction vector to an angle, where angle is -PI to +PI. /// </summary> /// <param name="vec">Vector to get the angle from.</param> /// <returns>Angle of the vector.</returns> public static Angle ToAngle(this Vector2 vec) { return(Math.Atan2(vec.Y, vec.X)); }
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; } }