2 * Copyright (c) 2020 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <dali/public-api/math/vector2.h>
26 #include <dali/internal/render/common/performance-monitor.h>
27 #include <dali/public-api/common/dali-common.h>
28 #include <dali/public-api/math/math-utils.h>
29 #include <dali/public-api/math/uint-16-pair.h>
30 #include <dali/public-api/math/vector3.h>
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);
41 Vector2::Vector2(const Vector3& vec3)
47 Vector2::Vector2(const Vector4& vec4)
53 Vector2::Vector2(const Uint16Pair& pair)
54 : width(pair.GetWidth()),
55 height(pair.GetHeight())
59 Vector2& Vector2::operator=(const Vector3& rhs)
67 Vector2& Vector2::operator=(const Vector4& rhs)
75 bool Vector2::operator==(const Vector2& rhs) const
77 if(fabsf(x - rhs.x) > GetRangedEpsilon(x, rhs.x))
81 if(fabsf(y - rhs.y) > GetRangedEpsilon(y, rhs.y))
89 float Vector2::Length() const
91 return sqrtf(LengthSquared());
94 float Vector2::LengthSquared() const
96 return (x * x) + (y * y);
99 void Vector2::Normalize()
101 float length = Length();
102 if(!EqualsZero(length))
104 MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY, 2);
106 const float inverseLength = 1.0f / length;
112 void Vector2::Clamp(const Vector2& min, const Vector2& max)
114 Dali::ClampInPlace<float>(x, min.x, max.x);
115 Dali::ClampInPlace<float>(y, min.y, max.y);
118 std::ostream& operator<<(std::ostream& o, const Vector2& vector)
120 return o << "[" << vector.x << ", " << vector.y << "]";
123 Vector2 Clamp(const Vector2& v, const float& min, const float& max)
126 result.Clamp(Vector2(min, min), Vector2(max, max));