C#:StrokeCollection类扩展方法——边界框计算

using System;
using System.Windows;
using System.Windows.Ink;
using System.Windows.Input;

namespace Splash
{
    /// <summary>
    /// StrokeCollection类扩展方法
    /// 编写:秦建辉 splashcn@msn.com
    /// 日期:2011年08月22日
    /// </summary>
    public static class Extensions
    {
        /// <summary>
        /// 计算笔画集合的边界框
        /// </summary>
        /// <param name="sc">笔画集合</param>
        /// <returns>矩形区域</returns>
        public static Rect Bounding(this StrokeCollection sc)
        {
            if (sc == null || sc.Count == 0)
                throw new InvalidOperationException();

            Double xMin = Double.MaxValue;  // X坐标最小值
            Double xMax = Double.MinValue;  // X坐标最大值
            Double yMin = Double.MaxValue;  // Y坐标最小值
            Double yMax = Double.MinValue;  // Y坐标最大值
            foreach (Stroke s in sc)
            {
                if (s.StylusPoints == null || s.StylusPoints.Count == 0)
                    continue;

                foreach (StylusPoint sp in s.StylusPoints)
                {
                    if (sp.X < xMin) 
                        xMin = sp.X;
                    else if(sp.X > xMax)
                        xMax = sp.X;

                    if (sp.Y < yMin)
                        yMin = sp.Y;
                    else if (sp.Y > yMax)
                        yMax = sp.Y;
                }
            }

            if (xMin > xMax || yMin > yMax)
                throw new InvalidOperationException();

            return new Rect { X = xMin, Y = yMin, Width = xMax - xMin, Height = yMax - yMin };            
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值