'snap to' 应用变换后的像素

'snap to' pixel after applying transformations

//given these coordinates for a rectangle (which I will draw with rectmode CORNER)
float layerX = -50;
float layerY = -50;
float layerW = 100;
float layerH = 100;

//and these transformations
translate( 300, 0 );
scale( 12, 1.5 );
scale( .5, .5 );

//which project those coordinates here.
float sx = screenX( layerX, layerY );
float sy = screenY( layerX, layerY );
float sw = screenX( layerX+layerW, layerY+layerH );
float sh = screenY( layerX+layerW, layerY+layerH );

//(this spits out 0, -37.5, 600, 37.5)
println( sx, sy, sw, sh );

//how do I modify the transformation to 'snap' 
//the transformed values to [0, -38, 600, 38], 
//effectively snapping to the pixel (and widening the rectangle)?

您应该能够将值四舍五入为整数(整数)。

最简单的是:

int sx = (int)screenX( layerX, layerY );
int sy = (int)screenY( layerX, layerY );
int sw = (int)screenX( layerX+layerW, layerY+layerH );
int sh = (int)screenY( layerX+layerW, layerY+layerH );

但使用 round() will give you more accurate results, since casting to int will essentially floor() 值:

int sx = (int)round(screenX( layerX, layerY ));
int sy = (int)round(screenY( layerX, layerY ));
int sw = (int)round(screenX( layerX+layerW, layerY+layerH ));
int sh = (int)round(screenY( layerX+layerW, layerY+layerH ));