使用Vector

时间:2018-11-17 17:16:21

标签: java android vector draw

我正在使用Java Studio在Java中做一个项目,在这里我需要使用util矢量绘制一个多边形。我需要用户为多边形插入所需的边数,然后在具有边数的输入上绘制多边形。如果有人可以帮助我,我将感到非常高兴,因为我仍在学习Java。
这是我的文件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;

    }

}

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

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

1 个答案:

答案 0 :(得分:1)

您的帖子还不是100%清晰。首先,您不能用矢量“绘制”多边形(这是一种存储数据的方式)。您必须使用一些图形库,创建一个窗口,创建一个画布等等。 相反,如果您想在概念上表示多边形,在这里我认为。 假设您想要一个regolar多边形,我们可以使用sin ad cos计算顶点位置(see this)。 您可以创建一个存储所有多边形顶点的向量。我建议扩展Ponto2D,以便可以在x和y中存储中心坐标。

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.

    }
}