15d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved. 25d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// found in the LICENSE file. 45d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 55d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Defines a simple float vector class. This class is used to indicate a 65d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// distance in two dimensions between two points. Subtracting two points should 75d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// produce a vector, and adding a vector to a point produces the point at the 85d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// vector's distance from the original point. 95d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#ifndef UI_GFX_GEOMETRY_VECTOR2D_F_H_ 115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#define UI_GFX_GEOMETRY_VECTOR2D_F_H_ 125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 13116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include <iosfwd> 145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <string> 155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ui/gfx/gfx_export.h" 175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace gfx { 195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class GFX_EXPORT Vector2dF { 215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) public: 225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) Vector2dF() : x_(0), y_(0) {} 235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) Vector2dF(float x, float y) : x_(x), y_(y) {} 245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) float x() const { return x_; } 265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) void set_x(float x) { x_ = x; } 275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) float y() const { return y_; } 295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) void set_y(float y) { y_ = y; } 305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // True if both components of the vector are 0. 325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) bool IsZero() const; 335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Add the components of the |other| vector to the current vector. 355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) void Add(const Vector2dF& other); 365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Subtract the components of the |other| vector from the current vector. 375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) void Subtract(const Vector2dF& other); 385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) void operator+=(const Vector2dF& other) { Add(other); } 405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) void operator-=(const Vector2dF& other) { Subtract(other); } 415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) void SetToMin(const Vector2dF& other) { 435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) x_ = x_ <= other.x_ ? x_ : other.x_; 445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) y_ = y_ <= other.y_ ? y_ : other.y_; 455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) } 465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) void SetToMax(const Vector2dF& other) { 485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) x_ = x_ >= other.x_ ? x_ : other.x_; 495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) y_ = y_ >= other.y_ ? y_ : other.y_; 505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) } 515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Gives the square of the diagonal length of the vector. 535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) double LengthSquared() const; 545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Gives the diagonal length of the vector. 555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) float Length() const; 565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Scale the x and y components of the vector by |scale|. 585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) void Scale(float scale) { Scale(scale, scale); } 595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Scale the x and y components of the vector by |x_scale| and |y_scale| 605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // respectively. 615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) void Scale(float x_scale, float y_scale); 625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) std::string ToString() const; 645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) private: 665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) float x_; 675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) float y_; 685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}; 695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)inline bool operator==(const Vector2dF& lhs, const Vector2dF& rhs) { 715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return lhs.x() == rhs.x() && lhs.y() == rhs.y(); 725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)inline bool operator!=(const Vector2dF& lhs, const Vector2dF& rhs) { 755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return !(lhs == rhs); 765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)inline Vector2dF operator-(const Vector2dF& v) { 795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return Vector2dF(-v.x(), -v.y()); 805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)inline Vector2dF operator+(const Vector2dF& lhs, const Vector2dF& rhs) { 835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) Vector2dF result = lhs; 845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) result.Add(rhs); 855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return result; 865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)inline Vector2dF operator-(const Vector2dF& lhs, const Vector2dF& rhs) { 895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) Vector2dF result = lhs; 905d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) result.Add(-rhs); 915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return result; 925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 945d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Return the cross product of two vectors. 955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)GFX_EXPORT double CrossProduct(const Vector2dF& lhs, const Vector2dF& rhs); 965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Return the dot product of two vectors. 985d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)GFX_EXPORT double DotProduct(const Vector2dF& lhs, const Vector2dF& rhs); 995d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1005d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Return a vector that is |v| scaled by the given scale factors along each 1015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// axis. 1025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)GFX_EXPORT Vector2dF ScaleVector2d(const Vector2dF& v, 1035d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) float x_scale, 1045d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) float y_scale); 1055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)// Return a vector that is |v| scaled by the given scale factor. 1075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)inline Vector2dF ScaleVector2d(const Vector2dF& v, float scale) { 1085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return ScaleVector2d(v, scale, scale); 1095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 1105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 111116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// This is declared here for use in gtest-based unit tests but is defined in 112116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// the gfx_test_support target. Depend on that to use this in your unit test. 113116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// This should not be used in production code - call ToString() instead. 114116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid PrintTo(const Vector2dF& vector, ::std::ostream* os); 115116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 1165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} // namespace gfx 1175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 1185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif // UI_GFX_GEOMETRY_VECTOR2D_F_H_ 119