Ejemplo n.º 1
0
        public void GridLineIntersects()
        {
            //
            // TODO: Add test logic	here
            //

            GridLine lineA = new GridLine(new GridVector2(-5, 0),
                                                        new GridVector2(-10, 0));
            GridLine lineB = new GridLine(new GridVector2(0, 5),
                                                        new GridVector2(0, -5));

            GridVector2 intersect = new GridVector2();
            bool result = lineA.Intersects(lineB, out intersect);
            Debug.Assert(result == true);
            Debug.Assert(intersect.X == 0 && intersect.Y == 0);
        }
Ejemplo n.º 2
0
        public bool Intersects(GridLine seg, out GridVector2 Intersection)
        {
            //Function for each line
            //Ax + By = C
            Intersection = new GridVector2();

            if (seg == null)
                throw new ArgumentNullException("seg");

            if (this.Direction == seg.Direction)
                return false;

            GridVector2 A = Origin;
            GridVector2 B = Origin + Direction;

            GridVector2 segA = seg.Origin;
            GridVector2 segB = seg.Origin + seg.Direction;

            double A1 = B.Y - A.Y;
            double A2 = segB.Y - segA.Y;

            double B1 = A.X - B.X;
            double B2 = segA.X - segB.X;

            double C1 = A1 * A.X + B1 * A.Y;
            double C2 = A2 * segA.X + B2 * segA.Y;

            double det = A1 * B2 - A2 * B1;
            //Check if lines are parallel
            if (det == 0)
            {

                return false;
            }
            else
            {
                double x = (B2 * C1 - B1 * C2) / det;
                double y = (A1 * C2 - A2 * C1) / det;

                Intersection = new GridVector2(x, y);
                return true;
            }
        }