tangxu
2024-10-23 6a9aa8b628ace581657972a2e502ceb9a58b2395
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// COPYRIGHT (C) Tom. ALL RIGHTS RESERVED.
// THE AntdUI PROJECT IS AN WINFORM LIBRARY LICENSED UNDER THE Apache-2.0 License.
// LICENSED UNDER THE Apache License, VERSION 2.0 (THE "License")
// YOU MAY NOT USE THIS FILE EXCEPT IN COMPLIANCE WITH THE License.
// YOU MAY OBTAIN A COPY OF THE LICENSE AT
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING, SOFTWARE
// DISTRIBUTED UNDER THE LICENSE IS DISTRIBUTED ON AN "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
// SEE THE LICENSE FOR THE SPECIFIC LANGUAGE GOVERNING PERMISSIONS AND
// LIMITATIONS UNDER THE License.
// GITEE: https://gitee.com/antdui/AntdUI
// GITHUB: https://github.com/AntdUI/AntdUI
// CSDN: https://blog.csdn.net/v_132
// QQ: 17379620
 
using System;
using System.Drawing;
 
namespace AntdUI
{
    public struct Vector
    {
        double _x, _y;
 
        public Vector(double x, double y)
        {
            _x = x; _y = y;
        }
        public Vector(PointF pt)
        {
            _x = pt.X;
            _y = pt.Y;
        }
        public Vector(PointF st, PointF end)
        {
            _x = end.X - st.X;
            _y = end.Y - st.Y;
        }
 
        public double X
        {
            get { return _x; }
            set { _x = value; }
        }
 
        public double Y
        {
            get { return _y; }
            set { _y = value; }
        }
 
        public double Magnitude
        {
            get { return Math.Sqrt(X * X + Y * Y); }
        }
 
        public static Vector operator +(Vector v1, Vector v2)
        {
            return new Vector(v1.X + v2.X, v1.Y + v2.Y);
        }
 
        public static Vector operator -(Vector v1, Vector v2)
        {
            return new Vector(v1.X - v2.X, v1.Y - v2.Y);
        }
 
        public static Vector operator -(Vector v)
        {
            return new Vector(-v.X, -v.Y);
        }
 
        public static Vector operator *(double c, Vector v)
        {
            return new Vector(c * v.X, c * v.Y);
        }
 
        public static Vector operator *(Vector v, double c)
        {
            return new Vector(c * v.X, c * v.Y);
        }
 
        public static Vector operator /(Vector v, double c)
        {
            return new Vector(v.X / c, v.Y / c);
        }
 
        public double CrossProduct(Vector v)
        {
            return _x * v.Y - v.X * _y;
        }
 
        public double DotProduct(Vector v)
        {
            return _x * v.X + _y * v.Y;
        }
 
        public static bool IsClockwise(PointF pt1, PointF pt2, PointF pt3)
        {
            Vector v1 = new Vector(pt2, pt1), v2 = new Vector(pt2, pt3);
            return v1.CrossProduct(v2) < 0;
        }
 
        public static bool IsCCW(PointF pt1, PointF pt2, PointF pt3)
        {
            Vector v1 = new Vector(pt2, pt1), v2 = new Vector(pt2, pt3);
            return v1.CrossProduct(v2) > 0;
        }
 
        public static double DistancePointLine(PointF pt, PointF lnA, PointF lnB)
        {
            Vector v1 = new Vector(lnA, lnB), v2 = new Vector(lnA, pt);
            v1 /= v1.Magnitude;
            return Math.Abs(v2.CrossProduct(v1));
        }
 
        public void Rotate(int degree)
        {
            double radian = degree * Math.PI / 180.0;
            double sin = Math.Sin(radian), cos = Math.Cos(radian);
            double nx = _x * cos - _y * sin, ny = _x * sin + _y * cos;
            _x = nx;
            _y = ny;
        }
 
        public PointF ToPointF()
        {
            return new PointF((float)_x, (float)_y);
        }
    }
}