1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 /**
17  * @file graphic_geometry_math.h
18  *
19  * @brief Common library functions in Mathematics
20  *
21  * @since 1.0
22  * @version 1.0
23  */
24 
25 #ifndef GRAPHIC_LITE_COMMON_MATH_H
26 #define GRAPHIC_LITE_COMMON_MATH_H
27 
28 
29 #include "gfx_utils/diagram/common/common_basics.h"
30 
31 namespace OHOS {
32 /**
33  * @brief Calculate vector cross product
34  * @param X1, X2, X are the X coordinates of the vector point, Y1, Y2, y are the Y coordinates of the vector point
35  * @return If the return value is greater than 0, it is counterclockwise; If the return value is less than 0,
36  * it is clockwise; If the return value is equal to 0, it is collinear
37  * @since 1.0
38  * @version 1.0
39  */
CrossProduct(float x1,float y1,float x2,float y2,float x,float y)40 inline float CrossProduct(float x1, float y1, float x2, float y2, float x, float y)
41 {
42     return (x - x2) * (y2 - y1) - (y - y2) * (x2 - x1);
43 }
44 
45 /**
46  * @brief Calculate the distance between two points
47  * @param X1 and X2 are the X coordinates of the point, Y1 and Y2 are the Y coordinates of the point
48  * @return The return value is the distance between two points
49  * @since 1.0
50  * @version 1.0
51  */
CalcDistance(float x1,float y1,float x2,float y2)52 inline float CalcDistance(float x1, float y1, float x2, float y2)
53 {
54     float dx = x2 - x1;
55     float dy = y2 - y1;
56     return Sqrt(dx * dx + dy * dy);
57 }
58 
59 /**
60  * @brief Calculate the square of two points
61  * @param X1 and X2 are the X coordinates of the point, Y1 and Y2 are the Y coordinates of the point
62  * @return The return value is the square of two points
63  * @since 1.0
64  * @version 1.0
65  */
CalcSqDistance(float x1,float y1,float x2,float y2)66 inline float CalcSqDistance(float x1, float y1, float x2, float y2)
67 {
68     float dx = x2 - x1;
69     float dy = y2 - y1;
70     return dx * dx + dy * dy;
71 }
72 
73 /**
74  * @brief Calculate whether the two line segments intersect and find the intersection
75  * @param aX,bX,cX,dX are the X coordinates of the point, and ay, by, cy and dy are the Y coordinates of the point
76  * @param X is the X coordinate pointer of the intersection point,
77  * and Y is the Y coordinate pointer of the intersection point
78  * @return The return value true is intersecting and false is disjoint
79  * @since 1.0
80  * @version 1.0
81  */
CalcIntersection(float aX,float aY,float bX,float bY,float cX,float cY,float dX,float dY,float * x,float * y)82 inline bool CalcIntersection(float aX, float aY, float bX, float bY,
83                              float cX, float cY, float dX, float dY,
84                              float* x, float* y)
85 {
86     float num = (aY - cY) * (dX - cX) - (aX - cX) * (dY - cY);
87     float den = (bX - aX) * (dY - cY) - (bY - aY) * (dX - cX);
88     if (MATH_ABS(den) < INTERSECTIONEPSILON) {
89         return false;
90     }
91     float r = num / den;
92     *x = aX + r * (bX - aX);
93     *y = aY + r * (bY - aY);
94     return true;
95 }
96 } // namespace OHOS
97 #endif
98