lottie/vector: move pixman code to separate pixman folder.
[platform/core/uifw/lottie-player.git] / src / vector / vline.h
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd. All rights reserved.
3  *
4  * Licensed under the Flora License, Version 1.1 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://floralicense.org/license/
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef VLINE_H
18 #define VLINE_H
19
20 #include "vglobal.h"
21 #include "vpoint.h"
22
23 V_BEGIN_NAMESPACE
24
25 class VLine {
26 public:
27     VLine() = default;
28     VLine(float x1, float y1, float x2, float y2)
29         : mX1(x1), mY1(y1), mX2(x2), mY2(y2)
30     {
31     }
32     VLine(const VPointF &p1, const VPointF &p2)
33         : mX1(p1.x()), mY1(p1.y()), mX2(p2.x()), mY2(p2.y())
34     {
35     }
36     float   length() const { return length(mX1, mY1, mX2, mY2);}
37     void    splitAtLength(float length, VLine &left, VLine &right) const;
38     VPointF p1() const { return {mX1, mY1}; }
39     VPointF p2() const { return {mX2, mY2}; }
40     float angle() const;
41     static float length(float x1, float y1, float x2, float y2);
42
43 private:
44     float mX1{0};
45     float mY1{0};
46     float mX2{0};
47     float mY2{0};
48 };
49
50 inline float VLine::angle() const
51 {
52     const float dx = mX2 - mX1;
53     const float dy = mY2 - mY1;
54
55     const float theta = std::atan2(dy, dx) * 180.0 / M_PI;
56     return theta;
57 }
58
59 // approximate sqrt(x*x + y*y) using alpha max plus beta min algorithm.
60 // With alpha = 1, beta = 3/8, giving results with the largest error less
61 // than 7% compared to the exact value.
62 inline float VLine::length(float x1, float y1, float x2, float y2)
63 {
64     float x = x2 - x1;
65     float y = y2 - y1;
66
67     x = x < 0 ? -x : x;
68     y = y < 0 ? -y : y;
69
70     return (x > y ? x + 0.375 * y : y + 0.375 * x);
71 }
72
73 inline void VLine::splitAtLength(float lengthAt, VLine &left, VLine &right) const
74 {
75     float  len = length();
76     float dx = ((mX2 - mX1) / len) * lengthAt;
77     float dy = ((mY2 - mY1) / len) * lengthAt;
78
79     left.mX1 = mX1;
80     left.mY1 = mY1;
81     left.mX2 = left.mX1 + dx;
82     left.mY2 = left.mY1 + dy;
83
84     right.mX1 = left.mX2;
85     right.mY1 = left.mY2;
86     right.mX2 = mX2;
87     right.mY2 = mY2;
88 }
89
90 #endif //VLINE_H