Drawing consecutive rectangles with an algorithm












2















I have an assignment to draw rectangles that get larger and larger each time I draw them. Just like shown below. I've labeled them TL = Top Left, TR = Top Right, BL = Bottom Left, BR = Bottom Right. The smallest boxes are 8 pixels by 8 pixels. From this drawing I pulled these algorithms for each box. Where n represents the level you are drawing. Level 0 is the inside set of four and grows out by 1.



TL = (x-n(8), y - n(8))
TR = ((x + 8) - (n*8), y - n(8))
BL = (x - n(8), y - (n+1)(8))
BR = ((x+8)+(n*8), (y+8)-(n*8));


My real questions is this. I feel like I can combine these algorithms into something smaller but i'm new to this type of thinking. This is the first Computer Science Class I've taken that required me to do this type of thinking. Do you have any suggestions?





From these algorithms I wrote this code in Java.



    public static void drawRectangle(Graphics g, int xCenter, int yCenter, int levelsNeeded){
//all squres are based off an 8 pixel system. I use this to shift things around ALOT.
final int CHANGECONSTANT = 8;
int width = 8;
int height = 8;
int dcg = 1; //Dimension Change Factor

Point origin = new Point(xCenter -= CHANGECONSTANT, yCenter -= CHANGECONSTANT); //Sets Origin to top left of inner most set of squares

for (int level = 0; level < levelsNeeded; level++)
{
g.setColor(Color.GRAY);
//Top Left
g.fillRect(
(int)origin.getX()-(level*CHANGECONSTANT), // XCord
(int)origin.getY()-(level*CHANGECONSTANT), // YCord
width, //width. This stays at 8 for all iterations
height*dcg);//height. This gets multiplied by odd factors each time
//Draw outline
g.setColor(Color.BLACK);
g.drawRect(
(int)origin.getX()-(level*CHANGECONSTANT), // XCord
(int)origin.getY()-(level*CHANGECONSTANT), // YCord
width, //width. This stays at 8 for all iterations
height*dcg);//height. This gets multiplied by odd factors each time

g.setColor(Color.GRAY);
//Bottom Left
g.fillRect(
(int)origin.getX()-(level*CHANGECONSTANT), //XCrod
(int)origin.getY()+((level+1)*CHANGECONSTANT), //YCord
width*dcg, //width. This gets multiplied by odd factors each time
height); //height. This stays at 8 for all iterations
g.setColor(Color.BLACK);
//Draw Outline
g.drawRect(
(int)origin.getX()-(level*CHANGECONSTANT), //XCrod
(int)origin.getY()+((level+1)*CHANGECONSTANT), //YCord
width*dcg, //width. This gets multiplied by odd factors each time
height); //height. This stays at 8 for all iterations


g.setColor(Color.WHITE);
//Top Right
g.fillRect(
((int)origin.getX()+CHANGECONSTANT)-(level*CHANGECONSTANT), //XCord
(int)origin.getY()-(level*CHANGECONSTANT), //YCord
width*dcg,//width. This gets multiplied by odd factors each time
height);//height. This stays at 8 for all iterations
//Draw Outline
g.setColor(Color.BLACK);
g.drawRect(
((int)origin.getX()+CHANGECONSTANT)-(level*CHANGECONSTANT), //XCord
(int)origin.getY()-(level*CHANGECONSTANT), //YCord
width*dcg,//width. This gets multiplied by odd factors each time
height);//height. This stays at 8 for all iterations

g.setColor(Color.WHITE);
//Bottom Right
g.fillRect(
((int)origin.getX()+CHANGECONSTANT)+(level*CHANGECONSTANT), //Xcord
((int)origin.getY()+CHANGECONSTANT)-(level*CHANGECONSTANT), //Ycord
width,//width. This gets multiplied by odd factors each time
height*dcg);//Height. This stays at 8 for all iterations.
//Draw Outline
g.setColor(Color.BLACK);
g.drawRect(
((int)origin.getX()+CHANGECONSTANT)+(level*CHANGECONSTANT), //Xcord
((int)origin.getY()+CHANGECONSTANT)-(level*CHANGECONSTANT), //Ycord
width,//width. This gets multiplied by odd factors each time
height*dcg);//Height. This stays at 8 for all iterations.

dcg += 2; //Increment DCG by two at end of each loop

}
}


Here is what my final product looks like right now. Disregard the triangle shapes that a different method that calls the first one multiple times.





This is my first quarter learning Java and i'm new to programming.










share|improve this question









New contributor




Luke Kelly is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

























    2















    I have an assignment to draw rectangles that get larger and larger each time I draw them. Just like shown below. I've labeled them TL = Top Left, TR = Top Right, BL = Bottom Left, BR = Bottom Right. The smallest boxes are 8 pixels by 8 pixels. From this drawing I pulled these algorithms for each box. Where n represents the level you are drawing. Level 0 is the inside set of four and grows out by 1.



    TL = (x-n(8), y - n(8))
    TR = ((x + 8) - (n*8), y - n(8))
    BL = (x - n(8), y - (n+1)(8))
    BR = ((x+8)+(n*8), (y+8)-(n*8));


    My real questions is this. I feel like I can combine these algorithms into something smaller but i'm new to this type of thinking. This is the first Computer Science Class I've taken that required me to do this type of thinking. Do you have any suggestions?





    From these algorithms I wrote this code in Java.



        public static void drawRectangle(Graphics g, int xCenter, int yCenter, int levelsNeeded){
    //all squres are based off an 8 pixel system. I use this to shift things around ALOT.
    final int CHANGECONSTANT = 8;
    int width = 8;
    int height = 8;
    int dcg = 1; //Dimension Change Factor

    Point origin = new Point(xCenter -= CHANGECONSTANT, yCenter -= CHANGECONSTANT); //Sets Origin to top left of inner most set of squares

    for (int level = 0; level < levelsNeeded; level++)
    {
    g.setColor(Color.GRAY);
    //Top Left
    g.fillRect(
    (int)origin.getX()-(level*CHANGECONSTANT), // XCord
    (int)origin.getY()-(level*CHANGECONSTANT), // YCord
    width, //width. This stays at 8 for all iterations
    height*dcg);//height. This gets multiplied by odd factors each time
    //Draw outline
    g.setColor(Color.BLACK);
    g.drawRect(
    (int)origin.getX()-(level*CHANGECONSTANT), // XCord
    (int)origin.getY()-(level*CHANGECONSTANT), // YCord
    width, //width. This stays at 8 for all iterations
    height*dcg);//height. This gets multiplied by odd factors each time

    g.setColor(Color.GRAY);
    //Bottom Left
    g.fillRect(
    (int)origin.getX()-(level*CHANGECONSTANT), //XCrod
    (int)origin.getY()+((level+1)*CHANGECONSTANT), //YCord
    width*dcg, //width. This gets multiplied by odd factors each time
    height); //height. This stays at 8 for all iterations
    g.setColor(Color.BLACK);
    //Draw Outline
    g.drawRect(
    (int)origin.getX()-(level*CHANGECONSTANT), //XCrod
    (int)origin.getY()+((level+1)*CHANGECONSTANT), //YCord
    width*dcg, //width. This gets multiplied by odd factors each time
    height); //height. This stays at 8 for all iterations


    g.setColor(Color.WHITE);
    //Top Right
    g.fillRect(
    ((int)origin.getX()+CHANGECONSTANT)-(level*CHANGECONSTANT), //XCord
    (int)origin.getY()-(level*CHANGECONSTANT), //YCord
    width*dcg,//width. This gets multiplied by odd factors each time
    height);//height. This stays at 8 for all iterations
    //Draw Outline
    g.setColor(Color.BLACK);
    g.drawRect(
    ((int)origin.getX()+CHANGECONSTANT)-(level*CHANGECONSTANT), //XCord
    (int)origin.getY()-(level*CHANGECONSTANT), //YCord
    width*dcg,//width. This gets multiplied by odd factors each time
    height);//height. This stays at 8 for all iterations

    g.setColor(Color.WHITE);
    //Bottom Right
    g.fillRect(
    ((int)origin.getX()+CHANGECONSTANT)+(level*CHANGECONSTANT), //Xcord
    ((int)origin.getY()+CHANGECONSTANT)-(level*CHANGECONSTANT), //Ycord
    width,//width. This gets multiplied by odd factors each time
    height*dcg);//Height. This stays at 8 for all iterations.
    //Draw Outline
    g.setColor(Color.BLACK);
    g.drawRect(
    ((int)origin.getX()+CHANGECONSTANT)+(level*CHANGECONSTANT), //Xcord
    ((int)origin.getY()+CHANGECONSTANT)-(level*CHANGECONSTANT), //Ycord
    width,//width. This gets multiplied by odd factors each time
    height*dcg);//Height. This stays at 8 for all iterations.

    dcg += 2; //Increment DCG by two at end of each loop

    }
    }


    Here is what my final product looks like right now. Disregard the triangle shapes that a different method that calls the first one multiple times.





    This is my first quarter learning Java and i'm new to programming.










    share|improve this question









    New contributor




    Luke Kelly is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.























      2












      2








      2








      I have an assignment to draw rectangles that get larger and larger each time I draw them. Just like shown below. I've labeled them TL = Top Left, TR = Top Right, BL = Bottom Left, BR = Bottom Right. The smallest boxes are 8 pixels by 8 pixels. From this drawing I pulled these algorithms for each box. Where n represents the level you are drawing. Level 0 is the inside set of four and grows out by 1.



      TL = (x-n(8), y - n(8))
      TR = ((x + 8) - (n*8), y - n(8))
      BL = (x - n(8), y - (n+1)(8))
      BR = ((x+8)+(n*8), (y+8)-(n*8));


      My real questions is this. I feel like I can combine these algorithms into something smaller but i'm new to this type of thinking. This is the first Computer Science Class I've taken that required me to do this type of thinking. Do you have any suggestions?





      From these algorithms I wrote this code in Java.



          public static void drawRectangle(Graphics g, int xCenter, int yCenter, int levelsNeeded){
      //all squres are based off an 8 pixel system. I use this to shift things around ALOT.
      final int CHANGECONSTANT = 8;
      int width = 8;
      int height = 8;
      int dcg = 1; //Dimension Change Factor

      Point origin = new Point(xCenter -= CHANGECONSTANT, yCenter -= CHANGECONSTANT); //Sets Origin to top left of inner most set of squares

      for (int level = 0; level < levelsNeeded; level++)
      {
      g.setColor(Color.GRAY);
      //Top Left
      g.fillRect(
      (int)origin.getX()-(level*CHANGECONSTANT), // XCord
      (int)origin.getY()-(level*CHANGECONSTANT), // YCord
      width, //width. This stays at 8 for all iterations
      height*dcg);//height. This gets multiplied by odd factors each time
      //Draw outline
      g.setColor(Color.BLACK);
      g.drawRect(
      (int)origin.getX()-(level*CHANGECONSTANT), // XCord
      (int)origin.getY()-(level*CHANGECONSTANT), // YCord
      width, //width. This stays at 8 for all iterations
      height*dcg);//height. This gets multiplied by odd factors each time

      g.setColor(Color.GRAY);
      //Bottom Left
      g.fillRect(
      (int)origin.getX()-(level*CHANGECONSTANT), //XCrod
      (int)origin.getY()+((level+1)*CHANGECONSTANT), //YCord
      width*dcg, //width. This gets multiplied by odd factors each time
      height); //height. This stays at 8 for all iterations
      g.setColor(Color.BLACK);
      //Draw Outline
      g.drawRect(
      (int)origin.getX()-(level*CHANGECONSTANT), //XCrod
      (int)origin.getY()+((level+1)*CHANGECONSTANT), //YCord
      width*dcg, //width. This gets multiplied by odd factors each time
      height); //height. This stays at 8 for all iterations


      g.setColor(Color.WHITE);
      //Top Right
      g.fillRect(
      ((int)origin.getX()+CHANGECONSTANT)-(level*CHANGECONSTANT), //XCord
      (int)origin.getY()-(level*CHANGECONSTANT), //YCord
      width*dcg,//width. This gets multiplied by odd factors each time
      height);//height. This stays at 8 for all iterations
      //Draw Outline
      g.setColor(Color.BLACK);
      g.drawRect(
      ((int)origin.getX()+CHANGECONSTANT)-(level*CHANGECONSTANT), //XCord
      (int)origin.getY()-(level*CHANGECONSTANT), //YCord
      width*dcg,//width. This gets multiplied by odd factors each time
      height);//height. This stays at 8 for all iterations

      g.setColor(Color.WHITE);
      //Bottom Right
      g.fillRect(
      ((int)origin.getX()+CHANGECONSTANT)+(level*CHANGECONSTANT), //Xcord
      ((int)origin.getY()+CHANGECONSTANT)-(level*CHANGECONSTANT), //Ycord
      width,//width. This gets multiplied by odd factors each time
      height*dcg);//Height. This stays at 8 for all iterations.
      //Draw Outline
      g.setColor(Color.BLACK);
      g.drawRect(
      ((int)origin.getX()+CHANGECONSTANT)+(level*CHANGECONSTANT), //Xcord
      ((int)origin.getY()+CHANGECONSTANT)-(level*CHANGECONSTANT), //Ycord
      width,//width. This gets multiplied by odd factors each time
      height*dcg);//Height. This stays at 8 for all iterations.

      dcg += 2; //Increment DCG by two at end of each loop

      }
      }


      Here is what my final product looks like right now. Disregard the triangle shapes that a different method that calls the first one multiple times.





      This is my first quarter learning Java and i'm new to programming.










      share|improve this question









      New contributor




      Luke Kelly is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.












      I have an assignment to draw rectangles that get larger and larger each time I draw them. Just like shown below. I've labeled them TL = Top Left, TR = Top Right, BL = Bottom Left, BR = Bottom Right. The smallest boxes are 8 pixels by 8 pixels. From this drawing I pulled these algorithms for each box. Where n represents the level you are drawing. Level 0 is the inside set of four and grows out by 1.



      TL = (x-n(8), y - n(8))
      TR = ((x + 8) - (n*8), y - n(8))
      BL = (x - n(8), y - (n+1)(8))
      BR = ((x+8)+(n*8), (y+8)-(n*8));


      My real questions is this. I feel like I can combine these algorithms into something smaller but i'm new to this type of thinking. This is the first Computer Science Class I've taken that required me to do this type of thinking. Do you have any suggestions?





      From these algorithms I wrote this code in Java.



          public static void drawRectangle(Graphics g, int xCenter, int yCenter, int levelsNeeded){
      //all squres are based off an 8 pixel system. I use this to shift things around ALOT.
      final int CHANGECONSTANT = 8;
      int width = 8;
      int height = 8;
      int dcg = 1; //Dimension Change Factor

      Point origin = new Point(xCenter -= CHANGECONSTANT, yCenter -= CHANGECONSTANT); //Sets Origin to top left of inner most set of squares

      for (int level = 0; level < levelsNeeded; level++)
      {
      g.setColor(Color.GRAY);
      //Top Left
      g.fillRect(
      (int)origin.getX()-(level*CHANGECONSTANT), // XCord
      (int)origin.getY()-(level*CHANGECONSTANT), // YCord
      width, //width. This stays at 8 for all iterations
      height*dcg);//height. This gets multiplied by odd factors each time
      //Draw outline
      g.setColor(Color.BLACK);
      g.drawRect(
      (int)origin.getX()-(level*CHANGECONSTANT), // XCord
      (int)origin.getY()-(level*CHANGECONSTANT), // YCord
      width, //width. This stays at 8 for all iterations
      height*dcg);//height. This gets multiplied by odd factors each time

      g.setColor(Color.GRAY);
      //Bottom Left
      g.fillRect(
      (int)origin.getX()-(level*CHANGECONSTANT), //XCrod
      (int)origin.getY()+((level+1)*CHANGECONSTANT), //YCord
      width*dcg, //width. This gets multiplied by odd factors each time
      height); //height. This stays at 8 for all iterations
      g.setColor(Color.BLACK);
      //Draw Outline
      g.drawRect(
      (int)origin.getX()-(level*CHANGECONSTANT), //XCrod
      (int)origin.getY()+((level+1)*CHANGECONSTANT), //YCord
      width*dcg, //width. This gets multiplied by odd factors each time
      height); //height. This stays at 8 for all iterations


      g.setColor(Color.WHITE);
      //Top Right
      g.fillRect(
      ((int)origin.getX()+CHANGECONSTANT)-(level*CHANGECONSTANT), //XCord
      (int)origin.getY()-(level*CHANGECONSTANT), //YCord
      width*dcg,//width. This gets multiplied by odd factors each time
      height);//height. This stays at 8 for all iterations
      //Draw Outline
      g.setColor(Color.BLACK);
      g.drawRect(
      ((int)origin.getX()+CHANGECONSTANT)-(level*CHANGECONSTANT), //XCord
      (int)origin.getY()-(level*CHANGECONSTANT), //YCord
      width*dcg,//width. This gets multiplied by odd factors each time
      height);//height. This stays at 8 for all iterations

      g.setColor(Color.WHITE);
      //Bottom Right
      g.fillRect(
      ((int)origin.getX()+CHANGECONSTANT)+(level*CHANGECONSTANT), //Xcord
      ((int)origin.getY()+CHANGECONSTANT)-(level*CHANGECONSTANT), //Ycord
      width,//width. This gets multiplied by odd factors each time
      height*dcg);//Height. This stays at 8 for all iterations.
      //Draw Outline
      g.setColor(Color.BLACK);
      g.drawRect(
      ((int)origin.getX()+CHANGECONSTANT)+(level*CHANGECONSTANT), //Xcord
      ((int)origin.getY()+CHANGECONSTANT)-(level*CHANGECONSTANT), //Ycord
      width,//width. This gets multiplied by odd factors each time
      height*dcg);//Height. This stays at 8 for all iterations.

      dcg += 2; //Increment DCG by two at end of each loop

      }
      }


      Here is what my final product looks like right now. Disregard the triangle shapes that a different method that calls the first one multiple times.





      This is my first quarter learning Java and i'm new to programming.







      java beginner algorithm homework graphics






      share|improve this question









      New contributor




      Luke Kelly is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      Luke Kelly is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited 5 hours ago









      200_success

      129k15152414




      129k15152414






      New contributor




      Luke Kelly is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked 5 hours ago









      Luke KellyLuke Kelly

      111




      111




      New contributor




      Luke Kelly is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Luke Kelly is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Luke Kelly is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






















          0






          active

          oldest

          votes











          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
          });


          }
          });






          Luke Kelly is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f211386%2fdrawing-consecutive-rectangles-with-an-algorithm%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          Luke Kelly is a new contributor. Be nice, and check out our Code of Conduct.










          draft saved

          draft discarded


















          Luke Kelly is a new contributor. Be nice, and check out our Code of Conduct.













          Luke Kelly is a new contributor. Be nice, and check out our Code of Conduct.












          Luke Kelly is a new contributor. Be nice, and check out our Code of Conduct.
















          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%2f211386%2fdrawing-consecutive-rectangles-with-an-algorithm%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