开发者

Algorithm for rotation a point along with the frame (relatively)

We have an application where we design a label, and then create an image from the design which is sent to a printer. The image is made into a byte stream before being sent over TCP/IP.

The short story is that we have a rotation setting that automatically calculates the position of the different elements on the label. And it is not working properly.

I was wondering if there is an easy way to calculate position of an element relative to height/width of the containing rectangle (label size). It also needs to take into account tha开发者_如何学Ct this is a bitmap, so the top left corner is (0,0). This is what confuses me, because most of the rotation algorithms I have found operates on an a regular X/Y axis.

So, say I have a label 500 x 300 (Width x height) with a point (text-box) at 100,100 (upper leftish). I want to rotate this 90 degrees, so that the label is now 300 x 500, with the text-box at 200 x 100 (upper rightish). Note, the rectangle calculation takes care of itself, I just need to find the point relative to the new sides.

I found 180 degrees (width - x, height - y), but having a hard time spotting the algorithm for 90 (and 270)

Probably no rocket surgery involved here, but I have stared myself blind at this problem.


A simple observation can show that for clockwise pi/2 rotation the formula is (hight - y, x). You can arrive to it from the general pi/2 rotation formula, (-y, x). (If you consider that 'origin of coordinates' changes too)

Getting 270-degrees formula is very similar.


If you want to rotate you label counterclockwise by 90 degrees, then each point (x, y) of the label "maps" to (-y, x). In the case of 270 degrees rotation (or 90 degrees clockwise, which is the same) (x, y) becomes (y, -x). Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜