/*** * Expand `rect' to the smallest standardized rect containing it with pixel-aligned origin and size. * If @c scale is zero, then a scale of 1 will be used instead. * * @param rect the rectangle to align. * @param scale the scale factor to use for pixel alignment. * * @return the input rectangle aligned to the nearest pixels using the provided scale factor. * * @see CGRectIntegral */ public static CGRect MDCRectAlignToScale(CGRect rect, nfloat scale) { if (rect.IsNull()) { return(CGRect.Null); } if (MDCCGFloatEqual(scale, 0)) { scale = 1; } if (MDCCGFloatEqual(scale, 1)) { return(rect.Integral()); } CGPoint originalMinimumPoint = new CGPoint(rect.GetMinX(), rect.GetMinY()); CGPoint newOrigin = new CGPoint(MDCFloor(originalMinimumPoint.X * scale) / scale, MDCFloor(originalMinimumPoint.Y * scale) / scale); CGSize adjustWidthHeight = new CGSize(originalMinimumPoint.X - newOrigin.X, originalMinimumPoint.Y - newOrigin.Y); return(new CGRect(newOrigin.X, newOrigin.Y, MDCCeil((rect.Width + adjustWidthHeight.Width) * scale) / scale, MDCCeil((rect.Height + adjustWidthHeight.Height) * scale) / scale)); }
public override bool FinishedLaunching (UIApplication application, NSDictionary launchOptions) { // Two disjoint rects var r1 = new CGRect (50, 50, 10, 10); var r2 = new CGRect (100, 100, 10, 10); // This condradicts with Apple's doc – https://developer.apple.com/reference/coregraphics/cgrectnull // The null rectangle. This is the rectangle returned when, for example, you intersect two disjoint rectangles. // Note that the null rectangle is not the same as the zero rectangle. // For example, the union of a rectangle with the null rectangle is the original rectangle (that is, the null rectangle contributes nothing). var tmp = r1; tmp.Intersect (r2); // this is mutable method Console.WriteLine (tmp.IsNull ()); // Expected true, but result is false tmp = CGRectIntersection (r1, r2); Console.WriteLine (tmp.IsNull ()); // Expected true, actual true // This should be CGRectNull var rectNull = new CGRect (nfloat.PositiveInfinity, nfloat.PositiveInfinity, 0, 0); Console.WriteLine (rectNull.IsNull ()); // Expected true, actual true // CGRectEmpty and CGRectNull are different var union1 = r1.UnionWith (CGRect.Empty); // new rect {0, 0, 60, 60} Console.WriteLine (union1); var union2 = r1.UnionWith (rectNull); // r1 Console.WriteLine (union2); return true; }
/*** * Align the centerPoint of a view so that its origin is pixel-aligned to the nearest pixel. * Returns @c CGRectZero if @c scale is zero or @c bounds is @c CGRectNull. * * @param center the unaligned center of the view. * @param bounds the bounds of the view. * @param scale the native scaling factor for pixel alignment. * * @return the center point of the view such that its origin will be pixel-aligned. */ public static CGPoint MDCRoundCenterWithBoundsAndScale(CGPoint center, CGRect bounds, nfloat scale) { if (MDCCGFloatEqual(scale, 0) || bounds.IsNull()) { return(CGPoint.Empty); } nfloat halfWidth = bounds.Width / 2; nfloat halfHeight = bounds.Height / 2; CGPoint origin = new CGPoint(center.X - halfWidth, center.Y - halfHeight); origin = MDCPointRoundWithScale(origin, scale); return(new CGPoint(origin.X + halfWidth, origin.Y + halfHeight)); }