如何在两点内画圆

如何在两点内画圆

这是我现在的密码

Path path_eclipse = new Path();
float radius = (float) (Math.sqrt(Math.pow(r.stopX - r.startX, 2.0f) + Math.pow(r.stopY - r.startY, 2.0f)) / 2.0f);
path_eclipse.addCircle(r.startX, r.startY, radius, Path.Direction.CCW);
canvas.drawPath(path_eclipse, paint);

有了这段代码,我得到的输出是:
android - Android:如何在两点内画圆?-LMLPHP
但我想画这样的圆:
android - Android:如何在两点内画圆?-LMLPHP
更新的源代码:这个源代码在我的案例中有效
[解决]
[ OnDRAP ]
@Override
protected void onDraw(Canvas canvas) {
        Path path_eclipse = new Path();
        float centerX = (r.startX + r.stopX) /2;
        float centerY = (r.startY + r.stopY) /2;
        float radius = (float)Math.sqrt((r.stopX - r.startX)*(r.stopX - r.startX)+(r.stopY - r.startY)*(r.stopY - r.startY));
        path_eclipse.addCircle(centerX, centerY, radius/2, Path.Direction.CCW);
        canvas.drawPath(path_eclipse,paint);
}

[本体事件]
@Override
public boolean onTouchEvent(MotionEvent event) {
        float eventX = event.getX();
        float eventY = event.getY();
        switch (event.getAction()) {
               case MotionEvent.ACTION_DOWN:
                    startX = eventX;
                    startY = eventY;
                    return true;
               case MotionEvent.ACTION_MOVE:
                    stopX = eventX;
                    stopY = eventY;
                    break;
               case MotionEvent.ACTION_UP:
                    stopX = eventX;
                    stopY = eventY;
                    break;
               default:
                    return false;
        }
        invalidate();
        return true;
}

最佳答案

addCircle的前两个参数是中心的x和y坐标。假设A和B是圆上彼此之间的最远距离,则中心应为与两者等距的点,因此:

float centerX = (pointA.x + pointB.x) /2
float centerY = (pointA.y + pointB.y) /2

你的半径应该是,A和B之间的距离,因此:
float radius = (Math.sqrt(Math.pow(x2−x1) + Math.pow(y2−y1))) / 2

关于android - Android:如何在两点内画圆?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42085507/

10-12 04:33