Programmatically drawing a polygon using Vector












2














I'm doing a project in Java, using Android Studio, where I need to draw a polygon with the util vector. I need the user to insert the number of sides he wants for the polygon and then draw the polygon with the number on the input with the sides. I'd be very glad if someone could help me because I'm still starting on java.

This is my file Poligono.java:



import java.util.Vector;

public class Poligono extends Reta{

Vector<Ponto2D> pontos_poligono;
static int verifica_pontos=0;


public Poligono(Vector<Ponto2D> p5){

this.pontos_poligono=p5;
verifica_pontos=p5.size(); //Numero total de pontos no Vector (exemplo no array number=0 number=1 -> o size é 2)

}

public Double PerimetroPoligono(){

double perimetro=0;


for (int i=0; i < verifica_pontos ;i++){

Ponto2D pinicial = pontos_poligono.get(i);
Ponto2D pfinal = pontos_poligono.get(i+1);

perimetro+=pinicial.dist(pfinal);
}

return perimetro;

}

}


And this is Ponto2D.java:



public class Ponto2D {
int x, y;

public Ponto2D() {
this.x = 0;
this.y = 0;
}

public Ponto2D(int a, int b) {
x = a;
y = b;
}
}


And Reta.java:



public class Reta {
Ponto2D pinicial;
Ponto2D pfinal;

public Reta(){
pinicial = new Ponto2D();
pfinal = new Ponto2D();
}


public Reta(Ponto2D a, Ponto2D b){
pinicial = a;
pfinal = b;
}
}









share|improve this question





























    2














    I'm doing a project in Java, using Android Studio, where I need to draw a polygon with the util vector. I need the user to insert the number of sides he wants for the polygon and then draw the polygon with the number on the input with the sides. I'd be very glad if someone could help me because I'm still starting on java.

    This is my file Poligono.java:



    import java.util.Vector;

    public class Poligono extends Reta{

    Vector<Ponto2D> pontos_poligono;
    static int verifica_pontos=0;


    public Poligono(Vector<Ponto2D> p5){

    this.pontos_poligono=p5;
    verifica_pontos=p5.size(); //Numero total de pontos no Vector (exemplo no array number=0 number=1 -> o size é 2)

    }

    public Double PerimetroPoligono(){

    double perimetro=0;


    for (int i=0; i < verifica_pontos ;i++){

    Ponto2D pinicial = pontos_poligono.get(i);
    Ponto2D pfinal = pontos_poligono.get(i+1);

    perimetro+=pinicial.dist(pfinal);
    }

    return perimetro;

    }

    }


    And this is Ponto2D.java:



    public class Ponto2D {
    int x, y;

    public Ponto2D() {
    this.x = 0;
    this.y = 0;
    }

    public Ponto2D(int a, int b) {
    x = a;
    y = b;
    }
    }


    And Reta.java:



    public class Reta {
    Ponto2D pinicial;
    Ponto2D pfinal;

    public Reta(){
    pinicial = new Ponto2D();
    pfinal = new Ponto2D();
    }


    public Reta(Ponto2D a, Ponto2D b){
    pinicial = a;
    pfinal = b;
    }
    }









    share|improve this question



























      2












      2








      2







      I'm doing a project in Java, using Android Studio, where I need to draw a polygon with the util vector. I need the user to insert the number of sides he wants for the polygon and then draw the polygon with the number on the input with the sides. I'd be very glad if someone could help me because I'm still starting on java.

      This is my file Poligono.java:



      import java.util.Vector;

      public class Poligono extends Reta{

      Vector<Ponto2D> pontos_poligono;
      static int verifica_pontos=0;


      public Poligono(Vector<Ponto2D> p5){

      this.pontos_poligono=p5;
      verifica_pontos=p5.size(); //Numero total de pontos no Vector (exemplo no array number=0 number=1 -> o size é 2)

      }

      public Double PerimetroPoligono(){

      double perimetro=0;


      for (int i=0; i < verifica_pontos ;i++){

      Ponto2D pinicial = pontos_poligono.get(i);
      Ponto2D pfinal = pontos_poligono.get(i+1);

      perimetro+=pinicial.dist(pfinal);
      }

      return perimetro;

      }

      }


      And this is Ponto2D.java:



      public class Ponto2D {
      int x, y;

      public Ponto2D() {
      this.x = 0;
      this.y = 0;
      }

      public Ponto2D(int a, int b) {
      x = a;
      y = b;
      }
      }


      And Reta.java:



      public class Reta {
      Ponto2D pinicial;
      Ponto2D pfinal;

      public Reta(){
      pinicial = new Ponto2D();
      pfinal = new Ponto2D();
      }


      public Reta(Ponto2D a, Ponto2D b){
      pinicial = a;
      pfinal = b;
      }
      }









      share|improve this question















      I'm doing a project in Java, using Android Studio, where I need to draw a polygon with the util vector. I need the user to insert the number of sides he wants for the polygon and then draw the polygon with the number on the input with the sides. I'd be very glad if someone could help me because I'm still starting on java.

      This is my file Poligono.java:



      import java.util.Vector;

      public class Poligono extends Reta{

      Vector<Ponto2D> pontos_poligono;
      static int verifica_pontos=0;


      public Poligono(Vector<Ponto2D> p5){

      this.pontos_poligono=p5;
      verifica_pontos=p5.size(); //Numero total de pontos no Vector (exemplo no array number=0 number=1 -> o size é 2)

      }

      public Double PerimetroPoligono(){

      double perimetro=0;


      for (int i=0; i < verifica_pontos ;i++){

      Ponto2D pinicial = pontos_poligono.get(i);
      Ponto2D pfinal = pontos_poligono.get(i+1);

      perimetro+=pinicial.dist(pfinal);
      }

      return perimetro;

      }

      }


      And this is Ponto2D.java:



      public class Ponto2D {
      int x, y;

      public Ponto2D() {
      this.x = 0;
      this.y = 0;
      }

      public Ponto2D(int a, int b) {
      x = a;
      y = b;
      }
      }


      And Reta.java:



      public class Reta {
      Ponto2D pinicial;
      Ponto2D pfinal;

      public Reta(){
      pinicial = new Ponto2D();
      pfinal = new Ponto2D();
      }


      public Reta(Ponto2D a, Ponto2D b){
      pinicial = a;
      pfinal = b;
      }
      }






      java android vector draw






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 17 '18 at 19:03









      Fantômas

      32.3k156388




      32.3k156388










      asked Nov 17 '18 at 17:16









      soalribeiro

      19312




      19312
























          1 Answer
          1






          active

          oldest

          votes


















          1














          Your post it's not 100% clear. First of all, you can't "draw" a polygon with vector (that is a way to store data). You have to use some Graphics library, create a window, create a canvas and so on...
          If you want, instead, to conceptually represent the polygon, here my opinion.
          Supposing that you want a regolar polygon, we can use sin ad cos to calculate the vertices position (see this).
          You can create a Vector that stores all the polygon vertices. I suggest to extend Ponto2D, so you can store in x and y the center coordinates.



          import java.util.Vector;

          public class Poligono extends Ponto2D{

          private Vector<Ponto2D> vertices;
          private int num_vertices

          public Poligono(int num_vertices){
          super(); //If you use a center != (0,0) you have
          //to add the center coordinates to all the vertices

          //You also should add a radius (distance from
          //the center to all vertices);
          this.num_vertices=num_vertices;
          this.radius=radius;
          vertices=new Vector<>();
          fillVector();
          }


          //This method calculate all the vertices position and
          //store it in the Vector
          private void fillVector(){
          for(int i=0; i<num_vertices;i++){
          //use sin and cos to calculate vertices
          for (int i = 0; i < sides; ++i) {
          x = (int) (Math.cos(theta * i));
          y = (int) (Math.sin(theta * i));
          vertices.add(new Ponto2D(x,y));
          }
          }
          }


          public Double PerimetroPoligono(){

          //take every point of the vector, and add the distances between them.

          }
          }





          share|improve this answer





















          • Hello, this is my trouble more specific stackoverflow.com/questions/53357206/…
            – soalribeiro
            Nov 18 '18 at 10:53











          Your Answer






          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: "1"
          };
          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: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          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%2fstackoverflow.com%2fquestions%2f53353599%2fprogrammatically-drawing-a-polygon-using-vector%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









          1














          Your post it's not 100% clear. First of all, you can't "draw" a polygon with vector (that is a way to store data). You have to use some Graphics library, create a window, create a canvas and so on...
          If you want, instead, to conceptually represent the polygon, here my opinion.
          Supposing that you want a regolar polygon, we can use sin ad cos to calculate the vertices position (see this).
          You can create a Vector that stores all the polygon vertices. I suggest to extend Ponto2D, so you can store in x and y the center coordinates.



          import java.util.Vector;

          public class Poligono extends Ponto2D{

          private Vector<Ponto2D> vertices;
          private int num_vertices

          public Poligono(int num_vertices){
          super(); //If you use a center != (0,0) you have
          //to add the center coordinates to all the vertices

          //You also should add a radius (distance from
          //the center to all vertices);
          this.num_vertices=num_vertices;
          this.radius=radius;
          vertices=new Vector<>();
          fillVector();
          }


          //This method calculate all the vertices position and
          //store it in the Vector
          private void fillVector(){
          for(int i=0; i<num_vertices;i++){
          //use sin and cos to calculate vertices
          for (int i = 0; i < sides; ++i) {
          x = (int) (Math.cos(theta * i));
          y = (int) (Math.sin(theta * i));
          vertices.add(new Ponto2D(x,y));
          }
          }
          }


          public Double PerimetroPoligono(){

          //take every point of the vector, and add the distances between them.

          }
          }





          share|improve this answer





















          • Hello, this is my trouble more specific stackoverflow.com/questions/53357206/…
            – soalribeiro
            Nov 18 '18 at 10:53
















          1














          Your post it's not 100% clear. First of all, you can't "draw" a polygon with vector (that is a way to store data). You have to use some Graphics library, create a window, create a canvas and so on...
          If you want, instead, to conceptually represent the polygon, here my opinion.
          Supposing that you want a regolar polygon, we can use sin ad cos to calculate the vertices position (see this).
          You can create a Vector that stores all the polygon vertices. I suggest to extend Ponto2D, so you can store in x and y the center coordinates.



          import java.util.Vector;

          public class Poligono extends Ponto2D{

          private Vector<Ponto2D> vertices;
          private int num_vertices

          public Poligono(int num_vertices){
          super(); //If you use a center != (0,0) you have
          //to add the center coordinates to all the vertices

          //You also should add a radius (distance from
          //the center to all vertices);
          this.num_vertices=num_vertices;
          this.radius=radius;
          vertices=new Vector<>();
          fillVector();
          }


          //This method calculate all the vertices position and
          //store it in the Vector
          private void fillVector(){
          for(int i=0; i<num_vertices;i++){
          //use sin and cos to calculate vertices
          for (int i = 0; i < sides; ++i) {
          x = (int) (Math.cos(theta * i));
          y = (int) (Math.sin(theta * i));
          vertices.add(new Ponto2D(x,y));
          }
          }
          }


          public Double PerimetroPoligono(){

          //take every point of the vector, and add the distances between them.

          }
          }





          share|improve this answer





















          • Hello, this is my trouble more specific stackoverflow.com/questions/53357206/…
            – soalribeiro
            Nov 18 '18 at 10:53














          1












          1








          1






          Your post it's not 100% clear. First of all, you can't "draw" a polygon with vector (that is a way to store data). You have to use some Graphics library, create a window, create a canvas and so on...
          If you want, instead, to conceptually represent the polygon, here my opinion.
          Supposing that you want a regolar polygon, we can use sin ad cos to calculate the vertices position (see this).
          You can create a Vector that stores all the polygon vertices. I suggest to extend Ponto2D, so you can store in x and y the center coordinates.



          import java.util.Vector;

          public class Poligono extends Ponto2D{

          private Vector<Ponto2D> vertices;
          private int num_vertices

          public Poligono(int num_vertices){
          super(); //If you use a center != (0,0) you have
          //to add the center coordinates to all the vertices

          //You also should add a radius (distance from
          //the center to all vertices);
          this.num_vertices=num_vertices;
          this.radius=radius;
          vertices=new Vector<>();
          fillVector();
          }


          //This method calculate all the vertices position and
          //store it in the Vector
          private void fillVector(){
          for(int i=0; i<num_vertices;i++){
          //use sin and cos to calculate vertices
          for (int i = 0; i < sides; ++i) {
          x = (int) (Math.cos(theta * i));
          y = (int) (Math.sin(theta * i));
          vertices.add(new Ponto2D(x,y));
          }
          }
          }


          public Double PerimetroPoligono(){

          //take every point of the vector, and add the distances between them.

          }
          }





          share|improve this answer












          Your post it's not 100% clear. First of all, you can't "draw" a polygon with vector (that is a way to store data). You have to use some Graphics library, create a window, create a canvas and so on...
          If you want, instead, to conceptually represent the polygon, here my opinion.
          Supposing that you want a regolar polygon, we can use sin ad cos to calculate the vertices position (see this).
          You can create a Vector that stores all the polygon vertices. I suggest to extend Ponto2D, so you can store in x and y the center coordinates.



          import java.util.Vector;

          public class Poligono extends Ponto2D{

          private Vector<Ponto2D> vertices;
          private int num_vertices

          public Poligono(int num_vertices){
          super(); //If you use a center != (0,0) you have
          //to add the center coordinates to all the vertices

          //You also should add a radius (distance from
          //the center to all vertices);
          this.num_vertices=num_vertices;
          this.radius=radius;
          vertices=new Vector<>();
          fillVector();
          }


          //This method calculate all the vertices position and
          //store it in the Vector
          private void fillVector(){
          for(int i=0; i<num_vertices;i++){
          //use sin and cos to calculate vertices
          for (int i = 0; i < sides; ++i) {
          x = (int) (Math.cos(theta * i));
          y = (int) (Math.sin(theta * i));
          vertices.add(new Ponto2D(x,y));
          }
          }
          }


          public Double PerimetroPoligono(){

          //take every point of the vector, and add the distances between them.

          }
          }






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 18 '18 at 9:12









          Calaf

          8611




          8611












          • Hello, this is my trouble more specific stackoverflow.com/questions/53357206/…
            – soalribeiro
            Nov 18 '18 at 10:53


















          • Hello, this is my trouble more specific stackoverflow.com/questions/53357206/…
            – soalribeiro
            Nov 18 '18 at 10:53
















          Hello, this is my trouble more specific stackoverflow.com/questions/53357206/…
          – soalribeiro
          Nov 18 '18 at 10:53




          Hello, this is my trouble more specific stackoverflow.com/questions/53357206/…
          – soalribeiro
          Nov 18 '18 at 10:53


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • 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.


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





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • 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.


          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%2fstackoverflow.com%2fquestions%2f53353599%2fprogrammatically-drawing-a-polygon-using-vector%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

          Biblatex bibliography style without URLs when DOI exists (in Overleaf with Zotero bibliography)

          ComboBox Display Member on multiple fields

          Is it possible to collect Nectar points via Trainline?