Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How to draw circle within two point?

Tags:

android

canvas

this is my current code

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);

with this code I am getting as output:

enter image description here

But I want to draw circle like this:

enter image description here

UPDATED Source code: This source code worked in my case [SOLVED]

[OnDraw]

@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);
}

[OnTouchEvent]

@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;
}
like image 437
Nicky Avatar asked Sep 14 '25 13:09

Nicky


2 Answers

The first two paramateres of addCircle are the x and y coordinates of the center. Assuming A and B are the furthest distance from each other on the circle you want, then the center should a point equidistant to both, hence:

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

And your radius should be, the distance between A and B, thus:

float radius = (Math.sqrt(Math.pow(x2−x1, 2) + Math.pow(y2−y1, 2))) / 2
like image 77
Mehmet K Avatar answered Sep 16 '25 02:09

Mehmet K


Mid points

 int mx=(r.stopX + r.startX)/2;
 int my= (r.stopy+r.startY)/2;

Radius

float radius = Math.sqrt(Math.pow(r.stopX - my, 2)
        + Math.pow(r.stopY - my, 2));

Edited

I have used code below

public class CustomView extends View {
  private Paint paint;
  private Circle circle;
  private List<Point> points;
  public final int CIRCLE_BETWEEN_TWO_POINTS = 1;
  private int viewType;

  {
    paint = new Paint();
    points = new ArrayList<>();
  }

  public CustomView(Context context) {
    super(context);
  }

  public CustomView(Context context, AttributeSet attrs) {
    super(context, attrs);
  }

  public CustomView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
  }

  @Override
  protected void onDraw(Canvas canvas) {
    switch (viewType) {
      case CIRCLE_BETWEEN_TWO_POINTS:
        drawView(canvas);
        break;
    }
  }

  private void drawView(Canvas canvas){
    for(Point point:points){
      drawCircle(canvas,new Circle(point.x,point.y,10),false);
    }
    drawCircle(canvas,circle,true);
  }

  private void drawCircle(Canvas canvas,Circle circle, boolean isStroke){
    paint.reset();
    paint.setAntiAlias(true);
    if(isStroke){
      paint.setStrokeWidth(5);
      paint.setColor(Color.BLACK);
      paint.setStyle(Paint.Style.STROKE);
    }else {
      paint.setColor(Color.RED);
      paint.setStyle(Paint.Style.FILL);
    }

    canvas.drawCircle(circle.getX(), circle.getY(), circle.getRadius(), paint);
  }

  public void drawCircleBetweenTwoPoints(int x1, int y1, int x2, int y2) {
    viewType = CIRCLE_BETWEEN_TWO_POINTS;
    points.clear();
    points.add(new Point(x1, y1));
    points.add(new Point(x2, y2));
    int mx = (x1 + x2) / 2;
    int my = (y1 + y2) / 2;
    double radius = Math.sqrt(Math.pow(x1 - mx, 2)
      + Math.pow(y1 - my, 2));
    circle=new Circle(mx,my,(int)radius);
    invalidate();
  }


} 

And called method as

customView.drawCircleBetweenTwoPoints(500,200,100,600);

and its working for me

Click to see output

like image 20
Sanjay Kumar Avatar answered Sep 16 '25 04:09

Sanjay Kumar