Ejemplo n.º 1
0
        public static long Pack(int xBits, int yBits, Vector2l vector)
        {
            Contract.Requires(0 <= xBits && xBits <= 64, "xBits must be between 0 and 64 inclusive.");
            Contract.Requires(0 <= yBits && yBits <= 64, "yBits must be between 0 and 64 inclusive.");
            Contract.Requires(xBits + yBits <= 64);
            ulong x = (ulong)(vector.X) >> (64 - xBits);
            ulong y = (ulong)(vector.Y) >> (64 - yBits);

            y <<= xBits;
            return((long)(x | y));
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="Vector8l"/> using the specified vector and values.
 /// </summary>
 /// <param name="value">A vector containing the values with which to initialize the first 2 components</param>
 /// <param name="v2">Value for the V2 component of the vector.</param>
 /// <param name="v3">Value for the V3 component of the vector.</param>
 /// <param name="v4">Value for the V4 component of the vector.</param>
 /// <param name="v5">Value for the V5 component of the vector.</param>
 /// <param name="v6">Value for the V6 component of the vector.</param>
 /// <param name="v7">Value for the V7 component of the vector.</param>
 public Vector8l(Vector2l value, long v2, long v3, long v4, long v5, long v6, long v7, long v8, long v9)
 {
     V0 = value.X;
     V1 = value.Y;
     V2 = v2;
     V3 = v3;
     V4 = v4;
     V5 = v5;
     V6 = v6;
     V7 = v7;
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Transforms a vector in cartesian coordinates to polar coordinates.
        /// </summary>
        /// <param name="value">The vector to transform.</param>
        /// <returns>The polar coordinates of value.</returns>
        public static PolarCoordinate CartesianToPolar(Vector2l value)
        {
            double theta = Functions.Atan2(value.Y, value.X);

            if (theta < 0)
            {
                theta += 2 * Constants.Pi;
            }
            return(new PolarCoordinate(
                       theta,
                       (double)Functions.Sqrt(value.X * value.X + value.Y * value.Y)));
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Computes the normalized value (or unit) of a vector.
        /// </summary>
        /// <param name="value">A vector.</param>
        /// <returns>The normalized value of value.</returns>
        public static Vector2d Normalize(Vector2l value)
        {
            var absolute = Absolute(value);

            if (absolute <= double.Epsilon)
            {
                return(Vector2l.Zero);
            }
            else
            {
                return((Vector2d)value / absolute);
            }
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Determines whether all components of a vector are non-zero.
 /// </summary>
 /// <param name="value">A vector.</param>
 /// <returns>true if all components are non-zero; false otherwise.</returns>
 public static bool All(Vector2l value)
 {
     return(value.X != 0 && value.Y != 0);
 }
Ejemplo n.º 6
0
 /// <summary>
 /// Determines whether any component of a vector is non-zero.
 /// </summary>
 /// <param name="value">A vector.</param>
 /// <returns>true if any components are non-zero; false otherwise.</returns>
 public static bool Any(Vector2l value)
 {
     return(value.X != 0 || value.Y != 0);
 }
Ejemplo n.º 7
0
 /// <summary>
 /// Returns a value that indicates whether two vectors are equal.
 /// </summary>
 /// <param name="left">The first vector to compare.</param>
 /// <param name="right">The second vector to compare.</param>
 /// <returns>true if the left and right are equal; otherwise, false.</returns>
 public static bool Equals(Vector2l left, Vector2l right)
 {
     return(left == right);
 }
Ejemplo n.º 8
0
 /// <summary>
 /// Calculates the dot product (inner product) of two vectors.
 /// </summary>
 /// <param name="left">First source vector.</param>
 /// <param name="right">Second source vector.</param>
 /// <returns>The dot product of the two vectors.</returns>
 public static long Dot(Vector2l left, Vector2l right)
 {
     return(left.X * right.X + left.Y * right.Y);
 }
Ejemplo n.º 9
0
 /// <summary>
 /// Returns a vector that contains the highest value from each pair of components.
 /// </summary>
 /// <param name="value1">The first vector.</param>
 /// <param name="value2">The second vector.</param>
 /// <returns>The highest of each component in left and the matching component in right.</returns>
 public static Vector2l Max(Vector2l value1, Vector2l value2)
 {
     return(new Vector2l(Functions.Max(value1.X, value2.X), Functions.Max(value1.Y, value2.Y)));
 }
Ejemplo n.º 10
0
 /// <summary>
 /// Maps the components of a vector and returns the result.
 /// </summary>
 /// <param name="value">The vector to map.</param>
 /// <param name="mapping">A mapping function to apply to each component.</param>
 /// <returns>The result of mapping each component of value.</returns>
 public static Vector2s Map(Vector2l value, Func <long, short> mapping)
 {
     return(new Vector2s(mapping(value.X), mapping(value.Y)));
 }
Ejemplo n.º 11
0
 /// <summary>
 /// Maps the components of a vector and returns the result.
 /// </summary>
 /// <param name="value">The vector to map.</param>
 /// <param name="mapping">A mapping function to apply to each component.</param>
 /// <returns>The result of mapping each component of value.</returns>
 public static Vector2d Map(Vector2l value, Func <long, double> mapping)
 {
     return(new Vector2d(mapping(value.X), mapping(value.Y)));
 }
Ejemplo n.º 12
0
 /// <summary>
 /// Computes the absolute value (or modulus or magnitude) of a vector and returns the result.
 /// </summary>
 /// <param name="value">A vector.</param>
 /// <returns>The absolute value of value.</returns>
 public static double Absolute(Vector2l value)
 {
     return(Functions.Sqrt(AbsoluteSquared(value)));
 }
Ejemplo n.º 13
0
 /// <summary>
 /// Maps the components of a vector and returns the result.
 /// </summary>
 /// <param name="value">The vector to map.</param>
 /// <param name="mapping">A mapping function to apply to each component.</param>
 /// <returns>The result of mapping each component of value.</returns>
 public static Vector2sb Map(Vector2l value, Func <long, sbyte> mapping)
 {
     return(new Vector2sb(mapping(value.X), mapping(value.Y)));
 }
Ejemplo n.º 14
0
 /// <summary>
 /// Returns the additive inverse of a vector.
 /// </summary>
 /// <param name="value">A vector.</param>
 /// <returns>The negative of value.</returns>
 public static Vector2l Negative(Vector2l value)
 {
     return(new Vector2l(-value.X, -value.Y));
 }
Ejemplo n.º 15
0
 /// <summary>
 /// Writes the given <see cref="Vector2l"/> to an <see cref="Ibasa.IO.BinaryWriter">.
 /// </summary>
 public static void Write(this Ibasa.IO.BinaryWriter writer, Vector2l vector)
 {
     writer.Write(vector.X);
     writer.Write(vector.Y);
 }
Ejemplo n.º 16
0
 /// <summary>
 /// Multiplys the components of two vectors and returns the result.
 /// </summary>
 /// <param name="left">The first vector to modulate.</param>
 /// <param name="right">The second vector to modulate.</param>
 /// <returns>The result of multiplying each component of left by the matching component in right.</returns>
 public static Vector2l Modulate(Vector2l left, Vector2l right)
 {
     return(new Vector2l(left.X * right.X, left.Y * right.Y));
 }
Ejemplo n.º 17
0
 /// <summary>
 /// Constrains each component to a given range.
 /// </summary>
 /// <param name="value">A vector to constrain.</param>
 /// <param name="min">The minimum values for each component.</param>
 /// <param name="max">The maximum values for each component.</param>
 /// <returns>A vector with each component constrained to the given range.</returns>
 public static Vector2l Clamp(Vector2l value, Vector2l min, Vector2l max)
 {
     return(new Vector2l(Functions.Clamp(value.X, min.X, max.X), Functions.Clamp(value.Y, min.Y, max.Y)));
 }
Ejemplo n.º 18
0
 /// <summary>
 /// Determines whether any components of a vector satisfy a condition.
 /// </summary>
 /// <param name="value">A vector.</param>
 /// <param name="predicate">A function to test each component for a condition.</param>
 /// <returns>true if any component of the vector passes the test in the specified
 /// predicate; otherwise, false.</returns>
 public static bool Any(Vector2l value, Predicate <long> predicate)
 {
     return(predicate(value.X) || predicate(value.Y));
 }
Ejemplo n.º 19
0
 /// <summary>
 /// Maps the components of a vector and returns the result.
 /// </summary>
 /// <param name="value">The vector to map.</param>
 /// <param name="mapping">A mapping function to apply to each component.</param>
 /// <returns>The result of mapping each component of value.</returns>
 public static Vector2h Map(Vector2l value, Func <long, Half> mapping)
 {
     return(new Vector2h(mapping(value.X), mapping(value.Y)));
 }
Ejemplo n.º 20
0
 /// <summary>
 /// Computes the absolute squared value of a vector and returns the result.
 /// </summary>
 /// <param name="value">A vector.</param>
 /// <returns>The absolute squared value of value.</returns>
 public static long AbsoluteSquared(Vector2l value)
 {
     return(Dot(value, value));
 }
Ejemplo n.º 21
0
 /// <summary>
 /// Maps the components of a vector and returns the result.
 /// </summary>
 /// <param name="value">The vector to map.</param>
 /// <param name="mapping">A mapping function to apply to each component.</param>
 /// <returns>The result of mapping each component of value.</returns>
 public static Vector2ui Map(Vector2l value, Func <long, uint> mapping)
 {
     return(new Vector2ui(mapping(value.X), mapping(value.Y)));
 }
Ejemplo n.º 22
0
 /// <summary>
 /// Adds two vectors and returns the result.
 /// </summary>
 /// <param name="left">The first value to add.</param>
 /// <param name="right">The second value to add.</param>
 /// <returns>The sum of left and right.</returns>
 public static Vector2l Add(Vector2l left, Vector2l right)
 {
     return(new Vector2l(left.X + right.X, left.Y + right.Y));
 }
Ejemplo n.º 23
0
 /// <summary>
 /// Returns the absolute value (per component).
 /// </summary>
 /// <param name="value">A vector.</param>
 /// <returns>The absolute value (per component) of value.</returns>
 public static Vector2l Abs(Vector2l value)
 {
     return(new Vector2l(Functions.Abs(value.X), Functions.Abs(value.Y)));
 }
Ejemplo n.º 24
0
 /// <summary>
 /// Maps the components of a vector and returns the result.
 /// </summary>
 /// <param name="value">The vector to map.</param>
 /// <param name="mapping">A mapping function to apply to each component.</param>
 /// <returns>The result of mapping each component of value.</returns>
 public static Vector2f Map(Vector2l value, Func <long, float> mapping)
 {
     return(new Vector2f(mapping(value.X), mapping(value.Y)));
 }
Ejemplo n.º 25
0
 /// <summary>
 /// Subtracts one vectors from another and returns the result.
 /// </summary>
 /// <param name="left">The value to subtract from (the minuend).</param>
 /// <param name="right">The value to subtract (the subtrahend).</param>
 /// <returns>The result of subtracting right from left (the difference).</returns>
 public static Vector2l Subtract(Vector2l left, Vector2l right)
 {
     return(new Vector2l(left.X - right.X, left.Y - right.Y));
 }
Ejemplo n.º 26
0
 /// <summary>
 /// Maps the components of a vector and returns the result.
 /// </summary>
 /// <param name="value">The vector to map.</param>
 /// <param name="mapping">A mapping function to apply to each component.</param>
 /// <returns>The result of mapping each component of value.</returns>
 public static Vector2l Map(Vector2l value, Func <long, long> mapping)
 {
     return(new Vector2l(mapping(value.X), mapping(value.Y)));
 }
Ejemplo n.º 27
0
 /// <summary>
 /// Returns the product of a vector and scalar.
 /// </summary>
 /// <param name="vector">The vector to multiply.</param>
 /// <param name="scalar">The scalar to multiply.</param>
 /// <returns>The product of the left and right parameters.</returns>
 public static Vector2l Multiply(Vector2l vector, long scalar)
 {
     return(new Vector2l(vector.X * scalar, vector.Y * scalar));
 }
Ejemplo n.º 28
0
 /// <summary>
 /// Divides a vector by a scalar and returns the result.
 /// </summary>
 /// <param name="vector">The vector to be divided (the dividend).</param>
 /// <param name="scalar">The scalar to divide by (the divisor).</param>
 /// <returns>The result of dividing left by right (the quotient).</returns>
 public static Vector2l Divide(Vector2l vector, long scalar)
 {
     return(new Vector2l(vector.X / scalar, vector.Y / scalar));
 }
Ejemplo n.º 29
0
        public IEnumerable <Tuple <Point2l, Point2l> > Trace(Point2l start, Point2l end)
        {
            var x0 = start.X / GridSize.Width;
            var y0 = start.Y / GridSize.Height;

            var x1 = end.X / GridSize.Width;
            var y1 = end.Y / GridSize.Height;

            var dir = new Vector2l(end.X - start.X, end.Y - start.Y);

            var dx = Math.Sign(dir.X);
            var dy = Math.Sign(dir.Y);

            var deltax = (double)GridSize.Width / Math.Abs(dir.X);
            var deltay = (double)GridSize.Height / Math.Abs(dir.Y);

            var minx = x0 * GridSize.Width;
            var maxx = minx + GridSize.Width;

            var miny = y0 * GridSize.Height;
            var maxy = miny + GridSize.Height;

            var distx = ((dx >= 0) ? (maxx - start.X) : (start.X - minx)) / (double)GridSize.Width;
            var disty = ((dy >= 0) ? (maxy - start.Y) : (start.Y - miny)) / (double)GridSize.Height;

            var tx = distx * deltax;
            var ty = disty * deltay;

            var prev = new Point2l(x0, y0);
            var hit  = start;

            while (true)
            {
                var grid = new Point2l(x0, y0);
                yield return(Tuple.Create(grid, hit));

                prev = grid;

                if (tx <= ty)
                {
                    if (x0 == x1)
                    {
                        break;
                    }

                    hit = new Point2l(
                        start.X + (long)(dir.X * tx),
                        start.Y + (long)(dir.Y * tx));

                    tx += deltax;
                    x0 += dx;
                }
                else
                {
                    if (y0 == y1)
                    {
                        break;
                    }

                    hit = new Point2l(
                        start.X + (long)(dir.X * ty),
                        start.Y + (long)(dir.Y * ty));

                    ty += deltay;
                    y0 += dy;
                }
            }
        }