earlygrey/shapedrawer

rectangle() with rotation is incorrect

Closed this issue · 2 comments

Using the rectangle() calls with the rotation parameter produces incorrect results. The cause is presumably the following incorrect usage of the polygon() method (the variables passed to the method don't match with the parameters):

polygon(x + 0.5f*width, y + 0.5f*height, 4, lineWidth, rotation + ShapeUtils.PI_4, width, height, joinType);

Setting the correct scale is not super straightforward, because the rotation is applied after the scale. Something like the following could work:

float scaleFactor = (float) (0.5 / Math.sin(rotation + ShapeUtils.PI_4));
polygon(x + 0.5f*width, y + 0.5f*height, 4, width * scaleFactor, height * scaleFactor, rotation + ShapeUtils.PI_4, lineWidth, joinType);

If you want to avoid the transcendental functions, you could also use the float[] version of polygon(), but then you would have to create a Polygon object beforehand to apply the rotation.

Thanks for pointing that out - not sure what was going on there! That PR should fix it, let me know if you you think it's fine, otherwise I'll merge it.

Thank you, that PR fixes it for me!