Beispiel #1
0
 public bool Equals(Ellipse other)
 {
     return(
         Mathx.IsEqual(centre, other.centre) &&
         Mathx.IsEqual(extents, other.extents)
         );
 }
Beispiel #2
0
 public bool Equals(Range3Int other)
 {
     return(
         Mathx.IsEqual(min, other.min) &&
         Mathx.IsEqual(max, other.max)
         );
 }
Beispiel #3
0
 public bool Equals(Line other)
 {
     return(
         Mathx.IsEqual(v0, other.v0) &&
         Mathx.IsEqual(v1, other.v1)
         );
 }
Beispiel #4
0
 public bool Equals(LinearEquation other)
 {
     return(
         Mathx.IsEqual(this.a, other.a) &&
         Mathx.IsEqual(this.b, other.b)
         );
 }
Beispiel #5
0
 /// <summary> Calculates rectangle vertices into 'target' array </summary>
 public static void GetVertices(Vector2[] target, Vector2 position, Vector2 size, float angle)
 {
     for (int i = 0; i < Vertices.Length; ++i)
     {
         target[i] = Mathx.Transform(Vertices[i], position, angle, size);
     }
 }
Beispiel #6
0
 public bool Equals(Sphere other)
 {
     return(
         Mathx.IsEqual(centre, other.centre) &&
         Mathx.IsEqual(radius, other.radius)
         );
 }
Beispiel #7
0
 public bool Equals(Equation1 other)
 {
     return(
         Mathx.IsEqual(this.x, other.x) &&
         Mathx.IsEqual(this.y, other.y)
         );
 }
Beispiel #8
0
        /// <summary> Attempts to calculate circle with center in 'c' and radius 'r' from three points </summary>
        public static bool TryCreate(Vector2 v0, Vector2 v1, Vector2 v2, out Vector2 c, out float r)
        {
            c = default;
            r = default;

            var c1 = new Vector4(v0.x * v0.x + v0.y * v0.y, v1.x * v1.x + v1.y * v1.y, v2.x * v2.x + v2.y * v2.y, 0.0f);
            var c2 = new Vector4(v0.x, v1.x, v2.x, 0.0f);
            var c3 = new Vector4(v0.y, v1.y, v2.y, 0.0f);
            var c4 = new Vector4(1.0f, 1.0f, 1.0f, 0.0f);
            var c5 = new Vector4(0.0f, 0.0f, 0.0f, 1.0f);

            var M11 = new Matrix4x4(c2, c3, c4, c5);
            var M12 = new Matrix4x4(c1, c3, c4, c5);
            var M13 = new Matrix4x4(c1, c2, c4, c5);
            var M14 = new Matrix4x4(c1, c2, c3, c5);

            var m11 = M11.determinant;
            var m12 = M12.determinant;
            var m13 = M13.determinant;
            var m14 = M14.determinant;

            if (Mathx.IsZero(m11))
            {
                return(false);
            }

            c.x = 0.5f * m12 / m11;
            c.y = -0.5f * m13 / m11;
            r   = Mathf.Sqrt(c.x * c.x + c.y * c.y + m14 / m11);
            return(true);
        }
Beispiel #9
0
 public bool Contains(Vector3Int otherMin, Vector3Int otherMax)
 {
     return(
         Mathx.IsLesserOrEqual(min, otherMin) &&
         Mathx.IsLesserOrEqual(otherMax, max)
         );
 }
Beispiel #10
0
 /// <summary> Calculates rectangle vertices into 'target' array </summary>
 public static void GetVertices(Vector3[] target, Vector3 position, Vector2 size, Quaternion rotation)
 {
     for (int i = 0; i < Vertices.Length; ++i)
     {
         target[i] = Mathx.Transform(Vertices[i], position, rotation, size);
     }
 }
Beispiel #11
0
 public bool Overlaps(Range3Int other)
 {
     return(
         Mathx.IsLesserOrEqual(min, other.max) &&
         Mathx.IsLesserOrEqual(other.min, max)
         );
 }
Beispiel #12
0
 public Range3Int Intersection(Range3Int other)
 {
     return(new Range3Int(
                Mathx.Max(min, other.min),
                Mathx.Min(max, other.max)
                ));
 }
Beispiel #13
0
 public bool Equals(Triangle other)
 {
     return(
         Mathx.IsEqual(v0, other.v0) &&
         Mathx.IsEqual(v1, other.v1) &&
         Mathx.IsEqual(v2, other.v2)
         );
 }
 public bool Equals(QuadraticEquation other)
 {
     return(
         Mathx.IsEqual(this.a, other.a) &&
         Mathx.IsEqual(this.b, other.b) &&
         Mathx.IsEqual(this.c, other.c)
         );
 }
Beispiel #15
0
        public static bool Contains(Vector3 v0, Vector3 v1, Vector3 p)
        {
            float v0v1 = (v1 - v0).sqrMagnitude;
            float v0p  = (p - v0).sqrMagnitude;
            float pv1  = (v1 - p).sqrMagnitude;

            return(Mathx.IsEqual(v0v1, v0p + pv1));
        }
Beispiel #16
0
 public bool Equals(SinEquation other)
 {
     return(
         Mathx.IsEqual(this.dx, other.dx) &&
         Mathx.IsEqual(this.sx, other.sx) &&
         Mathx.IsEqual(this.dy, other.dy) &&
         Mathx.IsEqual(this.sy, other.sy)
         );
 }
Beispiel #17
0
        public static bool Contains(Vector3 v0, Vector3 v1, Vector3 v2, Vector3 p)
        {
            var bc = Barycenter(v0, v1, v2, p);

            return(
                Mathx.IsEqual(bc.x + bc.y + bc.z, 1.0f) &&
                Mathx.IsInRange(bc, Vector3.zero, Vector3.one)
                );
        }
Beispiel #18
0
 public bool Equals(Equation3 other)
 {
     return(
         Mathx.IsEqual(this.x, other.x) &&
         Mathx.IsEqual(this.y, other.y) &&
         Mathx.IsEqual(this.z, other.z) &&
         Mathx.IsEqual(this.w, other.w)
         );
 }
Beispiel #19
0
        public static LinearEquation FromPoints(Vector2 A, Vector2 B)
        {
            if (Mathx.IsZero(A.x) && Mathx.IsZero(B.x))
            {
                return(invalid);
            }

            float a = (B.y - A.y) / (B.x - A.x);
            float b = A.y - a * A.x;

            return(new LinearEquation(a, b));
        }
Beispiel #20
0
        public static void GetNoiseMap(
            float[][] map, int dx = 0, int dy            = 0,
            int octaves           = 1, float persistance = 0.5f, float lacunarity = 2.0f,
            float sx = 1.0f, float sy                    = 1.0f, int seed = 0
            )
        {
            var random = new Random(seed);

            var width  = map.GetWidth();
            var height = map.GetHeight();

            var pmin = 0.0f;
            var pmax = 1.0f;

            for (uint i = 1; i < octaves; ++i)
            {
                var ppow = Mathx.Pow(persistance, i);
                pmin += ppow * 0.25f;
                pmax += ppow * 0.75f;
            }

            var rdx = random.NextFloat(-99999.0f, +99999.0f);
            var rdy = random.NextFloat(-99999.0f, +99999.0f);

            var fx = 1.0f / (sx * width);
            var fy = 1.0f / (sy * height);

            for (int y = 0; y < height; ++y)
            {
                for (int x = 0; x < width; ++x)
                {
                    float amplitude = 1.0f;
                    float frequency = 1.0f;
                    float value     = 0.0f;

                    for (int i = 0; i < octaves; ++i)
                    {
                        float sampleX = ((x - dx) * fx) * frequency + rdx;
                        float sampleY = ((y - dy) * fy) * frequency + rdy;

                        float pv = Mathf.PerlinNoise(sampleX, sampleY);
                        value += pv * amplitude;

                        amplitude *= persistance;
                        frequency *= lacunarity;
                    }

                    map[x][y] = Mathf.SmoothStep(pmin, pmax, value);
                }
            }
        }
Beispiel #21
0
 public static Color ToNormal(Vector3 v)
 {
     return(ToColor((Mathx.Add(v.XZY(), 1.0f)) * 0.5f));
 }
Beispiel #22
0
        /// <summary> Attempts to calculate a sphere with center in 'c' and radius 'r' from four points </summary>
        public static bool TryCreate(Vector3 v0, Vector3 v1, Vector3 v2, Vector3 v3, out Vector3 c, out float r)
        {
            c = default;
            r = default;

            var p = new float[4, 4]
            {
                { v0.x, v0.y, v0.z, 1.0f },
                { v1.x, v1.y, v1.z, 1.0f },
                { v2.x, v2.y, v2.z, 1.0f },
                { v3.x, v3.y, v3.z, 1.0f }
            };
            var a = new float[4, 4];

            // Find minor 1, 1.
            for (int i = 0; i < 4; i++)
            {
                a[i, 0] = p[i, 0];
                a[i, 1] = p[i, 1];
                a[i, 2] = p[i, 2];
            }
            float detM11 = Mathx.Determinant(a);

            if (Mathx.IsZero(detM11))
            {
                return(false);
            }

            for (int i = 0; i < 4; i++)
            {
                a[i, 0] = p[i, 0] * p[i, 0] + p[i, 1] * p[i, 1] + p[i, 2] * p[i, 2];
            }

            for (int i = 0; i < 4; i++)
            {
                a[i, 1] = p[i, 1];
                a[i, 2] = p[i, 2];
            }
            float detM12 = Mathx.Determinant(a);

            for (int i = 0; i < 4; i++)
            {
                a[i, 1] = p[i, 0];
                a[i, 2] = p[i, 2];
            }
            float detM13 = Mathx.Determinant(a);

            for (int i = 0; i < 4; i++)
            {
                a[i, 1] = p[i, 0];
                a[i, 2] = p[i, 1];
            }
            float detM14 = Mathx.Determinant(a);

            for (int i = 0; i < 4; i++)
            {
                a[i, 1] = p[i, 0];
                a[i, 2] = p[i, 1];
                a[i, 3] = p[i, 2];
            }
            float detM15 = Mathx.Determinant(a);

            c.x = 0.5f * detM12 / detM11;
            c.y = -0.5f * detM13 / detM11;
            c.z = 0.5f * detM14 / detM11;
            r   = Mathf.Sqrt(c.x * c.x + c.y * c.y + c.z * c.z - detM15 / detM11);
            return(true);
        }
Beispiel #23
0
        public static float PerlinNoise(float x, float y, float z)
        {
            const float MOD     = 289.0f;
            const float PERMUTE = 34.0f;

            Vector3 P   = new Vector3(x, y, z);
            Vector3 Pi0 = Mathx.Floor(P);    // Integer part for indexing
            Vector3 Pi1 = Pi0 + Vector3.one; // Integer part + 1

            Pi0 = Mathx.Mod(Pi0, MOD);
            Pi1 = Mathx.Mod(Pi1, MOD);
            Vector3 Pf0 = Mathx.Frac(P);     // Fractional part for interpolation
            Vector3 Pf1 = Pf0 - Vector3.one; // Fractional part - 1.0
            Vector4 ix  = new Vector4(Pi0.x, Pi1.x, Pi0.x, Pi1.x);
            Vector4 iy  = new Vector4(Pi0.y, Pi0.y, Pi1.y, Pi1.y);
            Vector4 iz0 = new Vector4(Pi0.z, Pi0.z, Pi0.z, Pi0.z);
            Vector4 iz1 = new Vector4(Pi1.z, Pi1.z, Pi1.z, Pi1.z);

            Vector4 ixy  = Mathx.Permute(Mathx.Permute(ix, PERMUTE, MOD) + iy, PERMUTE, MOD);
            Vector4 ixy0 = Mathx.Permute(ixy + iz0, PERMUTE, MOD);
            Vector4 ixy1 = Mathx.Permute(ixy + iz1, PERMUTE, MOD);

            Vector4 gx0 = ixy0 * (1.0f / 7.0f);
            Vector4 gy0 = Mathx.Sub(Mathx.Frac(Mathx.Mul(Mathx.Floor(gx0), (1.0f / 7.0f))), 0.5f);

            gx0 = Mathx.Frac(gx0);
            Vector4 gz0 = Vector4.one * 0.5f - Mathx.Abs(gx0) - Mathx.Abs(gy0);
            Vector4 sz0 = Mathx.Step(gz0, Vector4.zero);

            gx0 -= Mathx.Mul(sz0, Mathx.Sub(Mathx.Step(Vector4.zero, gx0), 0.5f));
            gy0 -= Mathx.Mul(sz0, Mathx.Sub(Mathx.Step(Vector4.zero, gy0), 0.5f));

            Vector4 gx1 = ixy1 * (1.0f / 7.0f);
            Vector4 gy1 = Mathx.Sub(Mathx.Frac(Mathx.Floor(gx1) * (1.0f / 7.0f)), 0.5f);

            gx1 = Mathx.Frac(gx1);
            Vector4 gz1 = Vector4.one * 0.5f - Mathx.Abs(gx1) - Mathx.Abs(gy1);
            Vector4 sz1 = Mathx.Step(gz1, Vector4.zero);

            gx1 -= Mathx.Mul(sz1, Mathx.Sub(Mathx.Step(Vector4.zero, gx1), 0.5f));
            gy1 -= Mathx.Mul(sz1, Mathx.Sub(Mathx.Step(Vector4.zero, gy1), 0.5f));

            Vector3 g000 = new Vector3(gx0.x, gy0.x, gz0.x);
            Vector3 g100 = new Vector3(gx0.y, gy0.y, gz0.y);
            Vector3 g010 = new Vector3(gx0.z, gy0.z, gz0.z);
            Vector3 g110 = new Vector3(gx0.w, gy0.w, gz0.w);
            Vector3 g001 = new Vector3(gx1.x, gy1.x, gz1.x);
            Vector3 g101 = new Vector3(gx1.y, gy1.y, gz1.y);
            Vector3 g011 = new Vector3(gx1.z, gy1.z, gz1.z);
            Vector3 g111 = new Vector3(gx1.w, gy1.w, gz1.w);

            Vector4 TaylorInvSqrt(Vector4 v)
            {
                return(Mathx.Sub(1.79284291400159f, Mathx.Mul(0.85373472095314f, v)));
            }

            Vector4 norm0 = TaylorInvSqrt(new Vector4(
                                              Vector3.Dot(g000, g000),
                                              Vector3.Dot(g010, g010),
                                              Vector3.Dot(g100, g100),
                                              Vector3.Dot(g110, g110)
                                              ));

            g000 *= norm0.x;
            g010 *= norm0.y;
            g100 *= norm0.z;
            g110 *= norm0.w;

            Vector4 norm1 = TaylorInvSqrt(new Vector4(
                                              Vector3.Dot(g001, g001),
                                              Vector3.Dot(g011, g011),
                                              Vector3.Dot(g101, g101),
                                              Vector3.Dot(g111, g111)
                                              ));

            g001 *= norm1.x;
            g011 *= norm1.y;
            g101 *= norm1.z;
            g111 *= norm1.w;

            float n000 = Vector3.Dot(g000, Pf0);
            float n100 = Vector3.Dot(g100, new Vector3(Pf1.x, Pf0.y, Pf0.z));
            float n010 = Vector3.Dot(g010, new Vector3(Pf0.x, Pf1.y, Pf0.z));
            float n110 = Vector3.Dot(g110, new Vector3(Pf1.x, Pf1.y, Pf0.z));
            float n001 = Vector3.Dot(g001, new Vector3(Pf0.x, Pf0.y, Pf1.z));
            float n101 = Vector3.Dot(g101, new Vector3(Pf1.x, Pf0.y, Pf1.z));
            float n011 = Vector3.Dot(g011, new Vector3(Pf0.x, Pf1.y, Pf1.z));
            float n111 = Vector3.Dot(g111, Pf1);

            Vector3 fade_xyz = Mathx.SmootherStep(Pf0);
            Vector4 n_z      = Vector4.Lerp(new Vector4(n000, n100, n010, n110), new Vector4(n001, n101, n011, n111), fade_xyz.z);
            Vector2 n_yz     = Vector2.Lerp(new Vector2(n_z.x, n_z.y), new Vector2(n_z.z, n_z.w), fade_xyz.y);
            float   n_xyz    = Mathf.Lerp(n_yz.x, n_yz.y, fade_xyz.x);

            return(2.2f * n_xyz);
        }
Beispiel #24
0
 public static Vector2 Centroid(params Vector2[] vs)
 {
     return(Mathx.Average(vs));
 }
Beispiel #25
0
 public void Include(Vector3Int otherMin, Vector3Int otherMax)
 {
     min = Mathx.Min(min, otherMin);
     max = Mathx.Max(max, otherMax);
 }
Beispiel #26
0
 public bool IsValid()
 {
     return(!Mathx.IsZero(x) || !Mathx.IsZero(y));
 }
 public bool IsValid()
 {
     return(!Mathx.IsZero(a) && !Mathx.IsZero(b));
 }
Beispiel #28
0
 public static Vector3 ToNormal(Color c)
 {
     return((Mathx.Sub(Mathx.Mul(ToVector3(c), 2), 1)).XZY());
 }