Tests if two line segments intersect or not. : Geometry « Development Class « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# / C Sharp » Development Class » Geometry 




Tests if two line segments intersect or not.
         

using System;

namespace ComputationalGeometry.Shapes
{
    public static class MathUtility
    {


        /// <summary>
        /// Tests if two line segments intersect or not.
        /// The orientation of each line to other line's endpoints is used to determine
        /// the intersection.
        /// </summary>
        /// <param name="line1">Line 1.</param>
        /// <param name="line2">Line 2.</param>
        /// <returns>True if the lines intersect each other.</returns>
        public static bool DoLinesIntersect(Line2D line1, Line2D line2)
        {
            return CrossProduct(line1.InitialPoint, line1.TerminalPoint, line2.InitialPoint!=
                   CrossProduct(line1.InitialPoint, line1.TerminalPoint, line2.TerminalPoint||
                   CrossProduct(line2.InitialPoint, line2.TerminalPoint, line1.InitialPoint!=
                   CrossProduct(line2.InitialPoint, line2.TerminalPoint, line1.TerminalPoint);
        }
        /// <summary>
        /// Finds the cross product of the 2 vectors created by the 3 vertices.
        /// Vector 1 = v1 -> v2, Vector 2 = v2 -> v3
        /// The vectors make a "right turn" if the sign of the cross product is negative.
        /// The vectors make a "left turn" if the sign of the cross product is positive.
        /// The vectors are colinear (on the same line) if the cross product is zero.
        /// </summary>
        /// <param name="p1">First point.</param>
        /// <param name="p2">Second point.</param>
        /// <param name="p3">Third point.</param>
        /// <returns>Cross product of the two vectors.</returns>
        public static double CrossProduct(Point2D p1, Point2D p2, Point2D p3)
        {
            return (p2.X - p1.X(p3.Y - p1.Y(p3.X - p1.X(p2.Y - p1.Y);
        }

    }
}

   
    
    
    
    
    
    
    
    
  














Related examples in the same category
1.Convert Meters To Inches
2.Convert Meters To Miles
3.Get Steps FromD istance And Stride
4.Convert Miles To Meters
5.Distance Util
6.PointD
7.Calculate Gradient Angle
8.Convert Meters To Feet
9.Get distance between two points
10.Sorts a graph by the dependencies.
11.Degrees To Radians
12.Radians To Degrees
13.Angles Difference
14.Degrees To Radians and Radians To Degrees
15.Get Distance From Steps
16.Meter to feet and feet to Mile
17.Distance From Point To Line
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.