/// <summary> /// Create a 2-dimensional vector of type T. /// </summary> /// <param name="X1">The first x-coordinate of the vector.</param> /// <param name="Y1">The first y-coordinate of the vector.</param> /// <param name="X2">The second x-coordinate of the vector.</param> /// <param name="Y2">The second y-coordinate of the vector.</param> public Vector2D(T X1, T Y1, T X2, T Y2) { #region Initial Checks if (X1 == null) { throw new ArgumentNullException("The given left-coordinate must not be null!"); } if (Y1 == null) { throw new ArgumentNullException("The given top-coordinate must not be null!"); } if (X2 == null) { throw new ArgumentNullException("The given right-coordinate must not be null!"); } if (Y2 == null) { throw new ArgumentNullException("The given bottom-coordinate must not be null!"); } #endregion this.Math = MathsFactory <T> .Instance; this.X = Math.Sub(X1, X2); this.Y = Math.Sub(Y1, Y2); this.Length = new Pixel <T>(X1, Y1).DistanceTo(X2, Y2); }
/// <summary> /// Create a 2-dimensional vector of type T. /// </summary> /// <param name="Vector1">A vector of type T.</param> /// <param name="Vector2">A vector of type T.</param> public Vector2D(IVector2D <T> Vector1, IVector2D <T> Vector2) { #region Initial Checks if (Vector1 == null) { throw new ArgumentNullException("The first vector must not be null!"); } if (Vector2 == null) { throw new ArgumentNullException("The second vector must not be null!"); } #endregion this.Math = MathsFactory <T> .Instance; this.X = Math.Sub(Vector1.X, Vector2.X); this.Y = Math.Sub(Vector1.Y, Vector2.Y); this.Length = Vector1.DistanceTo(Vector2); }
/// <summary> /// Create a 2-dimensional vector of type T. /// </summary> /// <param name="Pixel1">A pixel of type T.</param> /// <param name="Pixel2">A pixel of type T.</param> public Vector2D(IPixel <T> Pixel1, IPixel <T> Pixel2) { #region Initial Checks if (Pixel1 == null) { throw new ArgumentNullException("The first pixel must not be null!"); } if (Pixel2 == null) { throw new ArgumentNullException("The second pixel must not be null!"); } #endregion this.Math = MathsFactory <T> .Instance; this.X = Math.Sub(Pixel1.X, Pixel2.X); this.Y = Math.Sub(Pixel1.Y, Pixel2.Y); this.Length = Pixel1.DistanceTo(Pixel2); }
/// <summary> /// Checks if the given circle is located /// within this circle. /// </summary> /// <param name="Circle">A circle of type T.</param> /// <returns>True if the circle is located within this circle; False otherwise.</returns> public Boolean Contains(ICircle <T> Circle) { #region Initial Checks if (Circle == null) { throw new ArgumentNullException("The given circle must not be null!"); } #endregion if (Center.DistanceTo(Circle.Center).IsLessThanOrEquals(Math.Sub(Radius, Circle.Radius))) { return(true); } return(true); }
/// <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)); } }
/// <summary> /// A method to sub two vectors. /// </summary> /// <param name="v1">A vector.</param> /// <param name="v2">A vector.</param> /// <returns>The subtraction of v2 from v1: v1 - v2</returns> public IVector2D <T> Sub(IVector2D <T> v1, IVector2D <T> v2) { return(new Vector2D <T>(Math.Sub(v1.X, v2.X), Math.Sub(v1.Y, v2.Y))); }