【问题标题】:Determinate if a rectangle fits into another确定一个矩形是否适合另一个
【发布时间】:2020-02-15 19:07:22
【问题描述】:

我正在使用画布在另一个内部绘制两个矩形(这是一个交互式图像裁剪视图)

问题是蓝色矩形也可以缩放和旋转(我在位图上使用 Matrix() 来做到这一点):

我可以像这样在没有旋转的情况下计算边界:

if( ( centerX - offsetX ) > ( greenRectangleWidth / 2f ) )
     // out of bounds

但是如果蓝色矩形旋转,我完全不知道如何计算蓝色矩形是否仍在绿色矩形内。我想我可以通过两种方式解决这个问题:

1. Read the pixel color of the edges of the blue square, if any of the edges will be outside the green square, the color will change. This seems like an unreliable way and I don't think I can even read 'pixel value' from canvas, then I would need to convert it to bitmap on each frame which will be very slow.

2. Calculate if any of the corners of the blue rectangle cross the edge of the green rectangle, but I'm not sure where to go from here. 

任何提示请如何最有效地解决这个问题?谢谢!

【问题讨论】:

  • 有必要用canvas画吗? drawable 是实现这个正方形的另一种选择
  • @Niceumang 很遗憾,是的,因为它是一个交互式“图像裁剪”视图

标签: android


【解决方案1】:

如果蓝色和绿色矩形都存储为Rect,我会期待这条线

if (!rectGreen.contains(rectBlue)){
  // blue is not inside the green
}

适用于两种情况 - 旋转和不旋转(我没有测试过)

https://developer.android.com/reference/android/graphics/Rect.html#contains(android.graphics.Rect)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 2020-07-31
    • 2023-03-14
    • 1970-01-01
    • 2011-11-24
    • 1970-01-01
    相关资源
    最近更新 更多