Advertisement
Guest User

CentauriBoy

a guest
Jul 13th, 2009
2,101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 44.02 KB | None | 0 0
  1. //////////////////////////////////////////////////////////////////////////
  2. // Class    :   BasicPrimitives
  3. //
  4. // Purpose  :   Render simple 2D shapes.
  5. //////////////////////////////////////////////////////////////////////////
  6. public class BasicPrimitives
  7. {
  8.     /*********************************************************************/
  9.     // Members.
  10.     /*********************************************************************/
  11.  
  12.     #region Fields
  13.  
  14.     /// <summary>The color of the primitive object.</summary>
  15.     private Color m_Color = Color.White;
  16.  
  17.     /// <summary>The position of the primitive object.</summary>
  18.     private Vector2 m_vPosition = Vector2.Zero;
  19.  
  20.     /// <summary>The render depth of the primitive line object (0 = front, 1 = back).</summary>
  21.     private float m_fDepth = 0f;
  22.  
  23.     /// <summary>The thickness of the shape's edge.</summary>
  24.     private float m_fThickness = 1f;
  25.  
  26.     /// <summary>1x1 pixel that creates the shape.</summary>
  27.     private Texture2D m_Pixel = null;
  28.  
  29.     /// <summary>List of vectors.</summary>
  30.     private List<Vector2> m_VectorList = new List<Vector2>();
  31.  
  32.     #endregion // Fields
  33.  
  34.     #region Properties
  35.  
  36.     //////////////////////////////////////////////////////////////////////////
  37.     /// <summary>
  38.     /// Get/Set the colour of the primitive object.
  39.     /// </summary>
  40.     //////////////////////////////////////////////////////////////////////////
  41.     public Color Colour
  42.     {
  43.         get { return m_Color; }
  44.         set { m_Color = value; }
  45.     }
  46.  
  47.     //////////////////////////////////////////////////////////////////////////
  48.     /// <summary>
  49.     /// Get/Set the position of the primitive object.
  50.     /// </summary>
  51.     //////////////////////////////////////////////////////////////////////////
  52.     public Vector2 Position
  53.     {
  54.         get { return m_vPosition; }
  55.         set { m_vPosition = value; }
  56.     }
  57.  
  58.     //////////////////////////////////////////////////////////////////////////
  59.     /// <summary>
  60.     /// Get/Set the render depth of the primitive line object (0 = front, 1 = back).
  61.     /// </summary>
  62.     //////////////////////////////////////////////////////////////////////////
  63.     public float Depth
  64.     {
  65.         get { return m_fDepth; }
  66.         set { m_fDepth = value; }
  67.     }
  68.  
  69.     //////////////////////////////////////////////////////////////////////////
  70.     /// <summary>
  71.     /// Get/Set the thickness of the shape's edge.
  72.     /// </summary>
  73.     //////////////////////////////////////////////////////////////////////////
  74.     public float Thickness
  75.     {
  76.         get { return m_fThickness; }
  77.         set { m_fThickness = value; }
  78.     }
  79.  
  80.     //////////////////////////////////////////////////////////////////////////
  81.     /// <summary>
  82.     /// Gets the number of vectors which make up the primitive object.
  83.     /// </summary>
  84.     //////////////////////////////////////////////////////////////////////////
  85.     public int CountVectors
  86.     {
  87.         get { return m_VectorList.Count; }
  88.     }
  89.  
  90.     //////////////////////////////////////////////////////////////////////////
  91.     /// <summary>
  92.     /// Gets the vector position from the list.
  93.     /// </summary>
  94.     /// <param name="_nIndex">The index to get from.</param>
  95.     //////////////////////////////////////////////////////////////////////////
  96.     public Vector2 GetVector(int _nIndex)
  97.     {
  98.         return m_VectorList[_nIndex];
  99.     }
  100.  
  101.     #endregion // Properties
  102.  
  103.     /*********************************************************************/
  104.     // Functions.
  105.     /*********************************************************************/
  106.  
  107.     #region Initialization | Dispose
  108.  
  109.     //////////////////////////////////////////////////////////////////////////
  110.     /// <summary>
  111.     /// Creates a new primitive object.
  112.     /// </summary>
  113.     /// <param name="_graphicsDevice">The graphics device object to use.</param>
  114.     //////////////////////////////////////////////////////////////////////////
  115.     public BasicPrimitives(GraphicsDevice _graphicsDevice)
  116.     {
  117.         //////////////////////////////////////////////////////////////////////////
  118.         // Create the pixel texture.
  119.         m_Pixel = new Texture2D(_graphicsDevice, 1, 1, 1, TextureUsage.None, SurfaceFormat.Color);
  120.         m_Pixel.SetData<Color>(new Color[] { Color.White });
  121.         //
  122.         //////////////////////////////////////////////////////////////////////////
  123.     }
  124.  
  125.     //////////////////////////////////////////////////////////////////////////
  126.     /// <summary>
  127.     /// Called when the primitive object is destroyed.
  128.     /// </summary>
  129.     //////////////////////////////////////////////////////////////////////////
  130.     ~BasicPrimitives()
  131.     {
  132.         m_Pixel.Dispose();
  133.         m_VectorList.Clear();
  134.     }
  135.  
  136.     #endregion // Initialization | Dispose
  137.  
  138.     #region List Manipulation Methods
  139.  
  140.     //////////////////////////////////////////////////////////////////////////
  141.     /// <summary>
  142.     /// Adds a vector to the primitive object.
  143.     /// </summary>
  144.     /// <param name="_vPosition">The vector to add.</param>
  145.     //////////////////////////////////////////////////////////////////////////
  146.     public void AddVector(Vector2 _vPosition)
  147.     {
  148.         m_VectorList.Add(_vPosition);
  149.     }
  150.  
  151.     //////////////////////////////////////////////////////////////////////////
  152.     /// <summary>
  153.     /// Inserts a vector into the primitive object.
  154.     /// </summary>
  155.     /// <param name="_nIndex">The index to insert it at.</param>
  156.     /// <param name="_vPosition">The vector to insert.</param>
  157.     //////////////////////////////////////////////////////////////////////////
  158.     public void InsertVector(int _nIndex, Vector2 _vPosition)
  159.     {
  160.         m_VectorList.Insert(_nIndex, _vPosition);
  161.     }
  162.  
  163.     //////////////////////////////////////////////////////////////////////////
  164.     /// <summary>
  165.     /// Removes a vector from the primitive object.
  166.     /// </summary>
  167.     /// <param name="_vPosition">The vector to remove.</param>
  168.     //////////////////////////////////////////////////////////////////////////
  169.     public void RemoveVector(Vector2 _vPosition)
  170.     {
  171.         m_VectorList.Remove(_vPosition);
  172.     }
  173.  
  174.     //////////////////////////////////////////////////////////////////////////
  175.     /// <summary>
  176.     /// Removes a vector from the primitive object.
  177.     /// </summary>
  178.     /// <param name="_nIndex">The index of the vector to remove.</param>
  179.     //////////////////////////////////////////////////////////////////////////
  180.     public void RemoveVector(int _nIndex)
  181.     {
  182.         m_VectorList.RemoveAt(_nIndex);
  183.     }
  184.  
  185.     //////////////////////////////////////////////////////////////////////////
  186.     /// <summary>
  187.     /// Clears all vectors from the list.
  188.     /// </summary>
  189.     //////////////////////////////////////////////////////////////////////////
  190.     public void ClearVectors()
  191.     {
  192.         m_VectorList.Clear();
  193.     }
  194.  
  195.     #endregion // List Manipulation Methods
  196.  
  197.     #region Creation Methods
  198.  
  199.     //////////////////////////////////////////////////////////////////////////
  200.     /// <summary>
  201.     /// Create a line primitive.
  202.     /// </summary>
  203.     /// <param name="_vStart">Start of the line, in pixels.</param>
  204.     /// <param name="_vEnd">End of the line, in pixels.</param>
  205.     //////////////////////////////////////////////////////////////////////////
  206.     public void CreateLine(Vector2 _vStart, Vector2 _vEnd)
  207.     {
  208.         m_VectorList.Clear();
  209.         m_VectorList.Add(_vStart);
  210.         m_VectorList.Add(_vEnd);
  211.     }
  212.  
  213.     //////////////////////////////////////////////////////////////////////////
  214.     /// <summary>
  215.     /// Create a triangle primitive.
  216.     /// </summary>
  217.     /// <param name="_vPoint1">Fist point, in pixels.</param>
  218.     /// <param name="_vPoint2">Second point, in pixels.</param>
  219.     /// <param name="_vPoint3">Third point, in pixels.</param>
  220.     //////////////////////////////////////////////////////////////////////////
  221.     public void CreateTriangle(Vector2 _vPoint1, Vector2 _vPoint2, Vector2 _vPoint3)
  222.     {
  223.         m_VectorList.Clear();
  224.         m_VectorList.Add(_vPoint1);
  225.         m_VectorList.Add(_vPoint2);
  226.         m_VectorList.Add(_vPoint3);
  227.         m_VectorList.Add(_vPoint1);
  228.     }
  229.  
  230.     //////////////////////////////////////////////////////////////////////////
  231.     /// <summary>
  232.     /// Create a square primitive.
  233.     /// </summary>
  234.     /// <param name="_vTopLeft">Top left hand corner of the square.</param>
  235.     /// <param name="_vBottomRight">Bottom right hand corner of the square.</param>
  236.     //////////////////////////////////////////////////////////////////////////
  237.     public void CreateSquare(Vector2 _vTopLeft, Vector2 _vBottomRight)
  238.     {
  239.         m_VectorList.Clear();
  240.         m_VectorList.Add(_vTopLeft);
  241.         m_VectorList.Add(new Vector2(_vTopLeft.X, _vBottomRight.Y));
  242.         m_VectorList.Add(_vBottomRight);
  243.         m_VectorList.Add(new Vector2(_vBottomRight.X, _vTopLeft.Y));
  244.         m_VectorList.Add(_vTopLeft);
  245.     }
  246.  
  247.     //////////////////////////////////////////////////////////////////////////
  248.     /// <summary>
  249.     /// Creates a circle starting from (0, 0).
  250.     /// </summary>
  251.     /// <param name="_fRadius">The radius (half the width) of the circle.</param>
  252.     /// <param name="_nSides">The number of sides on the circle. (64 is average).</param>
  253.     //////////////////////////////////////////////////////////////////////////
  254.     public void CreateCircle(float _fRadius, int _nSides)
  255.     {
  256.         m_VectorList.Clear();
  257.  
  258.         //////////////////////////////////////////////////////////////////////////
  259.         // Local variables.
  260.         float fMax = (float)MathHelper.TwoPi;
  261.         float fStep = fMax / (float)_nSides;
  262.         //
  263.         //////////////////////////////////////////////////////////////////////////
  264.  
  265.         //////////////////////////////////////////////////////////////////////////
  266.         // Create the full circle.
  267.         for (float fTheta = fMax; fTheta >= -1; fTheta -= fStep)
  268.         {
  269.             m_VectorList.Add(new Vector2(_fRadius * (float)Math.Cos((double)fTheta),
  270.                                          _fRadius * (float)Math.Sin((double)fTheta)));
  271.         }
  272.         //
  273.         //////////////////////////////////////////////////////////////////////////
  274.     }
  275.  
  276.     //////////////////////////////////////////////////////////////////////////
  277.     /// <summary>
  278.     /// Creates an ellipse starting from (0, 0) with the given width and height.
  279.     /// Vectors are generated using the parametric equation of an ellipse.
  280.     /// </summary>
  281.     /// <param name="_fSemiMajorAxis">The width of the ellipse at its center.</param>
  282.     /// <param name="_fSemiMinorAxis">The height of the ellipse at its center.</param>
  283.     /// <param name="_nSides">The number of sides on the ellipse. (64 is average).</param>
  284.     //////////////////////////////////////////////////////////////////////////
  285.     public void CreateEllipse(float _fSemiMajorAxis, float _fSemiMinorAxis, int _nSides)
  286.     {
  287.         m_VectorList.Clear();
  288.  
  289.         //////////////////////////////////////////////////////////////////////////
  290.         // Local variables.
  291.         float fMax = (float)MathHelper.TwoPi;
  292.         float fStep = fMax / (float)_nSides;
  293.         //
  294.         //////////////////////////////////////////////////////////////////////////
  295.  
  296.         //////////////////////////////////////////////////////////////////////////
  297.         // Create full ellipse.
  298.         for (float fTheta = fMax; fTheta >= -1; fTheta -= fStep)
  299.         {
  300.             m_VectorList.Add(new Vector2((float)(_fSemiMajorAxis * Math.Cos(fTheta)),
  301.                                          (float)(_fSemiMinorAxis * Math.Sin(fTheta))));
  302.         }
  303.         //
  304.         //////////////////////////////////////////////////////////////////////////
  305.     }
  306.  
  307.     #endregion // Creation Methods
  308.  
  309.     #region Render Methods
  310.  
  311.     //////////////////////////////////////////////////////////////////////////
  312.     /// <summary>
  313.     /// Render points of the primitive.
  314.     /// </summary>
  315.     /// <param name="_spriteBatch">The sprite batch to use to render the primitive object.</param>
  316.     //////////////////////////////////////////////////////////////////////////
  317.     public void RenderPointPrimitive(SpriteBatch _spriteBatch)
  318.     {
  319.         //////////////////////////////////////////////////////////////////////////
  320.         // Validate.
  321.         if (m_VectorList.Count <= 0)
  322.             return;
  323.         //
  324.         //////////////////////////////////////////////////////////////////////////
  325.  
  326.         //////////////////////////////////////////////////////////////////////////
  327.         // Local variables.
  328.         Vector2 vPosition1 = Vector2.Zero, vPosition2 = Vector2.Zero;
  329.         float fAngle = 0f;
  330.         //
  331.         //////////////////////////////////////////////////////////////////////////
  332.  
  333.         //////////////////////////////////////////////////////////////////////////
  334.         // Run through the list of vectors.
  335.         for (int i = m_VectorList.Count - 1; i >= 1; --i)
  336.         {
  337.             // Store positions.
  338.             vPosition1 = m_VectorList[i - 1];
  339.             vPosition2 = m_VectorList[i];
  340.  
  341.             // Calculate the angle between the two vectors.
  342.             fAngle = (float)Math.Atan2((double)(vPosition2.Y - vPosition1.Y),
  343.                                        (double)(vPosition2.X - vPosition1.X));
  344.  
  345.             // Stretch the pixel between the two vectors.
  346.             _spriteBatch.Draw(m_Pixel,
  347.                               m_vPosition + m_VectorList[i],
  348.                               null,
  349.                               m_Color,
  350.                               fAngle,
  351.                               new Vector2(0.5f, 0.5f),
  352.                               m_fThickness,
  353.                               SpriteEffects.None,
  354.                               m_fDepth);
  355.         }
  356.         //
  357.         //////////////////////////////////////////////////////////////////////////
  358.     }
  359.  
  360.     //////////////////////////////////////////////////////////////////////////
  361.     /// <summary>
  362.     /// Render points of the primitive.
  363.     /// </summary>
  364.     /// <param name="_spriteBatch">The sprite batch to use to render the primitive object.</param>
  365.     /// <param name="_fAngle">The counterclockwise rotation in radians. (0.0f is default).</param>
  366.     /// <param name="_vPivot">Position in which to rotate around.</param>
  367.     //////////////////////////////////////////////////////////////////////////
  368.     public void RenderPointPrimitive(SpriteBatch _spriteBatch, float _fAngle, Vector2 _vPivot)
  369.     {
  370.         //////////////////////////////////////////////////////////////////////////
  371.         // Validate.
  372.         if (m_VectorList.Count <= 0)
  373.             return;
  374.         //
  375.         //////////////////////////////////////////////////////////////////////////
  376.  
  377.         //////////////////////////////////////////////////////////////////////////
  378.         // Rotate object based on pivot.
  379.         Rotate(_fAngle, _vPivot);
  380.         //
  381.         //////////////////////////////////////////////////////////////////////////
  382.  
  383.         //////////////////////////////////////////////////////////////////////////
  384.         // Local variables.
  385.         Vector2 vPosition1 = Vector2.Zero, vPosition2 = Vector2.Zero;
  386.         float fAngle = 0f;
  387.         //
  388.         //////////////////////////////////////////////////////////////////////////
  389.  
  390.         //////////////////////////////////////////////////////////////////////////
  391.         // Run through the list of vectors.
  392.         for (int i = m_VectorList.Count - 1; i >= 1; --i)
  393.         {
  394.             // Store positions.
  395.             vPosition1 = m_VectorList[i - 1];
  396.             vPosition2 = m_VectorList[i];
  397.  
  398.             // Calculate the angle between the two vectors.
  399.             fAngle = (float)Math.Atan2((double)(vPosition2.Y - vPosition1.Y),
  400.                                        (double)(vPosition2.X - vPosition1.X));
  401.  
  402.             // Stretch the pixel between the two vectors.
  403.             _spriteBatch.Draw(m_Pixel,
  404.                               m_vPosition + m_VectorList[i],
  405.                               null,
  406.                               m_Color,
  407.                               fAngle,
  408.                               new Vector2(0.5f, 0.5f),
  409.                               m_fThickness,
  410.                               SpriteEffects.None,
  411.                               m_fDepth);
  412.         }
  413.         //
  414.         //////////////////////////////////////////////////////////////////////////
  415.     }
  416.  
  417.     //////////////////////////////////////////////////////////////////////////
  418.     /// <summary>
  419.     /// Render the lines of the primitive.
  420.     /// </summary>
  421.     /// <param name="_spriteBatch">The sprite batch to use to render the primitive object.</param>
  422.     //////////////////////////////////////////////////////////////////////////
  423.     public void RenderLinePrimitive(SpriteBatch _spriteBatch)
  424.     {
  425.         //////////////////////////////////////////////////////////////////////////
  426.         // Validate.
  427.         if (m_VectorList.Count < 2)
  428.             return;
  429.         //
  430.         //////////////////////////////////////////////////////////////////////////
  431.  
  432.         //////////////////////////////////////////////////////////////////////////
  433.         // Local variables.
  434.         Vector2 vPosition1 = Vector2.Zero, vPosition2 = Vector2.Zero;
  435.         float fDistance = 0f, fAngle = 0f;
  436.         //
  437.         //////////////////////////////////////////////////////////////////////////
  438.  
  439.         //////////////////////////////////////////////////////////////////////////
  440.         // Run through the list of vectors.
  441.         for (int i = m_VectorList.Count - 1; i >= 1; --i)
  442.         {
  443.             // Store positions.
  444.             vPosition1 = m_VectorList[i - 1];
  445.             vPosition2 = m_VectorList[i];
  446.  
  447.             // Calculate the distance between the two vectors.
  448.             fDistance = Vector2.Distance(vPosition1, vPosition2);
  449.  
  450.             // Calculate the angle between the two vectors.
  451.             fAngle = (float)Math.Atan2((double)(vPosition2.Y - vPosition1.Y),
  452.                                        (double)(vPosition2.X - vPosition1.X));
  453.  
  454.             // Stretch the pixel between the two vectors.
  455.             _spriteBatch.Draw(m_Pixel,
  456.                               m_vPosition + vPosition1,
  457.                               null,
  458.                               m_Color,
  459.                               fAngle,
  460.                               new Vector2(0, 0.5f),
  461.                               new Vector2(fDistance, m_fThickness),
  462.                               SpriteEffects.None,
  463.                               m_fDepth);
  464.         }
  465.         //
  466.         //////////////////////////////////////////////////////////////////////////
  467.     }
  468.  
  469.     //////////////////////////////////////////////////////////////////////////
  470.     /// <summary>
  471.     /// Render the lines of the primitive.
  472.     /// </summary>
  473.     /// <param name="_spriteBatch">The sprite batch to use to render the primitive object.</param>
  474.     /// <param name="_fAngle">The counterclockwise rotation in radians. (0.0f is default).</param>
  475.     /// <param name="_vPivot">Position in which to rotate around.</param>
  476.     //////////////////////////////////////////////////////////////////////////
  477.     public void RenderLinePrimitive(SpriteBatch _spriteBatch, float _fAngle, Vector2 _vPivot)
  478.     {
  479.         //////////////////////////////////////////////////////////////////////////
  480.         // Validate.
  481.         if (m_VectorList.Count < 2)
  482.             return;
  483.         //
  484.         //////////////////////////////////////////////////////////////////////////
  485.  
  486.         //////////////////////////////////////////////////////////////////////////
  487.         // Rotate object based on pivot.
  488.         Rotate(_fAngle, _vPivot);
  489.         //
  490.         //////////////////////////////////////////////////////////////////////////
  491.  
  492.         //////////////////////////////////////////////////////////////////////////
  493.         // Local variables.
  494.         Vector2 vPosition1 = Vector2.Zero, vPosition2 = Vector2.Zero;
  495.         float fDistance = 0f, fAngle = 0f;
  496.         //
  497.         //////////////////////////////////////////////////////////////////////////
  498.  
  499.         //////////////////////////////////////////////////////////////////////////
  500.         // Run through the list of vectors.
  501.         for (int i = m_VectorList.Count - 1; i >= 1; --i)
  502.         {
  503.             // Store positions.
  504.             vPosition1 = m_VectorList[i - 1];
  505.             vPosition2 = m_VectorList[i];
  506.  
  507.             // Calculate the distance between the two vectors.
  508.             fDistance = Vector2.Distance(vPosition1, vPosition2);
  509.  
  510.             // Calculate the angle between the two vectors.
  511.             fAngle = (float)Math.Atan2((double)(vPosition2.Y - vPosition1.Y),
  512.                                        (double)(vPosition2.X - vPosition1.X));
  513.  
  514.             // Stretch the pixel between the two vectors.
  515.             _spriteBatch.Draw(m_Pixel,
  516.                               m_vPosition + vPosition1,
  517.                               null,
  518.                               m_Color,
  519.                               fAngle,
  520.                               new Vector2(0, 0.5f),
  521.                               new Vector2(fDistance, m_fThickness),
  522.                               SpriteEffects.None,
  523.                               m_fDepth);
  524.         }
  525.         //
  526.         //////////////////////////////////////////////////////////////////////////
  527.     }
  528.  
  529.     //////////////////////////////////////////////////////////////////////////
  530.     /// <summary>
  531.     /// Render primitive by using a square algorithm.
  532.     /// </summary>
  533.     /// <param name="_spriteBatch">The sprite batch to use to render the primitive object.</param>
  534.     //////////////////////////////////////////////////////////////////////////
  535.     public void RenderSquarePrimitive(SpriteBatch _spriteBatch)
  536.     {
  537.         //////////////////////////////////////////////////////////////////////////
  538.         // Validate.
  539.         if (m_VectorList.Count < 2)
  540.             return;
  541.         //
  542.         //////////////////////////////////////////////////////////////////////////
  543.  
  544.         //////////////////////////////////////////////////////////////////////////
  545.         // Local variables.
  546.         Vector2 vPosition1 = Vector2.Zero, vPosition2 = Vector2.Zero, vLength = Vector2.Zero;
  547.         float fDistance = 0f, fAngle = 0f;
  548.         int nCount = 0;
  549.         //
  550.         //////////////////////////////////////////////////////////////////////////
  551.  
  552.         //////////////////////////////////////////////////////////////////////////
  553.         // Run through the list of vectors.
  554.         for (int i = m_VectorList.Count - 1; i >= 1; --i)
  555.         {
  556.             //////////////////////////////////////////////////////////////////////////
  557.             // Store positions.
  558.             vPosition1 = m_VectorList[i - 1];
  559.             vPosition2 = m_VectorList[i];
  560.             //
  561.             //////////////////////////////////////////////////////////////////////////
  562.  
  563.             //////////////////////////////////////////////////////////////////////////
  564.             // Calculate the distance between the two vectors.
  565.             fDistance = Vector2.Distance(vPosition1, vPosition2);
  566.  
  567.             // Calculate the angle between the two vectors.
  568.             fAngle = (float)Math.Atan2((double)(vPosition2.Y - vPosition1.Y),
  569.                                        (double)(vPosition2.X - vPosition1.X));
  570.  
  571.             // Calculate length.
  572.             vLength = vPosition2 - vPosition1;
  573.             vLength.Normalize();
  574.  
  575.             // Calculate count for roundness.
  576.             nCount = (int)Math.Round(fDistance);
  577.             //
  578.             //////////////////////////////////////////////////////////////////////////
  579.  
  580.             //////////////////////////////////////////////////////////////////////////
  581.             // Run through and render the primitive.
  582.             while (nCount-- > 0)
  583.             {
  584.                 // Increment position.
  585.                 vPosition1 += vLength;
  586.  
  587.                 // Stretch the pixel between the two vectors.
  588.                 _spriteBatch.Draw(m_Pixel,
  589.                                   m_vPosition + vPosition1,
  590.                                   null,
  591.                                   m_Color,
  592.                                   0,
  593.                                   Vector2.Zero,
  594.                                   m_fThickness,
  595.                                   SpriteEffects.None,
  596.                                   m_fDepth);
  597.             }
  598.             //
  599.             //////////////////////////////////////////////////////////////////////////
  600.         }
  601.         //
  602.         //////////////////////////////////////////////////////////////////////////
  603.     }
  604.  
  605.     //////////////////////////////////////////////////////////////////////////
  606.     /// <summary>
  607.     /// Render primitive by using a square algorithm.
  608.     /// </summary>
  609.     /// <param name="_spriteBatch">The sprite batch to use to render the primitive object.</param>
  610.     /// <param name="_fAngle">The counterclockwise rotation in radians. (0.0f is default).</param>
  611.     /// <param name="_vPivot">Position in which to rotate around.</param>
  612.     //////////////////////////////////////////////////////////////////////////
  613.     public void RenderSquarePrimitive(SpriteBatch _spriteBatch, float _fAngle, Vector2 _vPivot)
  614.     {
  615.         //////////////////////////////////////////////////////////////////////////
  616.         // Validate.
  617.         if (m_VectorList.Count < 2)
  618.             return;
  619.         //
  620.         //////////////////////////////////////////////////////////////////////////
  621.  
  622.         //////////////////////////////////////////////////////////////////////////
  623.         // Rotate object based on pivot.
  624.         Rotate(_fAngle, _vPivot);
  625.         //
  626.         //////////////////////////////////////////////////////////////////////////
  627.  
  628.         //////////////////////////////////////////////////////////////////////////
  629.         // Local variables.
  630.         Vector2 vPosition1 = Vector2.Zero, vPosition2 = Vector2.Zero, vLength = Vector2.Zero;
  631.         float fDistance = 0f, fAngle = 0f;
  632.         int nCount = 0;
  633.         //
  634.         //////////////////////////////////////////////////////////////////////////
  635.  
  636.         //////////////////////////////////////////////////////////////////////////
  637.         // Run through the list of vectors.
  638.         for (int i = m_VectorList.Count - 1; i >= 1; --i)
  639.         {
  640.             //////////////////////////////////////////////////////////////////////////
  641.             // Store positions.
  642.             vPosition1 = m_VectorList[i - 1];
  643.             vPosition2 = m_VectorList[i];
  644.             //
  645.             //////////////////////////////////////////////////////////////////////////
  646.  
  647.             //////////////////////////////////////////////////////////////////////////
  648.             // Calculate the distance between the two vectors.
  649.             fDistance = Vector2.Distance(vPosition1, vPosition2);
  650.  
  651.             // Calculate the angle between the two vectors.
  652.             fAngle = (float)Math.Atan2((double)(vPosition2.Y - vPosition1.Y),
  653.                                        (double)(vPosition2.X - vPosition1.X));
  654.  
  655.             // Calculate length.
  656.             vLength = vPosition2 - vPosition1;
  657.             vLength.Normalize();
  658.  
  659.             // Calculate count for roundness.
  660.             nCount = (int)Math.Round(fDistance);
  661.             //
  662.             //////////////////////////////////////////////////////////////////////////
  663.  
  664.             //////////////////////////////////////////////////////////////////////////
  665.             // Run through and render the primitive.
  666.             while (nCount-- > 0)
  667.             {
  668.                 // Increment position.
  669.                 vPosition1 += vLength;
  670.  
  671.                 // Stretch the pixel between the two vectors.
  672.                 _spriteBatch.Draw(m_Pixel,
  673.                                   m_vPosition + vPosition1,
  674.                                   null,
  675.                                   m_Color,
  676.                                   0,
  677.                                   Vector2.Zero,
  678.                                   m_fThickness,
  679.                                   SpriteEffects.None,
  680.                                   m_fDepth);
  681.             }
  682.             //
  683.             //////////////////////////////////////////////////////////////////////////
  684.         }
  685.         //
  686.         //////////////////////////////////////////////////////////////////////////
  687.     }
  688.  
  689.     //////////////////////////////////////////////////////////////////////////
  690.     /// <summary>
  691.     /// Render primitive by using a round algorithm.
  692.     /// </summary>
  693.     /// <param name="_spriteBatch">The sprite batch to use to render the primitive object.</param>
  694.     //////////////////////////////////////////////////////////////////////////
  695.     public void RenderRoundPrimitive(SpriteBatch _spriteBatch)
  696.     {
  697.         //////////////////////////////////////////////////////////////////////////
  698.         // Validate.
  699.         if (m_VectorList.Count < 2)
  700.             return;
  701.         //
  702.         //////////////////////////////////////////////////////////////////////////
  703.  
  704.         //////////////////////////////////////////////////////////////////////////
  705.         // Local variables.
  706.         Vector2 vPosition1 = Vector2.Zero, vPosition2 = Vector2.Zero, vLength = Vector2.Zero;
  707.         float fDistance = 0f, fAngle = 0f;
  708.         int nCount = 0;
  709.         //
  710.         //////////////////////////////////////////////////////////////////////////
  711.  
  712.         //////////////////////////////////////////////////////////////////////////
  713.         // Run through the list of vectors.
  714.         for (int i = m_VectorList.Count - 1; i >= 1; --i)
  715.         {
  716.             //////////////////////////////////////////////////////////////////////////
  717.             // Store positions.
  718.             vPosition1 = m_VectorList[i - 1];
  719.             vPosition2 = m_VectorList[i];
  720.             //
  721.             //////////////////////////////////////////////////////////////////////////
  722.  
  723.             //////////////////////////////////////////////////////////////////////////
  724.             // Calculate the distance between the two vectors.
  725.             fDistance = Vector2.Distance(vPosition1, vPosition2);
  726.  
  727.             // Calculate the angle between the two vectors.
  728.             fAngle = (float)Math.Atan2((double)(vPosition2.Y - vPosition1.Y),
  729.                                        (double)(vPosition2.X - vPosition1.X));
  730.  
  731.             // Calculate length.
  732.             vLength = vPosition2 - vPosition1;
  733.             vLength.Normalize();
  734.  
  735.             // Calculate count for roundness.
  736.             nCount = (int)Math.Round(fDistance);
  737.             //
  738.             //////////////////////////////////////////////////////////////////////////
  739.  
  740.             //////////////////////////////////////////////////////////////////////////
  741.             // Run through and render the primitive.
  742.             while (nCount-- > 0)
  743.             {
  744.                 // Increment position.
  745.                 vPosition1 += vLength;
  746.  
  747.                 // Stretch the pixel between the two vectors.
  748.                 _spriteBatch.Draw(m_Pixel,
  749.                                   m_vPosition + vPosition1 + 0.5f * (vPosition2 - vPosition1),
  750.                                   null,
  751.                                   m_Color,
  752.                                   fAngle,
  753.                                   new Vector2(0.5f, 0.5f),
  754.                                   new Vector2(fDistance, m_fThickness),
  755.                                   SpriteEffects.None,
  756.                                   m_fDepth);
  757.             }
  758.             //
  759.             //////////////////////////////////////////////////////////////////////////
  760.         }
  761.         //
  762.         //////////////////////////////////////////////////////////////////////////
  763.     }
  764.  
  765.     //////////////////////////////////////////////////////////////////////////
  766.     /// <summary>
  767.     /// Render primitive by using a round algorithm.
  768.     /// </summary>
  769.     /// <param name="_spriteBatch">The sprite batch to use to render the primitive object.</param>
  770.     /// <param name="_fAngle">The counterclockwise rotation in radians. (0.0f is default).</param>
  771.     /// <param name="_vPivot">Position in which to rotate around.</param>
  772.     //////////////////////////////////////////////////////////////////////////
  773.     public void RenderRoundPrimitive(SpriteBatch _spriteBatch, float _fAngle, Vector2 _vPivot)
  774.     {
  775.         //////////////////////////////////////////////////////////////////////////
  776.         // Validate.
  777.         if (m_VectorList.Count < 2)
  778.             return;
  779.         //
  780.         //////////////////////////////////////////////////////////////////////////
  781.  
  782.         //////////////////////////////////////////////////////////////////////////
  783.         // Rotate object based on pivot.
  784.         Rotate(_fAngle, _vPivot);
  785.         //
  786.         //////////////////////////////////////////////////////////////////////////
  787.  
  788.         //////////////////////////////////////////////////////////////////////////
  789.         // Local variables.
  790.         Vector2 vPosition1 = Vector2.Zero, vPosition2 = Vector2.Zero, vLength = Vector2.Zero;
  791.         float fDistance = 0f, fAngle = 0f;
  792.         int nCount = 0;
  793.         //
  794.         //////////////////////////////////////////////////////////////////////////
  795.  
  796.         //////////////////////////////////////////////////////////////////////////
  797.         // Run through the list of vectors.
  798.         for (int i = m_VectorList.Count - 1; i >= 1; --i)
  799.         {
  800.             //////////////////////////////////////////////////////////////////////////
  801.             // Store positions.
  802.             vPosition1 = m_VectorList[i - 1];
  803.             vPosition2 = m_VectorList[i];
  804.             //
  805.             //////////////////////////////////////////////////////////////////////////
  806.  
  807.             //////////////////////////////////////////////////////////////////////////
  808.             // Calculate the distance between the two vectors.
  809.             fDistance = Vector2.Distance(vPosition1, vPosition2);
  810.  
  811.             // Calculate the angle between the two vectors.
  812.             fAngle = (float)Math.Atan2((double)(vPosition2.Y - vPosition1.Y),
  813.                                        (double)(vPosition2.X - vPosition1.X));
  814.  
  815.             // Calculate length.
  816.             vLength = vPosition2 - vPosition1;
  817.             vLength.Normalize();
  818.  
  819.             // Calculate count for roundness.
  820.             nCount = (int)Math.Round(fDistance);
  821.             //
  822.             //////////////////////////////////////////////////////////////////////////
  823.  
  824.             //////////////////////////////////////////////////////////////////////////
  825.             // Run through and render the primitive.
  826.             while (nCount-- > 0)
  827.             {
  828.                 // Increment position.
  829.                 vPosition1 += vLength;
  830.  
  831.                 // Stretch the pixel between the two vectors.
  832.                 _spriteBatch.Draw(m_Pixel,
  833.                                   m_vPosition + vPosition1 + 0.5f * (vPosition2 - vPosition1),
  834.                                   null,
  835.                                   m_Color,
  836.                                   fAngle,
  837.                                   new Vector2(0.5f, 0.5f),
  838.                                   new Vector2(fDistance, m_fThickness),
  839.                                   SpriteEffects.None,
  840.                                   m_fDepth);
  841.             }
  842.             //
  843.             //////////////////////////////////////////////////////////////////////////
  844.         }
  845.         //
  846.         //////////////////////////////////////////////////////////////////////////
  847.     }
  848.  
  849.     //////////////////////////////////////////////////////////////////////////
  850.     /// <summary>
  851.     /// Render primitive by using a point and line algorithm.
  852.     /// </summary>
  853.     /// <param name="_spriteBatch">The sprite batch to use to render the primitive object.</param>
  854.     //////////////////////////////////////////////////////////////////////////
  855.     public void RenderPolygonPrimitive(SpriteBatch _spriteBatch)
  856.     {
  857.         //////////////////////////////////////////////////////////////////////////
  858.         // Validate.
  859.         if (m_VectorList.Count < 2)
  860.             return;
  861.         //
  862.         //////////////////////////////////////////////////////////////////////////
  863.  
  864.         //////////////////////////////////////////////////////////////////////////
  865.         // Local variables.
  866.         Vector2 vPosition1 = Vector2.Zero, vPosition2 = Vector2.Zero;
  867.         float fDistance = 0f, fAngle = 0f;
  868.         //
  869.         //////////////////////////////////////////////////////////////////////////
  870.  
  871.         //////////////////////////////////////////////////////////////////////////
  872.         // Run through the list of vectors.
  873.         for (int i = m_VectorList.Count - 1; i >= 1; --i)
  874.         {
  875.             //////////////////////////////////////////////////////////////////////////
  876.             // Store positions.
  877.             vPosition1 = m_VectorList[i - 1];
  878.             vPosition2 = m_VectorList[i];
  879.             //
  880.             //////////////////////////////////////////////////////////////////////////
  881.  
  882.             //////////////////////////////////////////////////////////////////////////
  883.             // Calculate the distance between the two vectors.
  884.             fDistance = Vector2.Distance(vPosition1, vPosition2);
  885.  
  886.             // Calculate the angle between the two vectors.
  887.             fAngle = (float)Math.Atan2((double)(vPosition2.Y - vPosition1.Y),
  888.                                        (double)(vPosition2.X - vPosition1.X));
  889.             //
  890.             //////////////////////////////////////////////////////////////////////////
  891.  
  892.             //////////////////////////////////////////////////////////////////////////
  893.             // Stretch the pixel between the two vectors.
  894.             _spriteBatch.Draw(m_Pixel,
  895.                               Position + vPosition1 + 0.5f * (vPosition2 - vPosition1),
  896.                               null,
  897.                               m_Color,
  898.                               fAngle,
  899.                               new Vector2(0.5f, 0.5f),
  900.                               new Vector2(fDistance, Thickness),
  901.                               SpriteEffects.None,
  902.                               m_fDepth);
  903.  
  904.             // Render the points of the polygon.
  905.             _spriteBatch.Draw(m_Pixel,
  906.                               m_vPosition + vPosition1,
  907.                               null,
  908.                               m_Color,
  909.                               fAngle,
  910.                               new Vector2(0.5f, 0.5f),
  911.                               m_fThickness,
  912.                               SpriteEffects.None,
  913.                               m_fDepth);
  914.             //
  915.             //////////////////////////////////////////////////////////////////////////
  916.         }
  917.         //
  918.         //////////////////////////////////////////////////////////////////////////
  919.     }
  920.  
  921.     //////////////////////////////////////////////////////////////////////////
  922.     /// <summary>
  923.     /// Render primitive by using a point and line algorithm.
  924.     /// </summary>
  925.     /// <param name="_spriteBatch">The sprite batch to use to render the primitive object.</param>
  926.     /// <param name="_fAngle">The counterclockwise rotation in radians. (0.0f is default).</param>
  927.     /// <param name="_vPivot">Position in which to rotate around.</param>
  928.     //////////////////////////////////////////////////////////////////////////
  929.     public void RenderPolygonPrimitive(SpriteBatch _spriteBatch, float _fAngle, Vector2 _vPivot)
  930.     {
  931.         //////////////////////////////////////////////////////////////////////////
  932.         // Validate.
  933.         if (m_VectorList.Count < 2)
  934.             return;
  935.         //
  936.         //////////////////////////////////////////////////////////////////////////
  937.  
  938.         //////////////////////////////////////////////////////////////////////////
  939.         // Rotate object based on pivot.
  940.         Rotate(_fAngle, _vPivot);
  941.         //
  942.         //////////////////////////////////////////////////////////////////////////
  943.  
  944.         //////////////////////////////////////////////////////////////////////////
  945.         // Local variables.
  946.         Vector2 vPosition1 = Vector2.Zero, vPosition2 = Vector2.Zero;
  947.         float fDistance = 0f, fAngle = 0f;
  948.         //
  949.         //////////////////////////////////////////////////////////////////////////
  950.  
  951.         //////////////////////////////////////////////////////////////////////////
  952.         // Run through the list of vectors.
  953.         for (int i = m_VectorList.Count - 1; i >= 1; --i)
  954.         {
  955.             //////////////////////////////////////////////////////////////////////////
  956.             // Store positions.
  957.             vPosition1 = m_VectorList[i - 1];
  958.             vPosition2 = m_VectorList[i];
  959.             //
  960.             //////////////////////////////////////////////////////////////////////////
  961.  
  962.             //////////////////////////////////////////////////////////////////////////
  963.             // Calculate the distance between the two vectors.
  964.             fDistance = Vector2.Distance(vPosition1, vPosition2);
  965.  
  966.             // Calculate the angle between the two vectors.
  967.             fAngle = (float)Math.Atan2((double)(vPosition2.Y - vPosition1.Y),
  968.                                        (double)(vPosition2.X - vPosition1.X));
  969.             //
  970.             //////////////////////////////////////////////////////////////////////////
  971.  
  972.             //////////////////////////////////////////////////////////////////////////
  973.             // Stretch the pixel between the two vectors.
  974.             _spriteBatch.Draw(m_Pixel,
  975.                               Position + vPosition1 + 0.5f * (vPosition2 - vPosition1),
  976.                               null,
  977.                               m_Color,
  978.                               fAngle,
  979.                               new Vector2(0.5f, 0.5f),
  980.                               new Vector2(fDistance, Thickness),
  981.                               SpriteEffects.None,
  982.                               m_fDepth);
  983.  
  984.             // Render the points of the polygon.
  985.             _spriteBatch.Draw(m_Pixel,
  986.                               m_vPosition + vPosition1,
  987.                               null,
  988.                               m_Color,
  989.                               fAngle,
  990.                               new Vector2(0.5f, 0.5f),
  991.                               m_fThickness,
  992.                               SpriteEffects.None,
  993.                               m_fDepth);
  994.             //
  995.             //////////////////////////////////////////////////////////////////////////
  996.         }
  997.         //
  998.         //////////////////////////////////////////////////////////////////////////
  999.     }
  1000.  
  1001.     #endregion // Render Methods
  1002.  
  1003.     #region Public Methods
  1004.  
  1005.     //////////////////////////////////////////////////////////////////////////
  1006.     /// <summary>
  1007.     /// Rotate primitive object based on pivot.
  1008.     /// </summary>
  1009.     /// <param name="_fAngle">The counterclockwise rotation in radians. (0.0f is default).</param>
  1010.     /// <param name="_vPivot">Position in which to rotate around.</param>
  1011.     //////////////////////////////////////////////////////////////////////////
  1012.     public void Rotate(float _fAngle, Vector2 _vPivot)
  1013.     {
  1014.         //////////////////////////////////////////////////////////////////////////
  1015.         // Subtract pivot from all points.
  1016.         for (int i = m_VectorList.Count - 1; i >= 0; --i)
  1017.             m_VectorList[i] -= _vPivot;
  1018.         //
  1019.         //////////////////////////////////////////////////////////////////////////
  1020.  
  1021.         //////////////////////////////////////////////////////////////////////////
  1022.         // Rotate about the origin.
  1023.         Matrix mat = Matrix.CreateRotationZ(_fAngle);
  1024.         for (int i = m_VectorList.Count - 1; i >= 0; --i)
  1025.             m_VectorList[i] = Vector2.Transform(m_VectorList[i], mat);
  1026.         //
  1027.         //////////////////////////////////////////////////////////////////////////
  1028.  
  1029.         //////////////////////////////////////////////////////////////////////////
  1030.         // Add pivot to all points.
  1031.         for (int i = m_VectorList.Count - 1; i >= 0; --i)
  1032.             m_VectorList[i] += _vPivot;
  1033.         //
  1034.         //////////////////////////////////////////////////////////////////////////
  1035.     }
  1036.  
  1037.     #endregion // Public Methods
  1038. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement