Flutter Engine
The Flutter Engine
vector2d.h
Go to the documentation of this file.
1// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5// Defines a simple integer vector class. This class is used to indicate a
6// distance in two dimensions between two points. Subtracting two points should
7// produce a vector, and adding a vector to a point produces the point at the
8// vector's distance from the original point.
9
10#ifndef UI_GFX_GEOMETRY_VECTOR2D_H_
11#define UI_GFX_GEOMETRY_VECTOR2D_H_
12
13#include <cstdint>
14#include <iosfwd>
15#include <string>
16
17#include "gfx/gfx_export.h"
18#include "vector2d_f.h"
19
20namespace gfx {
21
23 public:
24 constexpr Vector2d() : x_(0), y_(0) {}
25 constexpr Vector2d(int x, int y) : x_(x), y_(y) {}
26
27 constexpr int x() const { return x_; }
28 void set_x(int x) { x_ = x; }
29
30 constexpr int y() const { return y_; }
31 void set_y(int y) { y_ = y; }
32
33 // True if both components of the vector are 0.
34 bool IsZero() const;
35
36 // Add the components of the |other| vector to the current vector.
37 void Add(const Vector2d& other);
38 // Subtract the components of the |other| vector from the current vector.
39 void Subtract(const Vector2d& other);
40
41 constexpr bool operator==(const Vector2d& other) const {
42 return x_ == other.x_ && y_ == other.y_;
43 }
44 void operator+=(const Vector2d& other) { Add(other); }
45 void operator-=(const Vector2d& other) { Subtract(other); }
46
47 void SetToMin(const Vector2d& other) {
48 x_ = x_ <= other.x_ ? x_ : other.x_;
49 y_ = y_ <= other.y_ ? y_ : other.y_;
50 }
51
52 void SetToMax(const Vector2d& other) {
53 x_ = x_ >= other.x_ ? x_ : other.x_;
54 y_ = y_ >= other.y_ ? y_ : other.y_;
55 }
56
57 // Gives the square of the diagonal length of the vector. Since this is
58 // cheaper to compute than Length(), it is useful when you want to compare
59 // relative lengths of different vectors without needing the actual lengths.
60 int64_t LengthSquared() const;
61 // Gives the diagonal length of the vector.
62 float Length() const;
63
64 std::string ToString() const;
65
66 operator Vector2dF() const {
67 return Vector2dF(static_cast<float>(x()), static_cast<float>(y()));
68 }
69
70 private:
71 int x_;
72 int y_;
73};
74
75inline constexpr Vector2d operator-(const Vector2d& v) {
76 return Vector2d(-v.x(), -v.y());
77}
78
79inline Vector2d operator+(const Vector2d& lhs, const Vector2d& rhs) {
80 Vector2d result = lhs;
81 result.Add(rhs);
82 return result;
83}
84
85inline Vector2d operator-(const Vector2d& lhs, const Vector2d& rhs) {
86 Vector2d result = lhs;
87 result.Add(-rhs);
88 return result;
89}
90
91// This is declared here for use in gtest-based unit tests but is defined in
92// the //ui/gfx:test_support target. Depend on that to use this in your unit
93// test. This should not be used in production code - call ToString() instead.
94void PrintTo(const Vector2d& vector, ::std::ostream* os);
95
96} // namespace gfx
97
98#endif // UI_GFX_GEOMETRY_VECTOR2D_H_
constexpr bool operator==(const Vector2d &other) const
Definition: vector2d.h:41
void set_y(int y)
Definition: vector2d.h:31
constexpr Vector2d(int x, int y)
Definition: vector2d.h:25
void SetToMin(const Vector2d &other)
Definition: vector2d.h:47
void operator-=(const Vector2d &other)
Definition: vector2d.h:45
void SetToMax(const Vector2d &other)
Definition: vector2d.h:52
constexpr int x() const
Definition: vector2d.h:27
void operator+=(const Vector2d &other)
Definition: vector2d.h:44
constexpr int y() const
Definition: vector2d.h:30
void set_x(int x)
Definition: vector2d.h:28
constexpr Vector2d()
Definition: vector2d.h:24
GAsyncResult * result
#define GFX_EXPORT
Definition: gfx_export.h:26
double y
double x
bool IsZero(char *begin, char *end)
Definition: insets.cc:10
Insets operator+(Insets lhs, const Insets &rhs)
Definition: insets.h:175
void PrintTo(const Point &point, ::std::ostream *os)
Definition: gfx_util.cc:74
Insets operator-(Insets lhs, const Insets &rhs)
Definition: insets.h:180
static std::string ToString(CompilerBackend::Type type)
Definition: reflector.cc:559