Exemplo n.º 1
0
        /// <summary>
        /// Checks if and where the given lines intersect.
        /// </summary>
        /// <param name="Line">A line.</param>
        /// <param name="Pixel">The intersection of both lines.</param>
        /// <param name="InfiniteLines">Whether the lines should be treated as infinite or not.</param>
        /// <returns>True if the lines intersect; False otherwise.</returns>
        public Boolean IntersectsWith(ILine2D <T> Line, out IPixel <T> Pixel, Boolean InfiniteLines = false)
        {
            #region Initial Checks

            if (Line == null)
            {
                Pixel = null;
                return(false);
            }

            #endregion

            // Assume both lines are infinite in order to get their intersection...

            #region This line is just a pixel

            if (this.IsJustAPixel())
            {
                var p = new Pixel <T>(this.X1, this.Y1);

                if (Line.Contains(p))
                {
                    Pixel = p;
                    return(true);
                }

                Pixel = null;
                return(false);
            }

            #endregion

            #region The given line is just a pixel

            else if (Line.IsJustAPixel())
            {
                var p = new Pixel <T>(Line.X1, Line.Y1);

                if (this.Contains(p))
                {
                    Pixel = p;
                    return(true);
                }

                Pixel = null;
                return(false);
            }

            #endregion

            #region Both lines are parallel or antiparallel

            else if (this.Normale.IsParallelTo(Line.Normale))
            {
                Pixel = null;
                return(false);
            }

            #endregion

            #region This line is parallel to the y-axis

            else if (this.Normale.Y.Equals(Math.Zero))
            {
                Pixel = new Pixel <T>(this.Pixel1.X,
                                      Math.Add(Math.Mul(Line.Gradient, this.Pixel1.X), Line.YIntercept));
            }

            #endregion

            #region The given line is parallel to the y-axis

            else if (Line.Normale.Y.Equals(Math.Zero))
            {
                Pixel = new Pixel <T>(Line.X1,
                                      Math.Add(Math.Mul(this.Gradient, Line.X1), this.YIntercept));
            }

            #endregion

            #region There is a real intersection

            else
            {
                Pixel = new Pixel <T>(Math.Div(Math.Sub(Line.YIntercept, this.YIntercept),
                                               Math.Sub(this.Gradient, Line.Gradient)),

                                      Math.Div(Math.Sub(Math.Mul(this.YIntercept, Line.Gradient),
                                                        Math.Mul(Line.YIntercept, this.Gradient)),
                                               Math.Sub(Line.Gradient, this.Gradient)));
            }

            #endregion

            if (InfiniteLines)
            {
                return(true);
            }
            else
            {
                return(this.Contains(Pixel));
            }
        }
Exemplo n.º 2
0
 /// <summary>
 /// A method to divide two vectors.
 /// </summary>
 /// <param name="v1">A vector.</param>
 /// <param name="v2">A vector.</param>
 /// <returns>The division of v1 by v2: v1 / v2</returns>
 public IVector2D <T> Div(IVector2D <T> v1, IVector2D <T> v2)
 {
     return(new Vector2D <T>(Math.Div(v1.X, v2.X),
                             Math.Div(v1.Y, v2.Y)));
 }