Pagrate
Pagrate

Reputation: 101

Android - draw line vertically

I have a custom view where I override the onDraw method and drawn a circle. Now I want to draw a line from the center of the circle to the top of the circle.

Here is my code..

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    canvas.drawRect(0, 0, getWidth(), getHeight(), backgroundPaint);
    canvas.drawCircle(centerX, centerY, outerRadius, outerCirclePaint);

    double angleRadians = Math.toRadians(0);

    double x = (outerRadius * Math.cos(angleRadians)) + centerX;
    double y = (outerRadius * Math.sin(angleRadians)) + centerY;

    canvas.drawLine((float)x, (float)y, centerX, centerY, innerCirclePaint);
}

centerX and centerY are the center of the circle outerRadius is the radius for the circle

When i run this the line is drawn from the center to the right at 90 degrees instead of the top of the circle 0 degrees, even though I have told it the angle is 0

This is confusing me and can't seem what I am doing wrong. If anyone has any ideas on this I would be very grateful

Upvotes: 0

Views: 160

Answers (1)

theKunz
theKunz

Reputation: 443

In mathematics, 0 degrees (also 0 radians) for the unit circle start at the right of the circle.

Unit circle

Add 90 degrees or pi/2 radians to start at the top.

Upvotes: 1

Related Questions