lottie/vector: move line related api to its own class .
[platform/core/uifw/lottie-player.git] / src / vector / vline.h
1 #ifndef VLINE_H
2 #define VLINE_H
3
4 #include "vglobal.h"
5 #include "vpoint.h"
6
7 V_BEGIN_NAMESPACE
8
9 class VLine {
10 public:
11     VLine() = default;
12     VLine(float x1, float y1, float x2, float y2)
13         : mX1(x1), mY1(y1), mX2(x2), mY2(y2)
14     {
15     }
16     VLine(const VPointF &p1, const VPointF &p2)
17         : mX1(p1.x()), mY1(p1.y()), mX2(p2.x()), mY2(p2.y())
18     {
19     }
20     float   length() const { return length(mX1, mY1, mX2, mY2);}
21     void    splitAtLength(float length, VLine &left, VLine &right) const;
22     VPointF p1() const { return VPointF(mX1, mY1); }
23     VPointF p2() const { return VPointF(mX2, mY2); }
24
25     static float length(float x1, float y1, float x2, float y2);
26
27 private:
28     float mX1{0};
29     float mY1{0};
30     float mX2{0};
31     float mY2{0};
32 };
33
34 // approximate sqrt(x*x + y*y) using alpha max plus beta min algorithm.
35 // With alpha = 1, beta = 3/8, giving results with the largest error less
36 // than 7% compared to the exact value.
37 inline float VLine::length(float x1, float y1, float x2, float y2)
38 {
39     float x = x2 - x1;
40     float y = y2 - y1;
41
42     x = x < 0 ? -x : x;
43     y = y < 0 ? -y : y;
44
45     return (x > y ? x + 0.375 * y : y + 0.375 * x);
46 }
47
48 inline void VLine::splitAtLength(float lengthAt, VLine &left, VLine &right) const
49 {
50     float  len = length();
51     float dx = ((mX2 - mX1) / len) * lengthAt;
52     float dy = ((mY2 - mY1) / len) * lengthAt;
53
54     left.mX1 = mX1;
55     left.mY1 = mY1;
56     left.mX2 = left.mX1 + dx;
57     left.mY2 = left.mY1 + dy;
58
59     right.mX1 = left.mX2;
60     right.mY1 = left.mY2;
61     right.mX2 = mX2;
62     right.mY2 = mY2;
63 }
64
65 #endif //VLINE_H