Example #1
0
        /// <summary>
        /// Gets the intersection between this line segment and the given line segment, null if the don't have one.
        /// </summary>
        /// <param name="line2">The other line segment.</param>
        /// <returns>The intersection between this line segment and the given line segment, null if the don't have one.</returns>
        public Point2D GetIntersectionWith(LineSegment2D line2)
        {
            // Try to get the intersection between the line containing this line segment and the line containing the given line segment.
            Point2D intersection = base.GetIntersectionWith(line2);

            // Return null if they don't have a intersection.
            if (intersection == null)
            {
                return(null);
            }

            // Return the intersection if its x and y are in the range, otherwise, null.
            if ((this.minX <= intersection.X) && (intersection.X <= this.maxX) &&
                (this.minY <= intersection.Y) && (intersection.Y <= this.maxY) &&
                (line2.minX <= intersection.X) && (intersection.X <= line2.maxX) &&
                (line2.minY <= intersection.Y) && (intersection.Y <= line2.maxY))
            {
                return(intersection);
            }
            else
            {
                return(null);
            }
        }
Example #2
0
        /// <summary>
        /// Returns true if this triangle collides with (contains) the given point, otherwise, false.
        /// </summary>
        /// <param name="x">X-coordinate of the given point.</param>
        /// <param name="y">Y-coordinate of the given point.</param>
        /// <returns>True if this triangle collides with (contains) the given point, otherwise, false.</returns>
        public bool Collide(double x, double y)
        {
            // Get 3 line segments of this triangle.
            LineSegment2D lineSegment1 = new LineSegment2D(this.Vertex1, this.Vertex2);
            LineSegment2D lineSegment2 = new LineSegment2D(this.Vertex1, this.Vertex3);
            LineSegment2D lineSegment3 = new LineSegment2D(this.Vertex2, this.Vertex3);

            // Return true if there is a line segment of this triangle that contains the given point.
            if ((lineSegment1.Contains(x, y)) ||
                (lineSegment2.Contains(x, y)) ||
                (lineSegment3.Contains(x, y)))
            {
                return(true);
            }

            // Get the horizontal line accross the given point.
            Line2D horizontalLine = new Line2D(0, 1, -y);

            // Switch references of Line objects to make sure that if there is a horizontal line that contains some segment of this triangle, then it must be line1.
            if (lineSegment2.A == 0)
            {
                SwitchLine(lineSegment1, lineSegment2);
            }
            else if (lineSegment3.A == 0)
            {
                SwitchLine(lineSegment1, lineSegment3);
            }

            if (lineSegment1.A == 0)
            {
                // If there is a line segment of this triangle is horizontal, then the horizontal line accross the given point will have at most 2 intersections with this triangle.

                Point2D intersection1 = lineSegment2.GetIntersectionWith(horizontalLine);
                Point2D intersection2 = lineSegment3.GetIntersectionWith(horizontalLine);
                return(Collide(intersection1, intersection2, x));
            }
            else
            {
                // If there is no line segment of this triangle is horizontal, then the horizontal line accross the given point will have at most 3 intersections with this triangle.

                Point2D intersection1 = lineSegment1.GetIntersectionWith(horizontalLine);
                Point2D intersection2 = lineSegment2.GetIntersectionWith(horizontalLine);
                Point2D intersection3 = lineSegment3.GetIntersectionWith(horizontalLine);

                // If there is 1 of the intersections is null, then just check the other 2.
                // Otherwise, just check 2 different intersections.

                if (intersection1 == null)
                {
                    return(Collide(intersection2, intersection3, x));
                }
                else if (intersection2 == null)
                {
                    return(Collide(intersection1, intersection3, x));
                }
                else if (intersection3 == null)
                {
                    return(Collide(intersection1, intersection2, x));
                }
                else
                {
                    // If program reaches here, it means that the horizontal line accross the given point have 3 intersections with this triangle.
                    // In other words, 2 of them are the same point.
                    // So, you need to find out the 3rd point.
                    if (intersection1.Equals(intersection2))
                    {
                        return(Collide(intersection1, intersection3, x));
                    }
                    else if (intersection1.Equals(intersection3))
                    {
                        return(Collide(intersection1, intersection2, x));
                    }
                    else
                    {
                        return(Collide(intersection1, intersection3, x));
                    }

                    // Or you can just merge the last 2 branches.
                }
            }

            // Note that program will never reach here.
        }