Revert "[Tizen] Add codes for Dali Windows Backend"
[platform/core/uifw/dali-core.git] / dali / public-api / math / vector2.cpp
1 /*
2  * Copyright (c) 2015 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (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://www.apache.org/licenses/LICENSE-2.0
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
18 // CLASS HEADER
19 #include <dali/public-api/math/vector2.h>
20
21 // EXTERNAL INCLUDES
22 #include <math.h>
23 #include <ostream>
24
25 // INTERNAL INCLUDES
26 #include <dali/public-api/common/dali-common.h>
27 #include <dali/public-api/math/vector3.h>
28 #include <dali/public-api/math/math-utils.h>
29 #include <dali/internal/render/common/performance-monitor.h>
30
31 namespace Dali
32 {
33
34 const Vector2 Vector2::ONE(1.0f, 1.0f);
35 const Vector2 Vector2::XAXIS(1.0f, 0.0f);
36 const Vector2 Vector2::YAXIS(0.0f, 1.0f);
37 const Vector2 Vector2::NEGATIVE_XAXIS(-1.0f, 0.0f);
38 const Vector2 Vector2::NEGATIVE_YAXIS(0.0f, -1.0f);
39 const Vector2 Vector2::ZERO(0.0f, 0.0f);
40
41 Vector2::Vector2(const Vector3& vec3)
42 : x(vec3.x),
43   y(vec3.y)
44 {
45 }
46
47 Vector2::Vector2(const Vector4& vec4)
48 : x(vec4.x),
49   y(vec4.y)
50 {
51 }
52
53 Vector2& Vector2::operator=(const Vector3& rhs)
54 {
55   x = rhs.x;
56   y = rhs.y;
57
58   return *this;
59 }
60
61 Vector2& Vector2::operator=(const Vector4& rhs)
62 {
63   x = rhs.x;
64   y = rhs.y;
65
66   return *this;
67 }
68
69
70 bool Vector2::operator==(const Vector2& rhs) const
71 {
72   if (fabsf(x - rhs.x) > GetRangedEpsilon(x, rhs.x))
73   {
74     return false;
75   }
76   if (fabsf(y - rhs.y) > GetRangedEpsilon(y, rhs.y))
77   {
78     return false;
79   }
80
81   return true;
82 }
83
84 float Vector2::Length() const
85 {
86   return sqrtf(LengthSquared());
87 }
88
89 float Vector2::LengthSquared() const
90 {
91   return (x*x) + (y*y);
92 }
93
94 void Vector2::Normalize()
95 {
96   float length = Length();
97   if( ! EqualsZero(length) )
98   {
99     MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY,2);
100
101     const float inverseLength = 1.0f / length;
102     x *= inverseLength;
103     y *= inverseLength;
104   }
105 }
106
107 void Vector2::Clamp( const Vector2& min, const Vector2& max )
108 {
109   Dali::ClampInPlace<float>( x, min.x, max.x );
110   Dali::ClampInPlace<float>( y, min.y, max.y );
111 }
112
113 std::ostream& operator<< (std::ostream& o, const Vector2& vector)
114 {
115   return o << "[" << vector.x << ", " << vector.y << "]";
116 }
117
118 Vector2 Clamp( const Vector2& v, const float& min, const float& max )
119 {
120   Vector2 result( v );
121   result.Clamp( Vector2( min, min ) , Vector2( max, max ) );
122
123   return result;
124 }
125
126 } // namespace Dali