Matt Borum
Matt Borum

Reputation: 83

Starting point of Android drawLine changes with angle

I'm drawing a line from a fixed point and moving it in a circle. Depending on the angle of the line (specifically which quarter of the circle the angle is within) the starting x and y co-ordinate of the line change. See this video to see what I mean.

I've put a white 2x2 pixel square behind the line to better show the change in the starting x and y co-ordinates. The line should be drawn from the bottom right pixel of the square.

Here is the code which I am running in my DrawFrame method:

radians = Math.toRadians(angle);
x2 = 15.0 * Math.cos(radians);
y2 = 15.0 * Math.sin(radians);
c.drawLine(80, 140, 80 + (float)x2, 140 + (float)y2, mPaint);
angle += 1;

c is the Canvas and mPaint is a new Paint() object.

I'm new to Android, so maybe I'm missing something.

Upvotes: 1

Views: 1785

Answers (1)

HalR
HalR

Reputation: 11083

Technically, with floating point precision, your starting point is the exact center of the white box, which is the left and top of point 80, 140.

enter image description here

You might be happier with the results if you have your line start in the middle of the pixel-- like using 80.49 and 140.49 for you center.

enter image description here

Either way, you are just suffering from rounding problems in trying to draw a fine line with clunky pixels.

Upvotes: 2

Related Questions