Dot() public static method

Calculates the dot product of two vectors. If the two vectors are unit vectors, the dot product returns a floating point value between -1 and 1 that can be used to determine some properties of the angle between two vectors. For example, it can show whether the vectors are orthogonal, parallel, or have an acute or obtuse angle between them.
public static Dot ( Vector2 value1, Vector2 value2 ) : float
value1 Vector2 Source vector.
value2 Vector2 Source vector.
return float
Ejemplo n.º 1
0
        public float DistanceToSquared(ref Vector3 vector, out Segment connectingSegment)
        {
            float segmentLength = (float)this.GetLength();

            Vector2 normalizedLine = new Vector2(
                (float)(Point2.X - Point1.X) / segmentLength,
                (float)(Point2.Y - Point1.Y) / segmentLength);

            Vector2 pointVector = new Vector2((float)(vector.X - Point1.X), (float)(vector.Y - Point1.Y));

            float length = Vector2.Dot(pointVector, normalizedLine);

            if (length < 0)
            {
                connectingSegment.Point1 = new Point(ref vector);
                connectingSegment.Point2 = Point1;

                return((float)connectingSegment.GetLengthSquared());
            }
            else if (length > segmentLength)
            {
                connectingSegment.Point1 = new Point(ref vector);
                connectingSegment.Point2 = Point2;

                return((float)connectingSegment.GetLengthSquared());
            }
            else
            {
                connectingSegment.Point1 = new Point(ref vector);
                connectingSegment.Point2 = new Point(Point1.X + length * normalizedLine.X,
                                                     Point1.Y + length * normalizedLine.Y);

                return((float)connectingSegment.GetLengthSquared());
            }
        }
Ejemplo n.º 2
0
        public float DistanceTo(double x, double y)
        {
            float segmentLength = (float)this.GetLength();

            Vector2 normalizedLine = new Vector2(
                (float)(Point2.X - Point1.X) / segmentLength,
                (float)(Point2.Y - Point1.Y) / segmentLength);

            Vector2 pointVector = new Vector2((float)(x - Point1.X), (float)(y - Point1.Y));

            float length = Vector2.Dot(pointVector, normalizedLine);

            if (length < 0)
            {
                return((float)(new Segment(x, y, Point1.X, Point1.Y).GetLength()));
            }
            else if (length > segmentLength)
            {
                return((float)(new Segment(x, y, Point2.X, Point2.Y).GetLength()));
            }
            else
            {
                normalizedLine.X *= length;
                normalizedLine.Y *= length;

                float xDistanceSquared = pointVector.X - normalizedLine.X;
                xDistanceSquared = xDistanceSquared * xDistanceSquared;

                float yDistanceSquared = pointVector.Y - normalizedLine.Y;
                yDistanceSquared = yDistanceSquared * yDistanceSquared;

                return((float)System.Math.Sqrt(xDistanceSquared + yDistanceSquared));
            }
        }
Ejemplo n.º 3
0
        public Point ClosestPointTo(Point point)
        {
            Vector2 unitSegment = new Vector2((float)(this.Point2.X - this.Point1.X), (float)(Point2.Y - Point1.Y));

            unitSegment.Normalize();

            Vector2 pointVector = new Vector2((float)(point.X - this.Point1.X), (float)(point.Y - Point1.Y));
            //			Segment pointSegment = new Segment( new Point(0,0),
            //				new Point(point.x - this.Point1.X, point.y - Point1.Y));

            float l = Vector2.Dot(pointVector, unitSegment);

            if (l < 0)
            {
                return(this.Point1);
            }
            else if (l * l > this.GetLengthSquared())
            {
                return(this.Point2);
            }
            else
            {
                Point newPoint = new Point(Point1.X + l * unitSegment.X, Point1.Y + l * unitSegment.Y);
                return(newPoint);
            }
        }
Ejemplo n.º 4
0
        /// <summarY>
        /// Draws the map
        /// </summarY>
        /// <param name="map">The map to draw</param>
        /// <param name="camera">The position to draw from</param>
        /// <param name="orientation">The rotation to draw from in degrees</param>
        public void RenderMap(SpriteBatch spriteBatch, Map map, Vector2 camera, float orientation, int cellSize,
                              string wallsLayer)
        {
            TmxMap mapData     = map.Data;
            int    slices      = viewport.Width;
            float  halfFov     = FOV / 2;
            float  focalLength = slices / 2 / (float)Math.Tan(halfFov);
            float  cameraAngle = orientation * (float)(Math.PI / 180.0f);

            float sliceAngle = FOV / slices;
            float beginAngle = cameraAngle - halfFov;

            // draw ceiling and floor
            spriteBatch.Draw(blankTexture, new Rectangle(0, 0, viewport.Width, viewport.Height / 2),
                             Color.FromNonPremultiplied(23, 14, 8, 255));
            spriteBatch.Draw(blankTexture, new Rectangle(0, viewport.Height / 2, viewport.Width, viewport.Height / 2),
                             Color.DarkKhaki);

            // draw all wallslices
            for (int x = 0; x < slices; x++)
            {
                float angle = beginAngle + x * sliceAngle;

                RayCaster.HitData castData;
                if (!RayCaster.RayIntersectsGrid(camera, angle, cellSize, out castData,
                                                 map.GetIsTileOccupiedFunction(wallsLayer)))
                {
                    continue;
                }

                // get the texture slice
                int          tileIndex = (int)(castData.tileCoordinates.Y * mapData.Width + castData.tileCoordinates.X);
                TmxLayer     wallLayer = mapData.Layers[wallsLayer];
                TmxLayerTile tile      = wallLayer.Tiles[tileIndex];
                TmxTileset   tileset   = map.GetTilesetForTile(tile);
                if (tileset == null)
                {
                    continue;
                }

                // fix fisheye for distance and get slice height
                float distance    = (float)(castData.rayLength * Math.Cos(angle - cameraAngle));
                int   sliceHeight = (int)(cellSize * focalLength / distance);
                zBuffer[x] = distance;

                // get drawing rectangles
                Rectangle wallRectangle    = new Rectangle(x, viewport.Height / 2 - sliceHeight / 2, 1, sliceHeight);
                Rectangle textureRectangle = map.GetSourceRectangleForTile(tileset, tile);

                textureRectangle.X =
                    (int)(textureRectangle.X + (textureRectangle.Width * castData.cellFraction) % cellSize);
                textureRectangle.Width = 1;

                // get texture tint
                float dot          = Vector2.Dot(castData.normal, Vector2.UnitY);
                Color lightingTint = Math.Abs(dot) > 0.9f ? Color.Gray : Color.White;

                spriteBatch.Draw(map.Textures[tileset], wallRectangle, textureRectangle, lightingTint);
            }
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Determines whether the closest point on the segment lies on one of the endpoints.
        /// </summary>
        /// <param name="point">The point to test to.</param>
        /// <returns>Whether the closest point on this segment to the argument point lies on the endpoints.</returns>
        #endregion
        public bool IsClosestPointOnEndpoint(ref Point point)
        {
            sUnitSegmentForIsClosestPointOnEndpoint.X = (float)(this.Point2.X - this.Point1.X);
            sUnitSegmentForIsClosestPointOnEndpoint.Y = (float)(Point2.Y - Point1.Y);
            sUnitSegmentForIsClosestPointOnEndpoint.Normalize();

            sPointVectorForIsClosestPointOnEndpoint.X = (float)(point.X - this.Point1.X);
            sPointVectorForIsClosestPointOnEndpoint.Y = (float)(point.Y - Point1.Y);

#if FRB_MDX
            float l = Vector2.Dot(sPointVectorForIsClosestPointOnEndpoint, sUnitSegmentForIsClosestPointOnEndpoint);
#else
            float l;
            Vector2.Dot(ref sPointVectorForIsClosestPointOnEndpoint, ref sUnitSegmentForIsClosestPointOnEndpoint, out l);
#endif
            return(l < 0 || l * l > this.GetLengthSquared());
        }
Ejemplo n.º 6
0
        private void DrawVector(SpriteBatch spriteBatch, IGameObject sprite, int borderSize, Color borderColor)
        {
            // Calculate line size and angle
            var       sr    = sprite.GetComponent <SpriteRenderer>();
            var       rb    = sprite.GetComponent <Rigidbody>();
            Vector2   vv    = new Vector2(rb.velocity.X, rb.velocity.Y);
            Vector2   begin = new Vector2(sprite.Transform.Position.X, sprite.Transform.Position.Y);
            Vector2   end   = begin + vv;
            Rectangle r     = new Rectangle((int)begin.X, (int)begin.Y, (int)(end - begin).Length() + borderSize, borderSize);
            Vector2   v     = Vector2.Normalize(begin - end);
            float     angle = (float)Math.Acos(Vector2.Dot(v, -Vector2.UnitX));

            if (begin.Y > end.Y)
            {
                angle = MathHelper.TwoPi - angle;
            }

            // Calculate text and background sizes
            Vector2 XtextSize = _spriteFont.MeasureString("X:" + (int)vv.X);
            Vector2 YtextSize = _spriteFont.MeasureString("Y:" + (int)vv.Y);
            int     maxWidth  = (int)XtextSize.X;

            if (YtextSize.X > maxWidth)
            {
                maxWidth = (int)YtextSize.X;
            }
            Rectangle textSize = new Rectangle((int)end.X - maxWidth / 2, (int)end.Y - (int)XtextSize.Y + 4,
                                               maxWidth + 4, (int)XtextSize.Y + 9);

            // Draw vector line and component values
            spriteBatch.Draw(_pixel, r, null, borderColor, angle, Vector2.Zero, SpriteEffects.None, 0.0002f);

            if (ShowVectorPositionLabels)
            {
                spriteBatch.DrawString(_spriteFont, "X:" + (int)vv.X, new Vector2(textSize.X + 2, end.Y - 12), Color.White, 0, Vector2.Zero, 1f, SpriteEffects.None, 0);
                spriteBatch.DrawString(_spriteFont, "Y:" + (int)vv.Y, new Vector2(textSize.X + 2, end.Y - 2), Color.White, 0, Vector2.Zero, 1f, SpriteEffects.None, 0);

                // Draw text background
                spriteBatch.Draw(_consoleWindow, textSize, null, Color.White, 0, Vector2.Zero, SpriteEffects.None, 0.0001f);
            }
        }
Ejemplo n.º 7
0
        /// <summary>
        /// Returns the distance to the argument point as well as
        /// the connectin Vector3 from the Point to this.
        /// </summary>
        /// <param name="point">The point to get the distance to.</param>
        /// <param name="connectingVector">The connecting vector from the argument Pointn to this.</param>
        /// <returns>The distance between this and the argument Point.</returns>
        #endregion
        public float DistanceTo(Point point, out Vector3 connectingVector)
        {
            connectingVector = new Vector3();
            float segmentLength = (float)GetLength();

            Vector2 normalizedLine = new Vector2(
                (float)(Point2.X - Point1.X) / segmentLength,
                (float)(Point2.Y - Point1.Y) / segmentLength);

            Vector2 pointVector = new Vector2((float)(point.X - Point1.X), (float)(point.Y - Point1.Y));

            float length = Vector2.Dot(pointVector, normalizedLine);

            if (length < 0)
            {
                connectingVector.X = (float)(Point1.X - point.X);
                connectingVector.Y = (float)(Point1.Y - point.Y);
                connectingVector.Z = 0;

                return(connectingVector.Length());
            }
            else if (length > segmentLength)
            {
                connectingVector.X = (float)(Point2.X - point.X);
                connectingVector.Y = (float)(Point2.Y - point.Y);
                connectingVector.Z = 0;
                return(connectingVector.Length());
            }
            else
            {
                Point tempPoint = new Point(Point1.X + length * normalizedLine.X,
                                            Point1.Y + length * normalizedLine.Y);

                connectingVector.X = (float)(tempPoint.X - point.X);
                connectingVector.Y = (float)(tempPoint.Y - point.Y);
                connectingVector.Z = 0;

                return(connectingVector.Length());
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Draws the sprite
        /// </summary>
        /// <param name="spriteBatch">batch to draw sprites with</param>
        /// <param name="position">position of the sprite</param>
        /// <param name="texture">texture to draw</param>
        /// <param name="camera">camera position</param>
        /// <param name="orientation">camera angle in degrees</param>
        public void RenderSprite(SpriteBatch spriteBatch, Vector2 position, Texture2D texture, Rectangle source,
                                 Vector2 camera, float orientation)
        {
            int   slices      = viewport.Width;
            int   halfSlice   = slices / 2;
            float halfFov     = FOV / 2;
            float cameraAngle = orientation * (float)(Math.PI / 180.0f);
            float focalLength = halfSlice / (float)Math.Tan(halfFov);

            Vector2 cameraForward     = new Vector2((float)Math.Cos(cameraAngle), (float)Math.Sin(cameraAngle));
            Vector2 spriteCameraSpace = position - camera;

            if (Vector2.Dot(cameraForward, spriteCameraSpace) <= 0)
            {
                return;
            }

            float angleToSprite     = (float)Math.Atan2(spriteCameraSpace.Y, spriteCameraSpace.X) - cameraAngle;
            float correctedDistance = (float)(spriteCameraSpace.Length() * Math.Cos(angleToSprite));
            int   spriteSize        = (int)(source.Width * focalLength / correctedDistance);
            int   spritePosition    = (int)(Math.Tan(angleToSprite) * focalLength + halfSlice);

            // draw slices for sprite
            int halfSprite    = spriteSize / 2;
            int startPosition = spritePosition - halfSprite;
            int endPosition   = spritePosition + halfSprite;
            int tileStart     = source.X;

            if (endPosition < 0 || startPosition >= slices)
            {
                return;
            }

            if (startPosition < 0)
            {
                startPosition = 0;
            }

            bool noOffsetNeeded = false;

            if (endPosition >= slices)
            {
                endPosition    = slices - 1;
                noOffsetNeeded = true;
            }

            int   spriteSizeRange = endPosition - startPosition;
            float spritePart      = source.Width / (float)spriteSize;
            float sourceOffset    = noOffsetNeeded ? 0 : source.Width - spriteSizeRange / (float)spriteSize * source.Width;

            source.Width = (int)Math.Ceiling(spritePart);
            for (int x = 0; x < spriteSizeRange; x++)
            {
                int screenColumn = startPosition + x;
                if (zBuffer[screenColumn] < correctedDistance)
                {
                    continue;
                }

                source.X = tileStart + (int)(sourceOffset + x * spritePart);

                spriteBatch.Draw(texture,
                                 new Rectangle(screenColumn, viewport.Height / 2 - halfSprite, 1, spriteSize),
                                 source, Color.White);
            }
        }
Ejemplo n.º 9
0
 public static double dot_product(Vector2 point1, Vector2 point2)
 {
     return(ApplyEpsilon(point1.Dot(point2)));
 }