Пример #1
0
            public Vector2 IntersectionWithOtherLine(InfiniteLine other)
            {
                Vector2 result = new Vector2();

                result.x = (other.b - b) / (a - other.a);
                result.y = a * result.x + b;
                return(result);
            }
Пример #2
0
        public static CutResult CutShapeIntoTwo(Vector2 lineStart, Vector2 lineEnd, Vector2[] shape)
        {
            List <Vector2> firstSide  = new List <Vector2>();
            List <Vector2> secondSide = new List <Vector2>();

            InfiniteLine cuttingLine = new InfiniteLine(lineStart, lineEnd);

            int intersectionsFound = 0;

            for (int i = 0; i < shape.Length; i++)
            {
                Vector2 point = shape[i];

                Vector2 previousPoint;
                if (i == 0)
                {
                    previousPoint = shape[shape.Length - 1];
                }
                else
                {
                    previousPoint = shape[i - 1];
                }

                if (cuttingLine.IntersectsWithSegment(previousPoint, point))
                {
                    InfiniteLine lastTwoPointsLine = new InfiniteLine(previousPoint, point);
                    Vector2      intersectionPoint = cuttingLine.IntersectionWithOtherLine(lastTwoPointsLine);
                    firstSide.Add(intersectionPoint);
                    secondSide.Add(intersectionPoint);
                    intersectionsFound++;
                }

                if (cuttingLine.PointBelowLine(point))
                {
                    firstSide.Add(point);
                }
                else
                {
                    secondSide.Add(point);
                }
            }

            if (intersectionsFound > 2)
            {
                //throw new System.Exception( "SpriteCutter cannot cut through non-convex shapes! Adjust your colliders shapes to be convex!" );
            }

            CutResult result = new CutResult();

            result.firstSidePoints  = firstSide.ToArray();
            result.secondSidePoints = secondSide.ToArray();
            return(result);
        }
Пример #3
0
        public static CutResult CutShapeIntoTwo( Vector2 lineStart, Vector2 lineEnd, Vector2[] shape )
        {
            List<Vector2> firstSide = new List<Vector2>();
            List<Vector2> secondSide = new List<Vector2>();

            InfiniteLine cuttingLine = new InfiniteLine( lineStart, lineEnd );

            int intersectionsFound = 0;

            for ( int i = 0; i < shape.Length; i++ ) {
                Vector2 point = shape[ i ];

                Vector2 previousPoint;
                if ( i == 0 ) {
                    previousPoint = shape[ shape.Length - 1 ];
                } else {
                    previousPoint = shape[ i - 1 ];
                }

                if ( cuttingLine.IntersectsWithSegment( previousPoint, point ) ) {
                    InfiniteLine lastTwoPointsLine = new InfiniteLine( previousPoint, point );
                    Vector2 intersectionPoint = cuttingLine.IntersectionWithOtherLine( lastTwoPointsLine );
                    firstSide.Add( intersectionPoint );
                    secondSide.Add( intersectionPoint );
                    intersectionsFound++;
                }

                if ( cuttingLine.PointBelowLine( point ) ) {
                    firstSide.Add( point );
                } else {
                    secondSide.Add( point );
                }
            }

            if ( intersectionsFound > 2 ) {
                throw new System.Exception( "SpriteCutter cannot cut through non-convex shapes! Adjust your colliders shapes to be convex!" );
            }

            CutResult result = new CutResult();
            result.firstSidePoints = firstSide.ToArray();
            result.secondSidePoints = secondSide.ToArray();
            return result;
        }
 private void OnEnable()
 {
     script = (InfiniteLine)target;
 }
Пример #5
0
 public Vector2 IntersectionWithOtherLine( InfiniteLine other )
 {
     Vector2 result = new Vector2();
     result.x = ( other.b - b ) / ( a - other.a );
     result.y = a * result.x + b;
     return result;
 }