using System; using System.Diagnostics; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; namespace BouncyCatIsBouncy.Debugging { class LineBrush { public float Layer { get; set; } public Color Color { get; set; } public int Thickness { get; set; } private Texture2D m_lineTexture; private Vector2 m_origin; public LineBrush() { Color = Color.Black; Thickness = 1; } public LineBrush(int thickness, Color color) { Color = color; Thickness = thickness; } public void Load(GraphicsDevice graphics) { m_lineTexture = DrawingHelper.CreateLineTexture(graphics, Thickness, Color); m_origin = new Vector2(0f, Thickness / 2f + 1f); } public void Draw(SpriteBatch spriteBatch, Vector2 startPoint, Vector2 endPoint) { Debug.Assert(m_lineTexture != null); Vector2 difference = endPoint - startPoint; float rotation = CalculateRotation(difference); Vector2 scale = CalculateScale(difference); spriteBatch.Draw(m_lineTexture, startPoint, null, Color, rotation, m_origin, scale, SpriteEffects.None, Layer); } private static float CalculateRotation(Vector2 difference) { difference.Normalize(); float theta = Vector2.Dot(Vector2.UnitX, difference); theta = (float)Math.Acos(theta); if (difference.Y < 0) { theta = -theta; } return theta; } private Vector2 CalculateScale(Vector2 difference) { return new Vector2(difference.Length() / m_lineTexture.Width, 1f); } } }