/// <summary>Return the closest point on 'rect' to 'pt'</summary> public static v2 ClosestPoint(BRect rect, v2 pt) { v2 lower = rect.Lower; v2 upper = rect.Upper; v2 closest; if (rect.IsWithin(pt)) { // if pt.x/pt.y > rect.sizeX/rect.sizeY then the point // is closer to the Y edge of the rectangle if (Math_.Abs(pt.x * rect.SizeY) > Math_.Abs(pt.y * rect.SizeX)) { closest = new v2(pt.x, Math_.Sign(pt.y) * rect.SizeY); } else { closest = new v2(Math_.Sign(pt.x) * rect.SizeX, pt.y); } } else { closest = new v2( Math_.Clamp(pt.x, lower.x, upper.x), Math_.Clamp(pt.y, lower.y, upper.y)); } return(closest); }
public static BBox operator *(m4x4 m, BBox rhs) { Debug.Assert(rhs.IsValid, "Transforming an invalid bounding box"); var bb = new BBox(m.pos, v4.Zero); var mat = Math_.Transpose3x3(m); bb.Centre.x += Math_.Dot(mat.x, rhs.Centre); bb.Radius.x += Math_.Dot(Math_.Abs(mat.x), rhs.Radius); bb.Centre.y += Math_.Dot(mat.y, rhs.Centre); bb.Radius.y += Math_.Dot(Math_.Abs(mat.y), rhs.Radius); bb.Centre.z += Math_.Dot(mat.z, rhs.Centre); bb.Radius.z += Math_.Dot(Math_.Abs(mat.z), rhs.Radius); return(bb); }
public static BRect From(Rectangle r) { return(new BRect(v2.From(r.Location) + v2.From(r.Size) / 2f, Math_.Abs(v2.From(r.Size)) / 2f)); }
// Construct from public static BBox From(v4 min, v4 max) { return(new BBox((max + min) * 0.5f, Math_.Abs(max - min) * 0.5f)); }