NSSec
NSSec

Reputation: 4561

Determine coordinates of rotated rectangle

I'm creating an utility application that should detect and report the coordinates of the corners of a transparent rectangle (alpha=0) within an image.

So far, I've set up a system with Javascript + Canvas that displays the image and starts a floodfill-like operation when I click inside the transparent rectangle in the image. It correctly determines the bounding box of the floodfill operation and as such can provide me the correct coordinates.

Here's my implementation so-far: http://www.scriptorama.nl/image/ (works in recent Firefox / Safari ).

However, the bounding box approach breaks down then the transparent rectangle is rotated (CW or CCW) as the resulting bounding box no longer properly represents the proper width and height. I've tried to come up with a few alternatives to detect to corners, but have not been able to think up a proper solution.

So, does anyone have any suggestions on how I might approach this so I can properly detect the coordinates of 4 corners of the rotated rectangle?

Upvotes: 1

Views: 805

Answers (1)

Matthew Slattery
Matthew Slattery

Reputation: 47038

I think you can do this with a simple extension to your existing solution: walk along each of the 4 edges of the bounding box, looking for transparent pixels:

  • In the non-rotated case, all the pixels along each edge of the box will be transparent.
  • In the rotated case, there must be one corner touching each edge of the box. This will be at the transparent pixel furthest away from the middle of the edge (there may be more than one due to aliasing, e.g. if the rectangle is only very slightly rotated).

Upvotes: 4

Related Questions