0

我从数据库中得到了一些点(A,B),想要绘制这些点并将它们连接起来形成一个多边形类型的形状。基本上它将代表游泳池盖的形状。现在我编写了以下代码,但结果很奇怪.只有最后绘制的线存在,之前绘制的线消失了。我做错了什么吗?或者有更好的方法吗?

      private void calculationForPlot(){
     ArrayList<String> al_edit_A_ft = new ArrayList<String>();

     ArrayList<String> al_edit_A_inch=new ArrayList<String>();
     ArrayList<String>al_edit_B_ft=new ArrayList<String>(); 
     ArrayList<String>al_edit_B_inch= new ArrayList<String>();
    float x=0;
     AndroidOpenDbHelper androidOpenDbHelperObj = new AndroidOpenDbHelper(this);
        SQLiteDatabase sqliteDatabase = androidOpenDbHelperObj.getReadableDatabase();
        String q="SELECT * FROM ab_measurement WHERE job_id=" +"\""+Settings.jobid +"\"";


   Cursor  cursor = sqliteDatabase.rawQuery(q,null);

        if (cursor.moveToNext()) {
            System.out.println(cursor.getCount());
        int m=Integer.parseInt(cursor.getString(4));

            do {
                try{

                float a = 0,b = 0;



                 a=(float) Double.parseDouble(cursor.getString(6));
                 String number =String.valueOf(a);
                 System.out.println("aaa ggg"+number);
                 String int_part =number.substring(0,number.indexOf("."));
                 String float_part=number.substring(number.lastIndexOf(".")+1,number.length());
                 System.out.println("aaa values"+int_part);
                 System.out.println("aaa values"+float_part);


                  al_edit_A_ft.add(int_part);
                  al_edit_A_inch.add(float_part);

                     b= (float) Double.parseDouble(cursor.getString(7));
                     String number_b =String.valueOf(b);
                     System.out.println("aaa ggg"+number_b);
                     String int_part_b =number_b.substring(0,number_b.indexOf("."));
                     String float_part_b=number_b.substring(number_b.lastIndexOf(".")+1,number_b.length());
                     System.out.println("aaa values"+int_part_b);
                     System.out.println("aaa values"+float_part_b);
                     al_edit_B_ft.add(int_part_b);
                     al_edit_B_inch.add(float_part_b);

                     x= (float) Double.parseDouble(cursor.getString(3));
                     String ft_base =String.valueOf(x);
                     System.out.println("aaa ggg"+ft_base);
                     String int_part_ft =ft_base.substring(0,ft_base.indexOf("."));
                     String float_part_inch=ft_base.substring(ft_base.lastIndexOf(".")+1,ft_base.length());
                     System.out.println("aaa values"+int_part_ft);
                     System.out.println("aaa values"+float_part_inch);


                }
                catch (Exception e) {

                }

            } while (cursor.moveToNext());

    if(m>0){
        float pixelPerUnit=240/x;

        if(x>0){
            float X_prev=110;
            float Y_prev=250;
            float X = 0;
            float Y = 0;

            drawPlot(X_prev,Y_prev, X_prev+1, Y_prev+1);//origin
            for(int i=0;i</*obj.*/m;i++){
                String str_pointA=al_edit_A_ft.get(i).concat(".").concat(al_edit_A_inch.get(i));
                float pointA= (float) (Double.parseDouble(str_pointA)*pixelPerUnit);

                String str_pointB=al_edit_B_ft.get(i).concat(".").concat(al_edit_B_inch.get(i));
                float pointB=(float) (Double.parseDouble(str_pointB)*pixelPerUnit);

                float d = 240;
                if (pointB<1) {
                    X =  (110+(pointA/2));
                }
                else if (pointA<1){
                    Y = (110-(pointB/2));
                }else{
                     float a = (pointA * pointA - pointB * pointB + d * d ) / (2 * d);
                      float h = (float) Math.abs(Math.sqrt( pointA * pointA - a * a ));

                        if (h<0){
                            h=0;
                        }

                        X  = (110+(a/2)) ;
                        Y  =  (250-h);   
                      }

                System.out.println("X and Y"+"  "+X+"  "+Y);
                      drawPlot(X_prev+1, Y_prev_+1, X, Y);


                       X_prev=X;
                       Y_prev=Y;
                    }

            drawPlot(X_prev, Y_prev, 110, 250);//to complete the loop

            }

        }
    }
    }

        private void drawPlot(float startX,float startY,float endX,float endY) {
    System.out.println("Draw plot called");
     bitmap = Bitmap.createBitmap((int) getWindowManager()
                .getDefaultDisplay().getWidth(), (int) getWindowManager()
                .getDefaultDisplay().getHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmap);
            drawingImageView.setImageBitmap(bitmap);


            Paint paint = new Paint();
            paint.setColor(Color.BLUE);
            paint.setStrokeWidth(2);

            canvas.drawLine(startX, startY, endX, endY, paint);


}
4

1 回答 1

0

You can draw lines from point a to b using Bresenham's Line Algorithm --It's quite a simple algorithm. Here it is in Lua

function line(x0,y0,x1,y1)
    local plots={}
    local steep=math.abs(y1-y0)>math.abs(x1-x0)
    if steep then
        x0,y0=y0,x0
        x1,y1=y1,x1
    end
    if x0>x1 then
        x0,x1=x1,x0
        y0,y1=y1,y0
    end
    local dx,dy=x1-x0,math.abs(y1-y0)
    local error=dx/2
    local y=y0
    local sy=(y0<y1 and 1 or -1)
    for x=x0,x1 do
        if steep then
            table.insert(plots,{y,x})
        else
            table.insert(plots,{x,y})
        end
        error=error-dy
        if error<0 then
            y=y+sy
            error=error+dx
        end
    end
    return plots
end
于 2012-07-21T07:48:39.920 回答