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 integer 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_H_ 115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#define UI_GFX_GEOMETRY_VECTOR2D_H_ 125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 13116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include <iosfwd> 145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include <string> 155d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 165d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "base/basictypes.h" 175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ui/gfx/geometry/vector2d_f.h" 185d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "ui/gfx/gfx_export.h" 195d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)namespace gfx { 215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class GFX_EXPORT Vector2d { 235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) public: 245d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) Vector2d() : x_(0), y_(0) {} 255d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) Vector2d(int x, int y) : x_(x), y_(y) {} 265d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 275d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) int x() const { return x_; } 285d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) void set_x(int x) { x_ = x; } 295d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 305d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) int y() const { return y_; } 315d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) void set_y(int y) { y_ = y; } 325d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 335d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // True if both components of the vector are 0. 345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) bool IsZero() const; 355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Add the components of the |other| vector to the current vector. 375d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) void Add(const Vector2d& other); 385d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Subtract the components of the |other| vector from the current vector. 395d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) void Subtract(const Vector2d& other); 405d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 415d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) void operator+=(const Vector2d& other) { Add(other); } 425d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) void operator-=(const Vector2d& other) { Subtract(other); } 435d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) void SetToMin(const Vector2d& other) { 455d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) x_ = x_ <= other.x_ ? x_ : other.x_; 465d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) y_ = y_ <= other.y_ ? y_ : other.y_; 475d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) } 485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 495d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) void SetToMax(const Vector2d& other) { 505d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) x_ = x_ >= other.x_ ? x_ : other.x_; 515d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) y_ = y_ >= other.y_ ? y_ : other.y_; 525d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) } 535d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 545d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Gives the square of the diagonal length of the vector. Since this is 555d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // cheaper to compute than Length(), it is useful when you want to compare 565d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // relative lengths of different vectors without needing the actual lengths. 575d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) int64 LengthSquared() const; 585d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Gives the diagonal length of the vector. 595d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) float Length() const; 605d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 615d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) std::string ToString() const; 625d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 635d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) operator Vector2dF() const { return Vector2dF(x_, y_); } 645d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 655d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) private: 665d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) int x_; 675d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) int y_; 685d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)}; 695d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 705d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)inline bool operator==(const Vector2d& lhs, const Vector2d& rhs) { 715d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return lhs.x() == rhs.x() && lhs.y() == rhs.y(); 725d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 735d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)inline Vector2d operator-(const Vector2d& v) { 755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return Vector2d(-v.x(), -v.y()); 765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 775d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)inline Vector2d operator+(const Vector2d& lhs, const Vector2d& rhs) { 795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) Vector2d result = lhs; 805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) result.Add(rhs); 815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return result; 825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)inline Vector2d operator-(const Vector2d& lhs, const Vector2d& rhs) { 855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) Vector2d result = lhs; 865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) result.Add(-rhs); 875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) return result; 885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} 895d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 90116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// This is declared here for use in gtest-based unit tests but is defined in 91116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// the gfx_test_support target. Depend on that to use this in your unit test. 92116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch// This should not be used in production code - call ToString() instead. 93116680a4aac90f2aa7413d9095a592090648e557Ben Murdochvoid PrintTo(const Vector2d& vector, ::std::ostream* os); 94116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch 955d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)} // namespace gfx 965d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#endif // UI_GFX_GEOMETRY_VECTOR2D_H_ 98