How to draw Arc between two points on the Canvas? –

Development issue/problem:

I have two points on the canvas, now I can draw a line between the points as in the image below with

This code canvas.drawLine(p1.x, p1.y, p2.x, p2.y, paint) ;

I want to draw an arc between two points as shown in the picture below.

How can I draw like this?

How can I solve this problem?

Solution 1:

I finally found a solution with this code:

floating radius = 20 ;
last 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, -(floating) sweepAngle, true) ;

Use this code to calculate startAngle :

int startAngle = (int) (180 / Math.PI * Math.atan2(dot.y – dot1.y, dot.x – dot1.x)) ;

Here point 1 means the place where you want to draw the arc. sweepAngle means the angle between two lines. We have to calculate that with the two dots, like the blue dots on the picture in my question.

Solution 2:

Do something like this:

//color initialized on a class object.
Paint p = new Paint();
p.setColor(Color.BLACK);
/calculate rectangle/border of oval
RectF rectF = new RectF(50, 20, 100, 80) ;

@Draw protected void onDraw(Canvas canvas) {// Put the drawing in the onDraw() method of View. canvas.drawArc (rectF, 90, 45, false, p);}

Solution 3:

I was trying to do something else, and it’s calculating the rotation and launch angle.

I wanted to show an arc that shows the progress in a circle that goes from top to bottom.

So I had a progression value of 0…100 and I want to show an arc from top to bottom to close the circle when the progression is 100.

To understand the scan, I use:

int sweepAngle = (int) (360 * (getProgress() / 100.f))

The next step is to calculate the starting angle.

int startAngle = 270 – sweepAngle / 2 ;

The starting angle is calculated in this way because :

  1. It always started on the left, from top to bottom. So, the starting angle at the top is 270 (note that it is clockwise and that 0 = 3 hours, so 12 hours equals 270 degrees).
  2. Next, I want to calculate the distance from my starting point (270), and for that I calculate only half of the scanning angle, since only half of the arc will be on the left side and the other half on the right side.

So, since I’ve gone up 25%.

Rotation angle = 90 degrees (90 degrees is a quarter circle) Starting angle
= 225 (45 degrees to 270)

If you want the progression to come from other directions (from left to right, from right to left, etc.), just replace 270 with the starting angle.

Solution 4:

fill in the description of picture here, we first have to visualize how the coordinates start and reproduce in the form of angels, then everything becomes clearer.

So if you want the upper right side of the circle, we can do something like this:

val rect = RectF(0f, 0f, 500f, 300f)
val paint = Paint()
paint.apply {
strokeWidth = 5f
setStyle(Paint.Style.STROKE)
color = COLOR.BLUE
}
path.addArc(rect, 270f, 90f)

..

It starts at 270 (according to the above scheme and a 90 degree forward deviation):

Give here a description of the image

This time we use a negative value: We want to make a sickle (bow) from the right side:

path.addArc(rect, 0f, -180f)

Here we started with 0 and deployed at -180 degrees.
and results:

Give here a description of the image

Solution No 5:

a model for the drawing board.

public static bitmap clipRoundCorner(Bitmap bitmap, float r, boolean tr, boolean tl, boolean bl, boolean br)
{
int W = bitmap.getWidth() ;
int H = bitmap.getHeight() ;

if (r smallLeg)
r = smallLeg / 2 ;

drift lineStop = r/2 ;

Path = new path() ;
path.moveTo(0,0) ;

if(tr)
{
path.moveTo(0, lineStop);
path.arcTo(new RectF(0,0, r,r), 180, 90, false);
}

path.lineTo(W-lineStop, 0) ;

if(tl)
path.arcTo(new RectF(W-r,0, W,r), 270, 90, false);
otherwise
path.lineTo(W, 0) ;

path.lineTo(W, H-lineStop) ;

if(bl)
path.arcTo(new RectF(W-r,H-r,W,H), 0, 90, false);
otherwise
path.lineTo(W,H) ;

path.lineTo(lineStop, H) ;

if(br)
path.arcTo(new RectF(0,H-r, r,H), 90, 90, false);
otherwise
path.lineTo(0,H) ;

if(tr)
path.lineTo(0,lineStop);
otherwise
path.lineTo(0,0) ;

Bitmap output = Bitmap.createBitmap(W, H, Config.ARGB_8888) ;
Canvas = new Canvas(output) ;
last Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG) ;

paint.setColor (Color.BLACK) ;
canvas.drawPath (pad, paint);

paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)) ;
canvas.drawBitmap(bitmap, 0, 0, paint) ;

Reverse;
}

Solution No 6:

A simple solution has been proposed here by Langkiller. It draws a cubic line from the starting point through the reference point to the end point.

Path = new Path();
float startX = 0;
float startY = 2;
float controlX = 2;
float controlY = 4;
float endX = 4
float endY = 2
conePath.cubTo(startX, startY, controlX, controlY,endX, endY) ;

Paint paint = new paint() ;
paint.setARGB(200, 62, 90, 177) ;
paint.setStyle(Paint.Style.FILL) ;

canvas.drawPad (toad, painting)

Solution No 7:

I may be late for answers, but I have more information.

After Android Lollipop there are two ways to solve this problem.

blank artworkAgs (RectF oval, startAngle float, sweepAngle float, boole useCenter, paint) blank artworkAgs (left float, top float, right float, bottom float, startAngle float, sweepAngle float, boole useCenter, paint)

Usage :

RectF rectF = new RectF (left, top, right, bottom) ;

// Method 1
canvas.drawArc (rectF, 90, 45, true, paint [0]) ;

// Method 2
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
canvas.drawArc (left, top, right, bottom, 0, 45, where, paint [1]);
}

The scanning angle is nothing more than the sector angle, which is z. B. is drawn clockwise for the next code.

private void drawArcs(Canvas) {
RectF rectF = new RectF(left, top, right, bottom) ;

// white arc
canvas.drawArc (rectF, 90, 45, true, paint [0]) ;

// Green arc
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
canvas.drawArc (left, top, right, bottom, 0, 45, where, dyeing [1]) ;
}

//
arc with red line as (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
canvas.drawArc (left, top, right, bottom, 180, 45, true, paintts [2]) ;
}
}

The result is then as follows

Give here a description of the image

You can do the same by defining paths and browsing in the onDraw method, as shown in this extract:

The ArcDrawable public class expands Drawable {

private int left, right, top, bottom;
private Paint[] paints = new Paint [3] ;
private HashMap pathMap = new HashMap() ;

public ArcDrawable() {

// white color
color whitePaint = new paint (Paint.ANTI_ALIAS_FLAG);
whitePaint.setColor(Color.WHITE);
paint[0]= whitePaint ;

// green color
Paint greenPaint = new paint (Paint.ANTI_ALIAS_FLAG);
greenPaint.setColor(Color.GREEN);
paint[1]=greenPaint ;

// red color
paint redPaint = new paint (Paint.ANTI_ALIAS_FLAG);
redPaint.setColor (Color.RED);
redPaint.setStyle (Paint.Style.STROKE);
paint [2]= redPaint;
}.

@Override
public blank toss (Canvas) {

//———-USE PATHS———-
// Set a custom path
(Map.entry entry: pathMap.entrySet()) {
// Set the path with the corresponding paint style
canvas.drawPath(entry.getKey(), entry.getValue()) ;

}

// ——-OR use normal style———
//drawArcs(canvas) ;

}

// Same result
private void drawArcs(Canvas) {
RectF rectF = new RectF(left, top, right, bottom) ;

// Method 1
canvas.drawArc (rectF, 90, 45, true, paint [0]) ;

// Method 2
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
canvas.drawArc (left, top, right, bottom, 0, 45, where, paint [1]);
}

// Second method using line
as (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
canvas.drawArc (left, top, right, bottom, 180, 45, true, paintts [2]) ;
}
}

Override
protected against void onBoundsChange(Rect bounds) {
super.onBoundsChange(bounds)) ;

int width = limits.width() ;
int height = limits.height() ;

left = edge.left;
right = edge.right;
above = edge.above;
below = edge.below ;

final int size = Math.min (width, height) ;
final int centerX = border.left + (width / 2) ;
final int centerY = border.top + (height / 2) ;

pathMap.clear() ;
//fris pathmap with new boundaries
recreatePathMap(size, centerX, centerY) ;
disabledSelf() ;
}

private Path recreationePathMap (int size, int centerX, int centerY) {

RectF rectF = new RectF (left, top, right, bottom) ;

// first arc
path arcPath = new path();
arcPath.moveTo(centerX,centerY);
arcPath.arcTo(rectF, 90, 45);
arcPath.close();
// add
pathMap.put(arcPath, paintts[0]) in the drawing;

// Second arc
arcPath = new path();
arcPath.moveTo(centerX,centerY);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
arcPath.arcTo (rectF, 0, 45) ;
}
arcPath.close() ;
// add a map to draw
pathMap.put(arcPath, paintts[1]);

// Arc tiers
arcPath = new path() ;
arcPath.moveTo(centerX,centerY) ;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
arcPath.arcTo (rectF, 180, 45) ;

arcPath.close();
//
pathMap.put(arcPath, paintts[2]) add to the map drawing ;

Inversion Arch ;

}

@Override
public empty setAl a(int alpha) {

}

@Override
public void setColorFilter(@Unreplaceable colorFilter colorFilter) {

}

@Override
public int getOpacity() {
return 0;
}

}

Full source code :

https://github.com/hiteshsahu/Arc-Drawable

Good luck!

Related Tags:

how to draw an arc between two points,java draw arc between two points,c# draw arc between two points,equation of an arc between two points,draw arc between two points matlab,unity draw arc between two points,draw arc circle android,sweep angle in arc,drawarc flutter,android-canvas draw arc example,android canvas draw half circle

Author:

Leave a Reply

Your email address will not be published. Required fields are marked *