【问题标题】:Programmatically drawing a polygon using Vector使用 Vector 以编程方式绘制多边形
【发布时间】:2018-11-17 17:16:20
【问题描述】:

我正在使用 Android Studio 用 Ja​​va 做一个项目,我需要在其中使用 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;
    }
}

【问题讨论】:

    标签: java android vector draw


    【解决方案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.
    
        }
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多