WPF 4 System.Windows.Point

This sample illustrates the different operations of the Point structure.

 

Point 是一个struct

1 Point 中的运算符+ 重载

Point + Vector ,得到一个新的Point ,这里用到了运算符重载+

        public static Point operator +( Point point, Vector vector);

这意味着将一个点位移到一个新的位置。

注:另一个+ 重载位于Vector 类中:

        public static Point operator +(Vector vector, Point point);

可以实现Vector + Point 的运算,见下面的示例。

2 )以上的效果,使用静态方法 Point .Add 也能实现:

        pointResult = Point.Add(point1, vector1);

3 -- 6

对于Point 减法,有两种:

一种是Point-Vector ,得到Point

                        pointResult = point1 - vector1;

另一种是Point-Point ,得到Vector

                        vectorResult = point1 - point2;

相应的静态方法分别为

pointResult = Point.Subtract(point1, vector1)

vectorResult = Point.Subtract(point1, point2)

7 )如果只是想产生位移,而不需得到这个位移量,可以使用 Offset 方法:

          point1.Offset(20, 30);

8 -- 9)Point * Matrix ,即将一个点位移(因为是点,没有变形效果):

                        Point point1 = new Point(10, 5);

                        Matrix matrix1 = new Matrix(40, 50, 60, 70, 80, 90);

                       //X = 10 * 40 + 5 * 60 + 80; Y = 10 * 50 + 5 * 70 + 90;

                         Point pointResult = point1 * matrix1;    //(780, 940)

以上的效果,使用静态方法 Point .Multiply 也能实现:

Point .Multiply(point1, matrix1);

10) - 12) 比较两个Point

使用运算符重载== ,或其实例方法 point1.Equals(point2) ,或其静态方法 Point .Equals(point1, point2) 来比较两个 Point

这里比较的是相等性,而不是对象实例的同一性,只要两个点在同一位置就返回true

13 )这里是比较两个 Vector

          vector1.Equals(vector2);

14 -- 15 )这里演示了Point String 的转换:

                        pointResult = Point.Parse( "1,3");             // 解析为一个Point

                        pointString = pointResult.ToString();         // 返回字符串: "1,3"

16 )重写了 GetHashCode 方法,以得到一个唯一的哈希值。

17 -- 18 )可以将Point 转换为Size 类型或Vector 类型:

                        Point point1 = new Point(10, 5);

                        Size size1 = new Size();

                        size1 = ( Size)point1;

这将得到一个宽10 5 Size 对象。

                         Vector vector1 = new Vector();

                        vector1 = ( Vector)point1;

这将得到一个(10, 5) Vector 对象。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值