Render Polygon Method (Documentation)












2












$begingroup$


I've written a method to render a polygon in C# using the SharpDX libraries and would like a review of my code's maintainability and the documentation around it. The method works fine as is, but I feel that perhaps the documentation could be improved, or that there could be simpler and more maintainable method of executing the algorithm.



/// <summary>
/// Render an n-sided polygon from a list of allowed polygons.
/// </summary>
/// <param name="context2D">The 2D context used to render the polygon.</param>
/// <param name="polygon">The n-sided polygon to render.</param>
/// <param name="center">The central position of the polygon in 2D space</param>
/// <param name="radius">The radius of the polygon.</param>
/// <param name="rotation">The global rotation of the polygon (in degrees).</param>
public void RenderPolygon(DeviceContext context2D, PolygonType polygon, Vector2 center, float radius, float rotation) {
float numberOfPoints = (float)polygon;
Vector2? firstPoint = null, previousPoint = null, currentPoint = null;
for (int n = 0; n < numberOfPoints; n++) {
float theta = ((360.0f / numberOfPoints) * n + rotation) * (float)Math.PI / 180.0f;
currentPoint = center + new Vector2((float)Math.Cos(theta), (float)Math.Sin(theta)) * radius;

if (previousPoint != null && currentPoint != null)
context2D.DrawLine((Vector2)previousPoint, (Vector2)currentPoint, brush);

previousPoint = currentPoint;
if (firstPoint == null)
firstPoint = currentPoint;
}

if (firstPoint != null && currentPoint != null)
context2D.DrawLine((Vector2)currentPoint, (Vector2)firstPoint, brush);
}




With regards to the PolygonType argument, this is an enum with int values for each type; for example:



public enum PolygonType {
Trigon = 3,
Tetragon = 4,
Pentagon = 5
}


There are a total of 30 polygon types offered stopping at Chiliagon which has 1000 sides.





The things I am most interested in are:




  • Ensuring documentation provides as much detail as possible (while remaining concise).

  • Ensuring that the algorithm is easy to follow and maintain.


The one thing I don't currently like about the algorithm:




  • Casting from Vector2? to Vector2 seems redundant (though required in the current case).


Since Vector2's default value is 0, 0, I'm not sure I should use it and have chosen to use null; but since Vector2 is not a nullable type, I have to cast back to the normal Vector2 prior to using my variables.










share|improve this question









$endgroup$

















    2












    $begingroup$


    I've written a method to render a polygon in C# using the SharpDX libraries and would like a review of my code's maintainability and the documentation around it. The method works fine as is, but I feel that perhaps the documentation could be improved, or that there could be simpler and more maintainable method of executing the algorithm.



    /// <summary>
    /// Render an n-sided polygon from a list of allowed polygons.
    /// </summary>
    /// <param name="context2D">The 2D context used to render the polygon.</param>
    /// <param name="polygon">The n-sided polygon to render.</param>
    /// <param name="center">The central position of the polygon in 2D space</param>
    /// <param name="radius">The radius of the polygon.</param>
    /// <param name="rotation">The global rotation of the polygon (in degrees).</param>
    public void RenderPolygon(DeviceContext context2D, PolygonType polygon, Vector2 center, float radius, float rotation) {
    float numberOfPoints = (float)polygon;
    Vector2? firstPoint = null, previousPoint = null, currentPoint = null;
    for (int n = 0; n < numberOfPoints; n++) {
    float theta = ((360.0f / numberOfPoints) * n + rotation) * (float)Math.PI / 180.0f;
    currentPoint = center + new Vector2((float)Math.Cos(theta), (float)Math.Sin(theta)) * radius;

    if (previousPoint != null && currentPoint != null)
    context2D.DrawLine((Vector2)previousPoint, (Vector2)currentPoint, brush);

    previousPoint = currentPoint;
    if (firstPoint == null)
    firstPoint = currentPoint;
    }

    if (firstPoint != null && currentPoint != null)
    context2D.DrawLine((Vector2)currentPoint, (Vector2)firstPoint, brush);
    }




    With regards to the PolygonType argument, this is an enum with int values for each type; for example:



    public enum PolygonType {
    Trigon = 3,
    Tetragon = 4,
    Pentagon = 5
    }


    There are a total of 30 polygon types offered stopping at Chiliagon which has 1000 sides.





    The things I am most interested in are:




    • Ensuring documentation provides as much detail as possible (while remaining concise).

    • Ensuring that the algorithm is easy to follow and maintain.


    The one thing I don't currently like about the algorithm:




    • Casting from Vector2? to Vector2 seems redundant (though required in the current case).


    Since Vector2's default value is 0, 0, I'm not sure I should use it and have chosen to use null; but since Vector2 is not a nullable type, I have to cast back to the normal Vector2 prior to using my variables.










    share|improve this question









    $endgroup$















      2












      2








      2





      $begingroup$


      I've written a method to render a polygon in C# using the SharpDX libraries and would like a review of my code's maintainability and the documentation around it. The method works fine as is, but I feel that perhaps the documentation could be improved, or that there could be simpler and more maintainable method of executing the algorithm.



      /// <summary>
      /// Render an n-sided polygon from a list of allowed polygons.
      /// </summary>
      /// <param name="context2D">The 2D context used to render the polygon.</param>
      /// <param name="polygon">The n-sided polygon to render.</param>
      /// <param name="center">The central position of the polygon in 2D space</param>
      /// <param name="radius">The radius of the polygon.</param>
      /// <param name="rotation">The global rotation of the polygon (in degrees).</param>
      public void RenderPolygon(DeviceContext context2D, PolygonType polygon, Vector2 center, float radius, float rotation) {
      float numberOfPoints = (float)polygon;
      Vector2? firstPoint = null, previousPoint = null, currentPoint = null;
      for (int n = 0; n < numberOfPoints; n++) {
      float theta = ((360.0f / numberOfPoints) * n + rotation) * (float)Math.PI / 180.0f;
      currentPoint = center + new Vector2((float)Math.Cos(theta), (float)Math.Sin(theta)) * radius;

      if (previousPoint != null && currentPoint != null)
      context2D.DrawLine((Vector2)previousPoint, (Vector2)currentPoint, brush);

      previousPoint = currentPoint;
      if (firstPoint == null)
      firstPoint = currentPoint;
      }

      if (firstPoint != null && currentPoint != null)
      context2D.DrawLine((Vector2)currentPoint, (Vector2)firstPoint, brush);
      }




      With regards to the PolygonType argument, this is an enum with int values for each type; for example:



      public enum PolygonType {
      Trigon = 3,
      Tetragon = 4,
      Pentagon = 5
      }


      There are a total of 30 polygon types offered stopping at Chiliagon which has 1000 sides.





      The things I am most interested in are:




      • Ensuring documentation provides as much detail as possible (while remaining concise).

      • Ensuring that the algorithm is easy to follow and maintain.


      The one thing I don't currently like about the algorithm:




      • Casting from Vector2? to Vector2 seems redundant (though required in the current case).


      Since Vector2's default value is 0, 0, I'm not sure I should use it and have chosen to use null; but since Vector2 is not a nullable type, I have to cast back to the normal Vector2 prior to using my variables.










      share|improve this question









      $endgroup$




      I've written a method to render a polygon in C# using the SharpDX libraries and would like a review of my code's maintainability and the documentation around it. The method works fine as is, but I feel that perhaps the documentation could be improved, or that there could be simpler and more maintainable method of executing the algorithm.



      /// <summary>
      /// Render an n-sided polygon from a list of allowed polygons.
      /// </summary>
      /// <param name="context2D">The 2D context used to render the polygon.</param>
      /// <param name="polygon">The n-sided polygon to render.</param>
      /// <param name="center">The central position of the polygon in 2D space</param>
      /// <param name="radius">The radius of the polygon.</param>
      /// <param name="rotation">The global rotation of the polygon (in degrees).</param>
      public void RenderPolygon(DeviceContext context2D, PolygonType polygon, Vector2 center, float radius, float rotation) {
      float numberOfPoints = (float)polygon;
      Vector2? firstPoint = null, previousPoint = null, currentPoint = null;
      for (int n = 0; n < numberOfPoints; n++) {
      float theta = ((360.0f / numberOfPoints) * n + rotation) * (float)Math.PI / 180.0f;
      currentPoint = center + new Vector2((float)Math.Cos(theta), (float)Math.Sin(theta)) * radius;

      if (previousPoint != null && currentPoint != null)
      context2D.DrawLine((Vector2)previousPoint, (Vector2)currentPoint, brush);

      previousPoint = currentPoint;
      if (firstPoint == null)
      firstPoint = currentPoint;
      }

      if (firstPoint != null && currentPoint != null)
      context2D.DrawLine((Vector2)currentPoint, (Vector2)firstPoint, brush);
      }




      With regards to the PolygonType argument, this is an enum with int values for each type; for example:



      public enum PolygonType {
      Trigon = 3,
      Tetragon = 4,
      Pentagon = 5
      }


      There are a total of 30 polygon types offered stopping at Chiliagon which has 1000 sides.





      The things I am most interested in are:




      • Ensuring documentation provides as much detail as possible (while remaining concise).

      • Ensuring that the algorithm is easy to follow and maintain.


      The one thing I don't currently like about the algorithm:




      • Casting from Vector2? to Vector2 seems redundant (though required in the current case).


      Since Vector2's default value is 0, 0, I'm not sure I should use it and have chosen to use null; but since Vector2 is not a nullable type, I have to cast back to the normal Vector2 prior to using my variables.







      c#






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked 7 hours ago









      PerpetualJPerpetualJ

      1418




      1418






















          1 Answer
          1






          active

          oldest

          votes


















          2












          $begingroup$

          The most confusing thing in your code is names of arguments and variables and their types.




          DeviceContext context2D



          Why not deviceContext?




          PolygonType polygon



          It's not polygon (which is collection of points as I expect), it's polygon type, so name it polygonType




          float numberOfPoints = (float)polygon;



          Why are you casting to float? Number of points can be non-integer? Also in my opinion it's not good to give another meanings to enum values except what they mean by their names. Your enum defines polygons types, not numbers of points. I recommend to create dictionary:



          private static readonly Dictionary<PolygonType, int> NumberOfPoints =
          new Dictionary<PolygonType, int>
          {
          [PolygonType.Trigon] = 3,
          // ...
          };


          and use it:



          var numberOfPoints = NumberOfPoints[polygonType];


          Why are you using for polygon types such strange names and not Triangle and Square?



          If it's public API method, add arguments checking using appropriate exceptions (ArgumentOutOfRangeException, InvalidEnumArgumentException and so on). All possible exceptions should be documented via <exception> tags.






          share|improve this answer









          $endgroup$













          • $begingroup$
            Thank you for the feedback; the reason for context2D has been made redundant by making that particular parameter a field in the class, however since DeviceContext is the same in the 2D and 3D namespaces calling it context2D seems to be pretty common across the board. Casting to float was to prevent casting down the line as part of division; and in regards to the PolygonType names within the enum I used the mathematical names to add clarity since a square is not a diamond, but they are both tetragons.
            $endgroup$
            – PerpetualJ
            4 hours ago











          Your Answer





          StackExchange.ifUsing("editor", function () {
          return StackExchange.using("mathjaxEditing", function () {
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          });
          });
          }, "mathjax-editing");

          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "196"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: false,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f211927%2frender-polygon-method-documentation%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2












          $begingroup$

          The most confusing thing in your code is names of arguments and variables and their types.




          DeviceContext context2D



          Why not deviceContext?




          PolygonType polygon



          It's not polygon (which is collection of points as I expect), it's polygon type, so name it polygonType




          float numberOfPoints = (float)polygon;



          Why are you casting to float? Number of points can be non-integer? Also in my opinion it's not good to give another meanings to enum values except what they mean by their names. Your enum defines polygons types, not numbers of points. I recommend to create dictionary:



          private static readonly Dictionary<PolygonType, int> NumberOfPoints =
          new Dictionary<PolygonType, int>
          {
          [PolygonType.Trigon] = 3,
          // ...
          };


          and use it:



          var numberOfPoints = NumberOfPoints[polygonType];


          Why are you using for polygon types such strange names and not Triangle and Square?



          If it's public API method, add arguments checking using appropriate exceptions (ArgumentOutOfRangeException, InvalidEnumArgumentException and so on). All possible exceptions should be documented via <exception> tags.






          share|improve this answer









          $endgroup$













          • $begingroup$
            Thank you for the feedback; the reason for context2D has been made redundant by making that particular parameter a field in the class, however since DeviceContext is the same in the 2D and 3D namespaces calling it context2D seems to be pretty common across the board. Casting to float was to prevent casting down the line as part of division; and in regards to the PolygonType names within the enum I used the mathematical names to add clarity since a square is not a diamond, but they are both tetragons.
            $endgroup$
            – PerpetualJ
            4 hours ago
















          2












          $begingroup$

          The most confusing thing in your code is names of arguments and variables and their types.




          DeviceContext context2D



          Why not deviceContext?




          PolygonType polygon



          It's not polygon (which is collection of points as I expect), it's polygon type, so name it polygonType




          float numberOfPoints = (float)polygon;



          Why are you casting to float? Number of points can be non-integer? Also in my opinion it's not good to give another meanings to enum values except what they mean by their names. Your enum defines polygons types, not numbers of points. I recommend to create dictionary:



          private static readonly Dictionary<PolygonType, int> NumberOfPoints =
          new Dictionary<PolygonType, int>
          {
          [PolygonType.Trigon] = 3,
          // ...
          };


          and use it:



          var numberOfPoints = NumberOfPoints[polygonType];


          Why are you using for polygon types such strange names and not Triangle and Square?



          If it's public API method, add arguments checking using appropriate exceptions (ArgumentOutOfRangeException, InvalidEnumArgumentException and so on). All possible exceptions should be documented via <exception> tags.






          share|improve this answer









          $endgroup$













          • $begingroup$
            Thank you for the feedback; the reason for context2D has been made redundant by making that particular parameter a field in the class, however since DeviceContext is the same in the 2D and 3D namespaces calling it context2D seems to be pretty common across the board. Casting to float was to prevent casting down the line as part of division; and in regards to the PolygonType names within the enum I used the mathematical names to add clarity since a square is not a diamond, but they are both tetragons.
            $endgroup$
            – PerpetualJ
            4 hours ago














          2












          2








          2





          $begingroup$

          The most confusing thing in your code is names of arguments and variables and their types.




          DeviceContext context2D



          Why not deviceContext?




          PolygonType polygon



          It's not polygon (which is collection of points as I expect), it's polygon type, so name it polygonType




          float numberOfPoints = (float)polygon;



          Why are you casting to float? Number of points can be non-integer? Also in my opinion it's not good to give another meanings to enum values except what they mean by their names. Your enum defines polygons types, not numbers of points. I recommend to create dictionary:



          private static readonly Dictionary<PolygonType, int> NumberOfPoints =
          new Dictionary<PolygonType, int>
          {
          [PolygonType.Trigon] = 3,
          // ...
          };


          and use it:



          var numberOfPoints = NumberOfPoints[polygonType];


          Why are you using for polygon types such strange names and not Triangle and Square?



          If it's public API method, add arguments checking using appropriate exceptions (ArgumentOutOfRangeException, InvalidEnumArgumentException and so on). All possible exceptions should be documented via <exception> tags.






          share|improve this answer









          $endgroup$



          The most confusing thing in your code is names of arguments and variables and their types.




          DeviceContext context2D



          Why not deviceContext?




          PolygonType polygon



          It's not polygon (which is collection of points as I expect), it's polygon type, so name it polygonType




          float numberOfPoints = (float)polygon;



          Why are you casting to float? Number of points can be non-integer? Also in my opinion it's not good to give another meanings to enum values except what they mean by their names. Your enum defines polygons types, not numbers of points. I recommend to create dictionary:



          private static readonly Dictionary<PolygonType, int> NumberOfPoints =
          new Dictionary<PolygonType, int>
          {
          [PolygonType.Trigon] = 3,
          // ...
          };


          and use it:



          var numberOfPoints = NumberOfPoints[polygonType];


          Why are you using for polygon types such strange names and not Triangle and Square?



          If it's public API method, add arguments checking using appropriate exceptions (ArgumentOutOfRangeException, InvalidEnumArgumentException and so on). All possible exceptions should be documented via <exception> tags.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 4 hours ago









          MaximMaxim

          2,402216




          2,402216












          • $begingroup$
            Thank you for the feedback; the reason for context2D has been made redundant by making that particular parameter a field in the class, however since DeviceContext is the same in the 2D and 3D namespaces calling it context2D seems to be pretty common across the board. Casting to float was to prevent casting down the line as part of division; and in regards to the PolygonType names within the enum I used the mathematical names to add clarity since a square is not a diamond, but they are both tetragons.
            $endgroup$
            – PerpetualJ
            4 hours ago


















          • $begingroup$
            Thank you for the feedback; the reason for context2D has been made redundant by making that particular parameter a field in the class, however since DeviceContext is the same in the 2D and 3D namespaces calling it context2D seems to be pretty common across the board. Casting to float was to prevent casting down the line as part of division; and in regards to the PolygonType names within the enum I used the mathematical names to add clarity since a square is not a diamond, but they are both tetragons.
            $endgroup$
            – PerpetualJ
            4 hours ago
















          $begingroup$
          Thank you for the feedback; the reason for context2D has been made redundant by making that particular parameter a field in the class, however since DeviceContext is the same in the 2D and 3D namespaces calling it context2D seems to be pretty common across the board. Casting to float was to prevent casting down the line as part of division; and in regards to the PolygonType names within the enum I used the mathematical names to add clarity since a square is not a diamond, but they are both tetragons.
          $endgroup$
          – PerpetualJ
          4 hours ago




          $begingroup$
          Thank you for the feedback; the reason for context2D has been made redundant by making that particular parameter a field in the class, however since DeviceContext is the same in the 2D and 3D namespaces calling it context2D seems to be pretty common across the board. Casting to float was to prevent casting down the line as part of division; and in regards to the PolygonType names within the enum I used the mathematical names to add clarity since a square is not a diamond, but they are both tetragons.
          $endgroup$
          – PerpetualJ
          4 hours ago


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Code Review Stack Exchange!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          Use MathJax to format equations. MathJax reference.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f211927%2frender-polygon-method-documentation%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          How to make a Squid Proxy server?

          第一次世界大戦

          Touch on Surface Book