canvas 输入两个点,半径h画弧度 弧的大小随半径h变化

在这里插入图片描述

  public void a(Point p1, Point p2, int x, Canvas canvas) {

        if (x == 0) {
            canvas.drawLine(p1.x, p1.y, p2.x, p2.y, mPaint);
            return;
        }

//        if (p2.x - p1.x > 0 && p2.y - p1.y < 0 || p2.x - p1.x < 0 && p2.y - p1.y > 0)
        x = x * -1;

        if (p2.x - p1.x >= 0) x = x * -1;

        float rr = (float) Math.sqrt((float) Math.pow((p2.y - p1.y), 2) + (float) Math.pow((p2.x - p1.x), 2)) / 2;

        float h = x > 0 ? rr - rr / 20 * x : rr / 20 * x;




        float cx = p1.x + (p2.x - p1.x) / 2;
        float cy = p1.y + (p2.y - p1.y) / 2;

        Point op1 = new Point();
        op1.x = (int) (cx - rr);
        op1.y = (int) (cy - rr);

        Point op2 = new Point();
        op2.x = (int) (cx + rr);
        op2.y = (int) (cy + rr);


        float k = (float) Math.toDegrees(Math.atan((float) (p2.y - p1.y) / (p2.x - p1.x)));
        canvas.rotate(k, cx, cy);
        //RectF oval = new RectF( op1.x, op1.y,
        //op2.x, op2.y);
        //canvas.drawRect(oval,mPaint);

        float dR = (float) Math.pow(rr, 2);
        float dH = (float) Math.pow(h, 2);
        float n = h >= 0 ? (dR + dH / 2 - rr * h) / (rr - h) : -(dR + dH) / (2 * h);


        float v = (float) Math.toDegrees(Math.acos(rr / n));
        if (h >= 0) {
            RectF oval1 = new RectF(op1.x + rr - n, op1.y + h,
                    n - rr + op2.x, n - rr + h + op2.y);

            canvas.drawArc(oval1, 180 + v, 180 - 2 * v, false, mPaint);
        } else {
            float gh = (float) (n - rr + Math.sqrt((float) Math.pow(n, 2) - dR));
            RectF oval1 = new RectF(op1.x + rr - n, op1.y - gh,
                    n - rr + op2.x, op2.y - rr - h);
            canvas.drawArc(oval1, 180 - v, -180 + 2 * v, false, mPaint);
        }
        canvas.save();
        canvas.rotate(-k, cx, cy);
    }


发布了18 篇原创文章 · 获赞 11 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/INTKILOW/article/details/82771623