Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make an equilateral triangle inscribed in a circle, knowing a vertex or a side

I'm learning Android and now I'm experimenting with the Canvas class.

I would like to draw a regular (equilateral) triangle inscribed into a known circle.

I think there must be a easier way to do it than getting into trigonomery, pythagoras,...

like image 957
Aritz Lopez Avatar asked Jan 25 '26 21:01

Aritz Lopez


1 Answers

Doing the trig is the most straightforward method that I've found. Below is a function for drawing an equilateral triangle in the normal, "pointing upward" orientation. I've posted a more sophisticated implementation here that also handles rotating the triangle.

private void drawCircumscribedTriangle(Canvas canvas, float circleCenterX, float circleCenterY, float radius, Paint paint) {
    float xOffsetFromCenter = FloatMath.cos((float)Math.PI/6) * radius;
    float yOffsetFromCenter = FloatMath.sin((float)Math.PI/6) * radius;

    canvas.drawLine(circleCenterX, circleCenterY - radius, circleCenterX + xOffsetFromCenter, circleCenterY + yOffsetFromCenter, paint);
    canvas.drawLine(circleCenterX + xOffsetFromCenter, circleCenterY + yOffsetFromCenter, circleCenterX - xOffsetFromCenter, circleCenterY + yOffsetFromCenter, paint);
    canvas.drawLine(circleCenterX - xOffsetFromCenter, circleCenterY + yOffsetFromCenter, circleCenterX, circleCenterY - radius, paint);
}
like image 150
acj Avatar answered Jan 27 '26 12:01

acj



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!