2 * Copyright (c) 2014 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/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>
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::operator=(const Vector3& rhs)
61 Vector2& Vector2::operator=(const Vector4& rhs)
70 bool Vector2::operator==(const Vector2& rhs) const
72 if (fabsf(x - rhs.x) > GetRangedEpsilon(x, rhs.x))
76 if (fabsf(y - rhs.y) > GetRangedEpsilon(y, rhs.y))
84 float Vector2::Length() const
86 return sqrtf(LengthSquared());
89 float Vector2::LengthSquared() const
94 void Vector2::Normalize()
96 float length = Length();
97 if( ! EqualsZero(length) )
99 MATH_INCREASE_BY(PerformanceMonitor::FLOAT_POINT_MULTIPLY,2);
101 const float inverseLength = 1.0f / length;
107 void Vector2::Clamp( const Vector2& min, const Vector2& max )
109 Dali::ClampInPlace<float>( x, min.x, max.x );
110 Dali::ClampInPlace<float>( y, min.y, max.y );
113 std::ostream& operator<< (std::ostream& o, const Vector2& vector)
115 return o << "[" << vector.x << ", " << vector.y << "]";
118 Vector2 Clamp( const Vector2& v, const float& min, const float& max )
121 result.Clamp( Vector2( min, min ) , Vector2( max, max ) );
126 Size FitInside( const Size& target, const Size& source )
128 Size fitted; //zero initialized
129 float widthScale = target.width / source.width;
130 float heightScale = target.height / source.height;
131 if( widthScale < heightScale )
133 // width scale is smaller so fit according to it
134 fitted.width = source.width * widthScale;
135 fitted.height = source.height * widthScale;
139 // height scale is smaller so fit according to it
140 fitted.width = source.width * heightScale;
141 fitted.height = source.height * heightScale;
146 Size FitScaleToFill( const Size& target, const Size& source )
148 Size fitted = target;
149 if( target.width > 0.0f && EqualsZero( target.height ) )
151 float scale = target.width / source.width;
152 fitted.height = scale*source.height;
154 else if( target.height > 0.0f && EqualsZero( target.width ) )
156 float scale = target.height / source.height;
157 fitted.width = scale * source.width;
159 else if( target.height > 0.0f && target.width > 0.0f ) // shouldn't be distorted
161 float widthScale = source.width / target.width;
162 float heightScale = source.height / target.height;
163 if (widthScale < heightScale && target.width > source.width)
166 fitted.height *= widthScale;
167 fitted.width *= widthScale;
169 else if (target.height > source.height)
172 fitted.width *= heightScale;
173 fitted.height *= heightScale;
175 // else requested dimensions don't need to be fitted
180 Size ShrinkInside( const Size& target, const Size& source )
182 // calculate source size vs target size to see if we need to shrink
183 float widthScale = 1.0f;
184 if( target.width < source.width )
186 // not enough width, width needs to shrink
187 widthScale = target.width / source.width;
189 float heightScale = 1.0f;
190 if( target.height < source.height )
192 // not enough height, height needs to shrink
193 heightScale = target.height / source.height;
195 // use smaller of the scales
196 float scale = std::min( widthScale, heightScale );
198 // check if we need to scale
201 // scale natural size to fit inside
202 return source * scale;
204 // there is enough space so use source size