如何准确绘制旋转位图?

发布于 2024-08-19 06:02:54 字数 1320 浏览 2 评论 0原文

我有一个容器,里面有蒙版位图。该容器的比例和旋转在运行时发生变化,我需要绘制遮罩位图,但无法找出适当的矩阵计算来执行此操作。

我的代码可以正确反映位置、比例、偏移量以实现居中而不旋转。旋转时,角度是正确的,但定位不正确 - 我相信是因为旋转矩形时尺寸会发生变化。

有人可以帮我弄清楚如何在定位中补偿这一点 - 这是一些代码:

 // Adjust the transformation matrix to account for the position of the container
        var tMatrix:Matrix = _imgContainer.transform.matrix;

        //Offset for container
        tMatrix.translate(this.x,this.y);

        //Offset for bounds centering
        tMatrix.translate(-_imgContainer.width/2,-_imgContainer.height/2);   

       // Compensate for rotation
       // ????

   var result_bitmap:BitmapData = new BitmapData(_maskedImg.width,_maskedImg.height,true,0x00FFFFFF);
   result_bitmap.lock();
   result_bitmap.draw(_maskedImg,tMatrix,null,null,null,true);
   result_bitmap.unlock();

提前感谢您提供的任何帮助 -

b

编辑: 抱歉,如果我没有正确解释这一点,请让我用图像再试一次以支持。我在容器中有一个蒙版位图,我将其用作绘制新位图的源。在捕获之前,用户可以在运行时缩放/旋转该容器。为了实现这一点,我向绘制方法传递了一个基于容器矩阵的变换矩阵,并调整 tx 和 ty 值以考虑非零原点(由于居中)。到目前为止,它运行良好并满足了我的期望。

然而,一旦该容器旋转,捕获的 POSITION 现在再次关闭 - 可能是由于尺寸发生变化,因此 tx/ty 偏移现在对于容器的新尺寸来说是不正确的。我只是需要补偿这一点,但不知道如何补偿。

有谁有变换矩阵的经验可以提供帮助吗?再次感谢您的努力!

由 imgur.com 托管

“由

I have a container with masked bitmap in it. The scale and rotation of this container changes at runtime, and I need to draw the masked bitmap but cannot figure out the appropriate matrix calculations to do so.

My code works correctly to reflect position, scale, offset for centering without rotation. When rotated, the angle is correct but the positioning is incorrect - I believe because the dimensions change when the rectangle is rotated.

Can someone help me figure out how to compensate for this in the positioning - here is some code:

 // Adjust the transformation matrix to account for the position of the container
        var tMatrix:Matrix = _imgContainer.transform.matrix;

        //Offset for container
        tMatrix.translate(this.x,this.y);

        //Offset for bounds centering
        tMatrix.translate(-_imgContainer.width/2,-_imgContainer.height/2);   

       // Compensate for rotation
       // ????

   var result_bitmap:BitmapData = new BitmapData(_maskedImg.width,_maskedImg.height,true,0x00FFFFFF);
   result_bitmap.lock();
   result_bitmap.draw(_maskedImg,tMatrix,null,null,null,true);
   result_bitmap.unlock();

Thanks in advance for any help you can provide -

b

EDIT:
Sorry if I'm not explaining this correctly, let me try again with an image to support. I have a masked bitmap in a container which I'm using as the source to draw a new bitmap. this container can be scaled/rotated at runtime by the user prior to capturing. In order to accomplish this I pass the draw method a transformation matrix based on the container matrix and adjust the tx and ty values to account for the non-zero origin (due to centering). Up to this point it works fine and captures what I would expect.

However - once this container is rotated, the POSITION of the capture is now off again - presumable due to the dimensions changing, therefore the tx/ty offsets now being incorrect for the new dimensions of the container. I simply need to compensate for this, but cannot figure out how.

Does anyone have experience with the transform matrix that can help?? Thanks again for the effort!

Hosted by imgur.com

Hosted by imgur.com

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

哀由 2024-08-26 06:02:54

仅供可能遇到类似问题的任何人参考。我通过显式设置变换矩阵值并按此特定顺序缩放、旋转然后平移解决了我的问题。

这不是从容器中复制变换矩阵。因此,我不是

var tMatrix:Matrix = _imgContainer.transform.matrix;

直接按以下顺序设置值:

tMatrix.scale (_imgContainer.scaleX, _imgContainer.scaleY);
tMatrix.rotate (_imgContainer.rotation * (Math.PI/180));
tMatrix.translate (_imgContainer.x, _imgContainer.y);

感谢您的努力 -

b

FYI for anyone that might come along with similar problems. I solved my issue by explicitly setting the transformation matrix values and in this specific order scale, rotation then translation.

This was instead of copying the transformation matrix from the container. So instead of

var tMatrix:Matrix = _imgContainer.transform.matrix;

I am setting the values directly in this order:

tMatrix.scale (_imgContainer.scaleX, _imgContainer.scaleY);
tMatrix.rotate (_imgContainer.rotation * (Math.PI/180));
tMatrix.translate (_imgContainer.x, _imgContainer.y);

Thanks for the effort -

b

守护在此方 2024-08-26 06:02:54

如果位图包含在容器内,则应用于容器的任何转换也应应用于其中包含的内容。

我不会对位图应用任何转换,我只会将它们应用到容器。

If the bitmap is contained inside the container, any transformations applied to the container should also be applied to the things contained within it.

I wouldn't apply any transformations to the bitmap, I'd apply them just to the container.

横笛休吹塞上声 2024-08-26 06:02:54

我不确定我是否完全理解你的问题,但如果你将 100x200 的对象旋转 90 度,它的宽度和高度将得到 200x100 ,这毫无价值。

有很多方法可以解决这个问题,但我通常使用scaleX/scaleY,因为它们不受旋转的影响,并将它们与剪辑的原始宽度/高度相乘。

I'm not sure I've understood your question completely, but it's worth nothing that if you rotate a 100x200 object 90 degrees it will give 200x100 for width and height.

There's a number of ways to get around this, but I normally use scaleX/scaleY, as they are not affected by rotations, and multiply those with the original width/height of the clip.

微凉 2024-08-26 06:02:54

我也没有理解你想要做什么 100% ,但我得到了一个基本问题,即你应用的旋转正在改变你在应用变换矩阵时需要使用的 tx/ty 。

在此基础上也许这会有所帮助: http://www.sencentric.com/flash/tutorials/参见“操作变换矩阵”部分,特别是关于使用 deltaTransformPoint 忽略平移值的部分。它是 AS2,但希望这些原则可以让您走上正轨。

I haven't grasped what you're trying to do 100% either, but I get the basic issue that the rotation you've applied is changing the tx/ty you need to use when applying your transform matrix.

On that basis perhaps this helps: http://www.senocular.com/flash/tutorials/transformmatrix/ Look at the section on "Manipulating Transformation Matrices", particularly the part about ignoring translation values with deltaTransformPoint. It's AS2, but hopefully the principles might get you on the right track.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文