記事のカテゴリー: C#、.NET 9、WPF
矩形Aを矩形Bの真ん中に置く
矩形r
を矩形baseRect
の真ん中に移動します。
ちなみに、矩形とは4つの角がすべて直角な四角形のことです。
C#:
- public static Rect Center(Rect baseRect, Rect r)
- {
- r.Location = new Point(
- baseRect.Left + (baseRect.Width - r.Width) / 2,
- baseRect.Top + (baseRect.Height - r.Height) / 2);
- return r;
- }
矩形Aが矩形Bに含まれるように位置を調整
矩形r
が矩形container
の中に含まれていればOK、含まれていなければ含まれるまで最小限位置をずらします。
C#:
- public static Rect OffsetIfNotContains(Rect container, Rect r)
- {
- if (!container.Contains(r))
- {
- double offsetX = 0;
- double offsetY = 0;
- if (r.Width < container.Width)
- {
- if (r.Left < container.Left)
- {
- offsetX = container.Left - r.Left;
- }
- else if (r.Right > container.Right)
- {
- offsetX = container.Right - r.Right;
- }
- }
- if (r.Height < container.Height)
- {
- if (r.Top < container.Top)
- {
- offsetY = container.Top - r.Top;
- }
- else if (r.Bottom > container.Bottom)
- {
- offsetY = container.Bottom - r.Bottom;
- }
- }
- r.Offset(offsetX, offsetY);
- }
- return r;
- }
0 件のコメント:
コメントを投稿