Exemple #1
0
        /// <summary>
        /// Computes an intersection of the line and the sphere
        /// </summary>
        public static bool IntersectLineSphere(Vector3 origin, Vector3 direction, Vector3 center, float radius,
                                               out Vector3 pointA, out Vector3 pointB)
        {
            Vector3 toCenter          = center - origin;
            float   toCenterOnLine    = Vector3.Dot(toCenter, direction);
            float   sqrDistanceToLine = toCenter.sqrMagnitude - toCenterOnLine * toCenterOnLine;

            float sqrRadius = radius * radius;

            if (sqrDistanceToLine > sqrRadius)
            {
                pointA = Vector3.zero;
                pointB = Vector3.zero;
                return(false);
            }
            float fromClosestPointToIntersection = Mathf.Sqrt(sqrRadius - sqrDistanceToLine);
            float intersectionA = toCenterOnLine - fromClosestPointToIntersection;
            float intersectionB = toCenterOnLine + fromClosestPointToIntersection;

            if (intersectionA > intersectionB)
            {
                PTUtils.Swap(ref intersectionA, ref intersectionB);
            }

            pointA = origin + intersectionA * direction;
            pointB = origin + intersectionB * direction;
            return(true);
        }
Exemple #2
0
        public void Simulate()
        {
            PTUtils.Swap(ref _cells, ref copy);
            for (int x = 0; x < config.width; x++)
            {
                for (int y = 0; y < config.height; y++)
                {
                    int aliveCells = CountAliveNeighbourCells(x, y);

                    if (!copy[x, y])
                    {
                        if (config.ruleset.CanSpawn(aliveCells))
                        {
                            cells[x, y] = true;
                        }
                        else
                        {
                            cells[x, y] = false;
                        }
                    }
                    else
                    {
                        if (!config.ruleset.CanSurvive(aliveCells))
                        {
                            cells[x, y] = false;
                        }
                        else
                        {
                            cells[x, y] = true;
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Draws aliased line and calls <paramref name="draw"/> on every pixel
        /// </summary>
        /// <remarks>
        /// https://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
        /// </remarks>
        public static void RasterLine(int x0, int y0, int x1, int y1, Action <int, int> draw)
        {
            bool steep = Math.Abs(y1 - y0) > Math.Abs(x1 - x0);

            if (steep)
            {
                PTUtils.Swap(ref x0, ref y0);
                PTUtils.Swap(ref x1, ref y1);
            }
            if (x0 > x1)
            {
                PTUtils.Swap(ref x0, ref x1);
                PTUtils.Swap(ref y0, ref y1);
            }

            int dx    = x1 - x0;
            int dy    = Math.Abs(y1 - y0);
            int error = dx / 2;
            int ystep = (y0 < y1) ? 1 : -1;
            int y     = y0;

            for (int x = x0; x <= x1; x++)
            {
                draw(steep ? y : x, steep ? x : y);
                error -= dy;
                if (error < 0)
                {
                    y     += ystep;
                    error += dx;
                }
            }
        }
Exemple #4
0
 /// <summary>
 /// Reverses winding order of mesh triangles
 /// </summary>
 public static void FlipTriangles(this Mesh mesh)
 {
     for (int i = 0; i < mesh.subMeshCount; i++)
     {
         var triangles = mesh.GetTriangles(i);
         for (int j = 0; j < triangles.Length; j += 3)
         {
             PTUtils.Swap(ref triangles[j], ref triangles[j + 1]);
         }
         mesh.SetTriangles(triangles, i);
     }
 }
 /// <summary>
 /// Reverses winding order of mesh triangles
 /// </summary>
 public static void FlipTriangles(this Mesh mesh)
 {
     if (mesh == null)
     {
         throw new ArgumentNullException("mesh");
     }
     for (int i = 0; i < mesh.subMeshCount; i++)
     {
         var triangles = mesh.GetTriangles(i);
         for (int j = 0; j < triangles.Length; j += 3)
         {
             PTUtils.Swap(ref triangles[j], ref triangles[j + 1]);
         }
         mesh.SetTriangles(triangles, i);
     }
 }
        /// <summary>
        /// Draws anti-aliased line and calls <paramref name="draw"/> on every pixel
        /// </summary>
        /// <remarks>
        /// https://en.wikipedia.org/wiki/Xiaolin_Wu%27s_line_algorithm
        /// </remarks>
        public static void RasterAALine(int x0, int y0, int x1, int y1, Action <int, int, float> draw)
        {
            bool steep = Math.Abs(y1 - y0) > Math.Abs(x1 - x0);

            if (steep)
            {
                PTUtils.Swap(ref x0, ref y0);
                PTUtils.Swap(ref x1, ref y1);
            }
            if (x0 > x1)
            {
                PTUtils.Swap(ref x0, ref x1);
                PTUtils.Swap(ref y0, ref y1);
            }

            if (steep)
            {
                draw(y0, x0, 1);
                draw(y1, x1, 1);
            }
            else
            {
                draw(x0, y0, 1);
                draw(x1, y1, 1);
            }
            float dx       = x1 - x0;
            float dy       = y1 - y0;
            float gradient = dy / dx;
            float y        = y0 + gradient;

            for (var x = x0 + 1; x <= x1 - 1; x++)
            {
                if (steep)
                {
                    draw((int)y, x, 1 - (y - (int)y));
                    draw((int)y + 1, x, y - (int)y);
                }
                else
                {
                    draw(x, (int)y, 1 - (y - (int)y));
                    draw(x, (int)y + 1, y - (int)y);
                }
                y += gradient;
            }
        }
Exemple #7
0
        /// <summary>
        /// Returns the distance between the closest points on the segments
        /// </summary>
        public static float SegmentSegment(Vector2 segment1A, Vector2 segment1B, Vector2 segment2A, Vector2 segment2B)
        {
            Vector2 from2ATo1A     = segment1A - segment2A;
            Vector2 direction1     = segment1B - segment1A;
            Vector2 direction2     = segment2B - segment2A;
            float   segment1Length = direction1.magnitude;
            float   segment2Length = direction2.magnitude;

            bool segment1IsAPoint = segment1Length < Geometry.Epsilon;
            bool segment2IsAPoint = segment2Length < Geometry.Epsilon;

            if (segment1IsAPoint && segment2IsAPoint)
            {
                return(Vector2.Distance(segment1A, segment2A));
            }
            if (segment1IsAPoint)
            {
                direction2.Normalize();
                return(PointSegment(segment1A, segment2A, segment2B, direction2, segment2Length));
            }
            if (segment2IsAPoint)
            {
                direction1.Normalize();
                return(PointSegment(segment2A, segment1A, segment1B, direction1, segment1Length));
            }

            direction1.Normalize();
            direction2.Normalize();
            float denominator = VectorE.PerpDot(direction1, direction2);
            float perpDot1    = VectorE.PerpDot(direction1, from2ATo1A);
            float perpDot2    = VectorE.PerpDot(direction2, from2ATo1A);

            if (Mathf.Abs(denominator) < Geometry.Epsilon)
            {
                // Parallel
                if (Mathf.Abs(perpDot1) > Geometry.Epsilon || Mathf.Abs(perpDot2) > Geometry.Epsilon)
                {
                    // Not collinear
                    float segment2AProjection = -Vector2.Dot(direction1, from2ATo1A);
                    if (segment2AProjection > -Geometry.Epsilon &&
                        segment2AProjection < segment1Length + Geometry.Epsilon)
                    {
                        float distanceSqr = from2ATo1A.sqrMagnitude - segment2AProjection * segment2AProjection;
                        // distanceSqr can be negative
                        return(distanceSqr <= 0 ? 0 : Mathf.Sqrt(distanceSqr));
                    }

                    Vector2 from1ATo2B          = segment2B - segment1A;
                    float   segment2BProjection = Vector2.Dot(direction1, from1ATo2B);
                    if (segment2BProjection > -Geometry.Epsilon &&
                        segment2BProjection < segment1Length + Geometry.Epsilon)
                    {
                        float distanceSqr = from1ATo2B.sqrMagnitude - segment2BProjection * segment2BProjection;
                        // distanceSqr can be negative
                        return(distanceSqr <= 0 ? 0 : Mathf.Sqrt(distanceSqr));
                    }

                    if (segment2AProjection < 0 && segment2BProjection < 0)
                    {
                        if (segment2AProjection > segment2BProjection)
                        {
                            return(Vector2.Distance(segment1A, segment2A));
                        }
                        return(Vector2.Distance(segment1A, segment2B));
                    }
                    if (segment2AProjection > 0 && segment2BProjection > 0)
                    {
                        if (segment2AProjection < segment2BProjection)
                        {
                            return(Vector2.Distance(segment1B, segment2A));
                        }
                        return(Vector2.Distance(segment1B, segment2B));
                    }
                    float   segment1AProjection = Vector2.Dot(direction2, from2ATo1A);
                    Vector2 segment2Point       = segment2A + direction2 * segment1AProjection;
                    return(Vector2.Distance(segment1A, segment2Point));
                }
                // Collinear

                bool codirected = Vector2.Dot(direction1, direction2) > 0;
                if (codirected)
                {
                    // Codirected
                    float segment2AProjection = -Vector2.Dot(direction1, from2ATo1A);
                    if (segment2AProjection > -Geometry.Epsilon)
                    {
                        // 1A------1B
                        //     2A------2B
                        return(SegmentSegmentCollinear(segment1A, segment1B, segment2A));
                    }
                    else
                    {
                        //     1A------1B
                        // 2A------2B
                        return(SegmentSegmentCollinear(segment2A, segment2B, segment1A));
                    }
                }
                else
                {
                    // Contradirected
                    float segment2BProjection = Vector2.Dot(direction1, segment2B - segment1A);
                    if (segment2BProjection > -Geometry.Epsilon)
                    {
                        // 1A------1B
                        //     2B------2A
                        return(SegmentSegmentCollinear(segment1A, segment1B, segment2B));
                    }
                    else
                    {
                        //     1A------1B
                        // 2B------2A
                        return(SegmentSegmentCollinear(segment2B, segment2A, segment1A));
                    }
                }
            }

            // Not parallel
            float distance1 = perpDot2 / denominator;
            float distance2 = perpDot1 / denominator;

            if (distance1 < -Geometry.Epsilon || distance1 > segment1Length + Geometry.Epsilon ||
                distance2 < -Geometry.Epsilon || distance2 > segment2Length + Geometry.Epsilon)
            {
                // No intersection
                bool    codirected = Vector2.Dot(direction1, direction2) > 0;
                Vector2 from1ATo2B;
                if (!codirected)
                {
                    PTUtils.Swap(ref segment2A, ref segment2B);
                    direction2 = -direction2;
                    from1ATo2B = -from2ATo1A;
                    from2ATo1A = segment1A - segment2A;
                    distance2  = segment2Length - distance2;
                }
                else
                {
                    from1ATo2B = segment2B - segment1A;
                }
                Vector2 segment1Point;
                Vector2 segment2Point;

                float segment2AProjection = -Vector2.Dot(direction1, from2ATo1A);
                float segment2BProjection = Vector2.Dot(direction1, from1ATo2B);

                bool segment2AIsAfter1A  = segment2AProjection > -Geometry.Epsilon;
                bool segment2BIsBefore1B = segment2BProjection < segment1Length + Geometry.Epsilon;
                bool segment2AOnSegment1 = segment2AIsAfter1A && segment2AProjection < segment1Length + Geometry.Epsilon;
                bool segment2BOnSegment1 = segment2BProjection > -Geometry.Epsilon && segment2BIsBefore1B;
                if (segment2AOnSegment1 && segment2BOnSegment1)
                {
                    if (distance2 < -Geometry.Epsilon)
                    {
                        segment1Point = segment1A + direction1 * segment2AProjection;
                        segment2Point = segment2A;
                    }
                    else
                    {
                        segment1Point = segment1A + direction1 * segment2BProjection;
                        segment2Point = segment2B;
                    }
                }
                else if (!segment2AOnSegment1 && !segment2BOnSegment1)
                {
                    if (!segment2AIsAfter1A && !segment2BIsBefore1B)
                    {
                        segment1Point = distance1 < -Geometry.Epsilon ? segment1A : segment1B;
                    }
                    else
                    {
                        // Not on segment
                        segment1Point = segment2AIsAfter1A ? segment1B : segment1A;
                    }
                    float segment1PointProjection = Vector2.Dot(direction2, segment1Point - segment2A);
                    segment1PointProjection = Mathf.Clamp(segment1PointProjection, 0, segment2Length);
                    segment2Point           = segment2A + direction2 * segment1PointProjection;
                }
                else if (segment2AOnSegment1)
                {
                    if (distance2 < -Geometry.Epsilon)
                    {
                        segment1Point = segment1A + direction1 * segment2AProjection;
                        segment2Point = segment2A;
                    }
                    else
                    {
                        segment1Point = segment1B;
                        float segment1PointProjection = Vector2.Dot(direction2, segment1Point - segment2A);
                        segment1PointProjection = Mathf.Clamp(segment1PointProjection, 0, segment2Length);
                        segment2Point           = segment2A + direction2 * segment1PointProjection;
                    }
                }
                else
                {
                    if (distance2 > segment2Length + Geometry.Epsilon)
                    {
                        segment1Point = segment1A + direction1 * segment2BProjection;
                        segment2Point = segment2B;
                    }
                    else
                    {
                        segment1Point = segment1A;
                        float segment1PointProjection = Vector2.Dot(direction2, segment1Point - segment2A);
                        segment1PointProjection = Mathf.Clamp(segment1PointProjection, 0, segment2Length);
                        segment2Point           = segment2A + direction2 * segment1PointProjection;
                    }
                }
                return(Vector2.Distance(segment1Point, segment2Point));
            }

            // Point intersection
            return(0);
        }
Exemple #8
0
        /// <summary>
        /// Returns the distance between the closest points on the ray and the segment
        /// </summary>
        public static float RaySegment(Vector2 rayOrigin, Vector2 rayDirection, Vector2 segmentA, Vector2 segmentB)
        {
            Vector2 segmentAToOrigin = rayOrigin - segmentA;
            Vector2 segmentDirection = segmentB - segmentA;
            float   denominator      = VectorE.PerpDot(rayDirection, segmentDirection);
            float   perpDotA         = VectorE.PerpDot(rayDirection, segmentAToOrigin);
            // Normalized direction gives more stable results
            float perpDotB = VectorE.PerpDot(segmentDirection.normalized, segmentAToOrigin);

            if (Mathf.Abs(denominator) < Geometry.Epsilon)
            {
                // Parallel
                float   segmentAProjection = -Vector2.Dot(rayDirection, segmentAToOrigin);
                Vector2 originToSegmentB   = segmentB - rayOrigin;
                float   segmentBProjection = Vector2.Dot(rayDirection, originToSegmentB);
                if (Mathf.Abs(perpDotA) > Geometry.Epsilon || Mathf.Abs(perpDotB) > Geometry.Epsilon)
                {
                    // Not collinear
                    if (segmentAProjection > -Geometry.Epsilon)
                    {
                        float distanceSqr = segmentAToOrigin.sqrMagnitude - segmentAProjection * segmentAProjection;
                        // distanceSqr can be negative
                        return(distanceSqr <= 0 ? 0 : Mathf.Sqrt(distanceSqr));
                    }
                    if (segmentBProjection > -Geometry.Epsilon)
                    {
                        float distanceSqr = originToSegmentB.sqrMagnitude - segmentBProjection * segmentBProjection;
                        // distanceSqr can be negative
                        return(distanceSqr <= 0 ? 0 : Mathf.Sqrt(distanceSqr));
                    }

                    if (segmentAProjection > segmentBProjection)
                    {
                        return(Vector2.Distance(rayOrigin, segmentA));
                    }
                    return(Vector2.Distance(rayOrigin, segmentB));
                }
                // Collinear
                if (segmentAProjection > -Geometry.Epsilon || segmentBProjection > -Geometry.Epsilon)
                {
                    // Point or segment intersection
                    return(0);
                }
                // No intersection
                return(segmentAProjection > segmentBProjection ? -segmentAProjection : -segmentBProjection);
            }

            // Not parallel
            float rayDistance     = perpDotB / denominator;
            float segmentDistance = perpDotA / denominator;

            if (rayDistance < -Geometry.Epsilon ||
                segmentDistance < -Geometry.Epsilon || segmentDistance > 1 + Geometry.Epsilon)
            {
                // No intersection
                bool    codirected = Vector2.Dot(rayDirection, segmentDirection) > 0;
                Vector2 segmentBToOrigin;
                if (!codirected)
                {
                    PTUtils.Swap(ref segmentA, ref segmentB);
                    segmentDirection = -segmentDirection;
                    segmentBToOrigin = segmentAToOrigin;
                    segmentAToOrigin = rayOrigin - segmentA;
                    segmentDistance  = 1 - segmentDistance;
                }
                else
                {
                    segmentBToOrigin = rayOrigin - segmentB;
                }

                float segmentAProjection = -Vector2.Dot(rayDirection, segmentAToOrigin);
                float segmentBProjection = -Vector2.Dot(rayDirection, segmentBToOrigin);
                bool  segmentAOnRay      = segmentAProjection > -Geometry.Epsilon;
                bool  segmentBOnRay      = segmentBProjection > -Geometry.Epsilon;
                if (segmentAOnRay && segmentBOnRay)
                {
                    if (segmentDistance < 0)
                    {
                        Vector2 rayPoint     = rayOrigin + rayDirection * segmentAProjection;
                        Vector2 segmentPoint = segmentA;
                        return(Vector2.Distance(rayPoint, segmentPoint));
                    }
                    else
                    {
                        Vector2 rayPoint     = rayOrigin + rayDirection * segmentBProjection;
                        Vector2 segmentPoint = segmentB;
                        return(Vector2.Distance(rayPoint, segmentPoint));
                    }
                }
                else if (!segmentAOnRay && segmentBOnRay)
                {
                    if (segmentDistance < 0)
                    {
                        Vector2 rayPoint     = rayOrigin;
                        Vector2 segmentPoint = segmentA;
                        return(Vector2.Distance(rayPoint, segmentPoint));
                    }
                    else if (segmentDistance > 1 + Geometry.Epsilon)
                    {
                        Vector2 rayPoint     = rayOrigin + rayDirection * segmentBProjection;
                        Vector2 segmentPoint = segmentB;
                        return(Vector2.Distance(rayPoint, segmentPoint));
                    }
                    else
                    {
                        Vector2 rayPoint         = rayOrigin;
                        float   originProjection = Vector2.Dot(segmentDirection, segmentAToOrigin);
                        Vector2 segmentPoint     = segmentA + segmentDirection * originProjection / segmentDirection.sqrMagnitude;
                        return(Vector2.Distance(rayPoint, segmentPoint));
                    }
                }
                else
                {
                    // Not on ray
                    Vector2 rayPoint         = rayOrigin;
                    float   originProjection = Vector2.Dot(segmentDirection, segmentAToOrigin);
                    float   sqrSegmentLength = segmentDirection.sqrMagnitude;
                    if (originProjection < 0)
                    {
                        return(Vector2.Distance(rayPoint, segmentA));
                    }
                    else if (originProjection > sqrSegmentLength)
                    {
                        return(Vector2.Distance(rayPoint, segmentB));
                    }
                    else
                    {
                        Vector2 segmentPoint = segmentA + segmentDirection * originProjection / sqrSegmentLength;
                        return(Vector2.Distance(rayPoint, segmentPoint));
                    }
                }
            }
            // Point intersection
            return(0);
        }