PAIR-code / megaplot

Apache License 2.0
19 stars 5 forks source link

Fix border distance calculation for non-1:1 scale component ratio #63

Closed jimbojw closed 2 years ago

jimbojw commented 2 years ago

When rendering, Megaplot uses the distance to the nearest shape edge to determine whether each pixel is inside the shape, outside the shape, or in the border. The following image shows an ellipse with a 100px border rendered correctly.

Screen Shot 2022-09-30 at 5 29 21 AM

Notice that the tops and sides have the same thickness. (There is currently no closed form solution to computing the distance from a point to an ellipse, so Megaplot estimates the distance, which is imperfect in places).

However, currently the edge distance computation assumes a 1:1 ratio between the scene's scale.x and scale.y. When the ratio is something else, the border calculation uses the square values, causing borders to be stretched or squished.

The following image shows a circle with 100px borders where the Y component of the scene scale has been stretched. Notice that the borders are correctly 100px wide on the sides, but about twice as large on the top and bottom.

Screen Shot 2022-09-30 at 5 34 07 AM

The combination of these effects causes especially strange behavior. The following is a tall ellipse, like the first image, but rendered in a scene whose scale.y has been compressed so that the final shape is back to circular in canvas coordinates. Notice how here again, the side borders are correctly 100px, but the top and bottom are compressed.

Screen Shot 2022-09-30 at 5 54 41 AM

Fix the border distance calculation to account for scene scale component ratio so that all of these cases render correctly sized borders.