Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw Arc between two points on the Canvas?

I have two points in the canvas, now I'm able to draw a line between those points like this below image by using

This code canvas.drawLine(p1.x, p1.y, p2.x, p2.y, paint); enter image description here

I want to draw the arc between two points like below image.

enter image description here

How can I draw like this.

like image 656
RajaReddy PolamReddy Avatar asked Jun 21 '12 05:06

RajaReddy PolamReddy


People also ask

How do you put an arc on a canvas?

The arc() method creates an arc/curve (used to create circles, or parts of circles). Tip: To create a circle with arc(): Set start angle to 0 and end angle to 2*Math. PI. Tip: Use the stroke() or the fill() method to actually draw the arc on the canvas.

How do you draw an angle with an arc?

You can draw arcs using any of the following methods: Three points on an arc ( ). Start point-center-endpoint ( ), or Start point-endpoint-center ( ), or Center-start point-endpoint ( ). Start point-center-included angle ( ), or Start point-included angle-center ( ), or Center-start point-included angle ( ).

How do you make a bitmap on canvas?

Use the Canvas method public void drawBitmap (Bitmap bitmap, Rect src, RectF dst, Paint paint) . Set dst to the size of the rectangle you want the entire image to be scaled into. EDIT: Here's a possible implementation for drawing the bitmaps in squares across on the canvas.


2 Answers

Finally I got the solution from this code:

float radius = 20; final RectF oval = new RectF(); oval.set(point1.x - radius, point1.y - radius, point1.x + radius, point1.y+ radius); Path myPath = new Path(); myPath.arcTo(oval, startAngle, -(float) sweepAngle, true); 

To calculate startAngle, use this code:

int startAngle = (int) (180 / Math.PI * Math.atan2(point.y - point1.y, point.x - point1.x)); 

Here, point1 means where you want to start drawing the Arc. sweepAngle means the angle between two lines. We have to calculate that by using two points like the blue points in my Question image.

like image 141
RajaReddy PolamReddy Avatar answered Sep 19 '22 16:09

RajaReddy PolamReddy


Do something like this:

//Initialized paint on a class level object. Paint p = new Paint(); p.setColor(Color.BLACK); //Calculate the rect / bounds of oval RectF rectF = new RectF(50, 20, 100, 80);  @Override protected void onDraw(Canvas canvas) {           //Do the drawing in onDraw() method of View.     canvas.drawArc (rectF, 90, 45, false, p); } 
like image 34
Adil Soomro Avatar answered Sep 21 '22 16:09

Adil Soomro