首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >有效的目标矩形源矩形裁剪

有效的目标矩形源矩形裁剪
EN

Stack Overflow用户
提问于 2014-04-23 11:01:21
回答 1查看 816关注 0票数 0

每次我画到一个表面,我需要确保不要画出界限。我一次抽到262,144张。我不能编写或修改任何底层机器指令。我需要剪辑部分屏幕上的源矩形及其相关的目标矩形。

代码语言:javascript
复制
// return if dest_rect is entirely out bounds

// if dest_rect is partially inbounds modify src_rect and dest_rect accordingly

// draw without clipping if dest_rect is entirely inbounds

我将使用SDK提供的基于浮点的向量对象来进行一些硬件优化。我在过去见过一些帮助解决类似问题的min max技巧,我不确定我是否能够编写最优的代码。

编辑*为了明确起见,我向C++咨询如何为所描述的问题编写一个最优的剪裁机制。例如,这可能有一个最优的逻辑,我已经知道如何通过保持最优的调用堆栈大小和内联函数等来使代码优化。例如,建议只能解决一部分问题,比如有一个最优的裁剪函数。

*编辑我目前的解决方案:

代码语言:javascript
复制
#include <algorithm>
float Clip(float _n, float _lower, float _upper)
{

    return std::max(_lower, std::min(_n, _upper));
}

void Iw2DImage::DrawClipped(CIwFVec2 _here, CIwFVec2 _bounds_xy, CIwFVec2 _bounds_wh) const
{
    // image is a region of texture atlas starting at xy with width and height wh

    // clip the destination region by the given bounds 
    const CIwFVec2 clipped_xy(
        Clip(_here.x,                _bounds_xy.x,   _bounds_xy.x + _bounds_wh.x),
        Clip(_here.y,                _bounds_xy.y,   _bounds_xy.y + _bounds_wh.y));
    const CIwFVec2 clipped_wh(
        Clip(_here.x + atlas_wh.x,   _bounds_xy.x,   _bounds_xy.x + _bounds_wh.x),
        Clip(_here.y + atlas_wh.y,   _bounds_xy.y,   _bounds_xy.y + _bounds_wh.y));

    // no point drawing an image with 0 width or height
    if (clipped_xy.x == clipped_wh.x) return;
    if (clipped_xy.y == clipped_wh.y) return;

    // change the source xy and wh by the same change in the destination region
    const CIwFVec2 clipped_atlas_xy(atlas_xy + (clipped_xy - _here));
    const CIwFVec2 clipped_atlas_wh(atlas_wh + (clipped_wh - (_here + atlas_wh)));

    /**
     * Draw a region of an image at 1:1 size at the specified location, modulated by the current colour, using the current alphamode
     * @param image the image to draw
     * @param topLeft the top left position of the image on the screen
     * @param regionOffset the top left of the region within the image
     * @param regionSize the size of the region within the image (and thus, on the screen)
     * @see Iw2DSetColour, Iw2DSetAlphaMode, Iw2DSetImageTransform
     * @par Required Header Files
     * Iw2D.h
     */
    Iw2DDrawImageRegion(i, clipped_xy, clipped_atlas_xy, clipped_atlas_wh);

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-04-23 11:17:17

我不确定我是否正确地理解了这个问题,但是如果您想检查src_rect是否在从(0,0)(dest_rect.width(), dest_rect.height())的坐标的绘图区域内,您可以这样做

代码语言:javascript
复制
bool outOfBonds( src_rect, dest_rect )
{
   if( src_rect.X < 0 || src_rect.X + src_rect.Width() > dest_rect.width() )
      return true; //clipping on X axis
   else if( src_rect.Y < 0 || src_rect.Y + src_rect.Height() > dest_rect.Height() )
      return true; //clipping on Y axis

   return false;//No clipping
}

如果您有两个具有随机坐标的区域,并且希望确保src_rectdest_rect中,请将其修改为

代码语言:javascript
复制
bool outOfBonds( src_rect, dest_rect )
{
   if( src_rect.X < dest_rect.X || 
       src_rect.X + src_rect.Width() > dest_rect.X + dest_rect.width() )
   {
      return true; //clipping on X axis
   }
   else if( src_rect.Y < dest_rect.Y || 
            src_rect.Y + src_rect.Height() > dest_rect.Y +dest_rect.Height() )
   {
      return true; //clipping on Y axis
   }

   return false;//No clipping
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23242315

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档