当前位置: 首页 > 工具软件 > Arrow > 使用案例 >

draw arrow

丁恩
2023-12-01
public double getAngle(int Ax,int Ay,int Bx,int By,int Cx,int Cy){

        double lengthAB = Math.sqrt( Math.pow(Ax - Bx, 2) +
                Math.pow(Ay - By, 2)),
                lengthAC = Math.sqrt( Math.pow(Ax - Cx, 2) +
                        Math.pow(Ay - Cy, 2)),
                lengthBC = Math.sqrt( Math.pow(Bx - Cx, 2) +
                        Math.pow(By - Cy, 2));
        double cosA = (Math.pow(lengthAB, 2) + Math.pow(lengthAC, 2) - Math.pow(lengthBC, 2)) /
                (2 * lengthAB * lengthAC);
        double angleA = Math.round( Math.acos(cosA) * 180 / Math.PI );
        return  angleA;
    }
    public  float distance(float x, float y, float sx, float sy) {
        float dx = x - sx;
        float dy = y - sy;
        return (float) Math.sqrt(dx * dx + dy * dy);
    }
    private void drawArrow(Paint paint, Canvas canvas, float from_x, float from_y, float to_x, float to_y,float arrow_x,float arrow_y,float angle)
    {
        float anglerad, radius, lineangle;

        //values to change for other appearance *CHANGE THESE FOR OTHER SIZE ARROWHEADS*
        radius=distance(to_x,to_y,arrow_x,arrow_y);
        ;

        //some angle calculations
        anglerad= (float) (PI*angle*2/180.0f);
        lineangle= (float) (atan2(to_y-from_y,to_x-from_x));

        //tha line
        canvas.drawLine(from_x,from_y,to_x,to_y,paint);
//        canvas.drawLine(to_x,to_y,(float)(to_x-radius*cos(lineangle - (anglerad / 2.0))),
//                (float)(to_y-radius*sin(lineangle - (anglerad / 2.0))),paint);
//
//           canvas.drawLine(to_x,to_y,(float)(to_x-radius*cos(lineangle + (anglerad / 2.0))),
//                (float)(to_y-radius*sin(lineangle + (anglerad / 2.0))),paint);

        //tha triangle
        Path path = new Path();
//
        path.moveTo(to_x,to_y);
        path.lineTo((float)(to_x-radius*cos(lineangle - (anglerad / 2.0))),
                (float)(to_y-radius*sin(lineangle - (anglerad / 2.0))));
        path.moveTo(to_x,to_y);
        path.lineTo((float)(to_x-radius*cos(lineangle + (anglerad / 2.0))),
                (float)(to_y-radius*sin(lineangle + (anglerad / 2.0))));

        canvas.drawPath(path, paint);
    }
 类似资料:

相关阅读

相关文章

相关问答