Normalising CGRect between 0 and 1

前端 未结 3 522
遥遥无期
遥遥无期 2021-01-04 19:18

What is the best way to normalise CGRect values so that they are between 0 and 1 (unit coordinate system)?

3条回答
  •  半阙折子戏
    2021-01-04 19:27

    How about this:

    CGRect rectToConvert;
    NSAffineTransform *n = [NSAffineTransform transform];
    [n scaleXBy:rectToConvert.size.width 
            yBy:rectToConvert.size.height];
    [n invert];
    
    CGRect normalizedRect;
    normalizedRect.origin = [n transformPoint:rectToConvert.origin];
    normalizedRect.size   = [n transformSize: rectToConvert.size];
    
    CGPoint anyPointINeedToConvert;
    CGPoint convertedPoint = [n transformPoint:anyPointINeedToConvert];
    

    I thinks this makes it a little clearer what's going on and lets you take care of translations in an obvious way, if you have to do them.

提交回复
热议问题