示例#1
0
        /// <summary>
        /// A streak of lightning is drawn simply by piecing together a long string of small line segments, 
        /// each of which is slightly rotated.
        /// </summary>
        private void DrawLightningStreak(LineBatch lineBatch, Color color, Vector2 start, Vector2 end, int numSegments,
            float deviation)
        {
            Random randomGen = new Random();

            Vector2 disp = end - start;
            Vector2 unitDisp = Vector2.Normalize(disp);
            Vector2 stepVector = unitDisp * disp.Length() / (float)numSegments;
            Vector2 perpVec = new Vector2(-stepVector.Y, stepVector.X);
            perpVec.Normalize();

            Vector2 startPoint = start;
            Vector2 endPoint = new Vector2();

            for (int i = 0; i < numSegments; ++i)
            {
                endPoint = start + stepVector * (i + 1);

                // If this is an intermediate segment, apply an offset perpendicular to the line connecting start to end.
                if (i < numSegments - 1)
                {
                    float random = (float)randomGen.NextDouble();
                    float offset = (random >= 0.5f ? 1 : -1) * random * deviation;

                    endPoint += perpVec * offset;
                }

                lineBatch.DrawLine(startPoint, endPoint, color);

                startPoint = endPoint;
            }
        }
示例#2
0
        /// <summary>
        /// Draws the grid, showing the springs connecting neighboring nodes as line segments. The color of each
        /// line segment is interpolated linearly between dimColor and brightColor depending on the length
        /// of the segment.
        /// </summary>
        public void Draw(LineBatch lineBatch, Color dimColor, Color brightColor)
        {
            float dx = (bounds.Right - bounds.Left) / gridSize;
            float dy = (bounds.Bottom - bounds.Top) / gridSize;

            Vector4 vecDimColor = new Vector4(dimColor.R, dimColor.G, dimColor.B, dimColor.A);
            Vector4 vecBrightColor = new Vector4(brightColor.R, brightColor.G, brightColor.B, brightColor.A);

            for (int x = 0; x < gridSize; ++x)
            {
                for (int y = 0; y < gridSize; ++y)
                {
                    if (x < gridSize - 1)
                    {
                        float lineLength = (grid[x + 1, y].Position - grid[x, y].Position).Length();
                        float t = (lineLength - dx) / (5 * (float)Math.Sqrt(2) * dx);

                        Vector4 modulatedColorVec = Vector4.Lerp(vecDimColor, vecBrightColor, t);
                        Color modulatedColor = new Color((byte)modulatedColorVec.X, (byte)modulatedColorVec.Y, (byte)modulatedColorVec.Z,
                                                         (byte)modulatedColorVec.W);

                        lineBatch.DrawLine(grid[x, y].Position, grid[x + 1, y].Position, modulatedColor);
                    }

                    if (y < gridSize - 1)
                    {
                        float lineLength = (grid[x, y + 1].Position - grid[x, y].Position).Length();
                        float t = (lineLength - dy) / (5 * (float)Math.Sqrt(2) * dx);

                        Vector4 modulatedColorVec = Vector4.Lerp(vecDimColor, vecBrightColor, t);
                        Color modulatedColor = new Color((byte)modulatedColorVec.X, (byte)modulatedColorVec.Y, (byte)modulatedColorVec.Z,
                                                         (byte)modulatedColorVec.W);

                        lineBatch.DrawLine(grid[x, y].Position, grid[x, y + 1].Position, modulatedColor);
                    }
                }
            }
        }