soalrib
soalrib

Reputation: 599

Programmatically drawing a polygon using Vector

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

Upvotes: 2

Views: 577

Answers (1)

Calaf
Calaf

Reputation: 1173

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.

    }
}

Upvotes: 1

Related Questions