Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
vector2d_f.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 float 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_F_H_
11#define UI_GFX_GEOMETRY_VECTOR2D_F_H_
12
13#include <iosfwd>
14#include <memory>
15#include <string>
16
17#include "gfx/gfx_export.h"
18
19namespace gfx {
20
22 public:
23 constexpr Vector2dF() : x_(0), y_(0) {}
24 constexpr Vector2dF(float x, float y) : x_(x), y_(y) {}
25
26 constexpr float x() const { return x_; }
27 void set_x(float x) { x_ = x; }
28
29 constexpr float y() const { return y_; }
30 void set_y(float y) { y_ = y; }
31
32 // True if both components of the vector are 0.
33 bool IsZero() const;
34
35 // Add the components of the |other| vector to the current vector.
36 void Add(const Vector2dF& other);
37 // Subtract the components of the |other| vector from the current vector.
38 void Subtract(const Vector2dF& other);
39
40 void operator+=(const Vector2dF& other) { Add(other); }
41 void operator-=(const Vector2dF& other) { Subtract(other); }
42
43 void SetToMin(const Vector2dF& other) {
44 x_ = x_ <= other.x_ ? x_ : other.x_;
45 y_ = y_ <= other.y_ ? y_ : other.y_;
46 }
47
48 void SetToMax(const Vector2dF& other) {
49 x_ = x_ >= other.x_ ? x_ : other.x_;
50 y_ = y_ >= other.y_ ? y_ : other.y_;
51 }
52
53 // Gives the square of the diagonal length of the vector.
54 double LengthSquared() const;
55 // Gives the diagonal length of the vector.
56 float Length() const;
57
58 // Scale the x and y components of the vector by |scale|.
59 void Scale(float scale) { Scale(scale, scale); }
60 // Scale the x and y components of the vector by |x_scale| and |y_scale|
61 // respectively.
62 void Scale(float x_scale, float y_scale);
63
64 std::string ToString() const;
65
66 private:
67 float x_;
68 float y_;
69};
70
71inline constexpr bool operator==(const Vector2dF& lhs, const Vector2dF& rhs) {
72 return lhs.x() == rhs.x() && lhs.y() == rhs.y();
73}
74
75inline constexpr bool operator!=(const Vector2dF& lhs, const Vector2dF& rhs) {
76 return !(lhs == rhs);
77}
78
79inline constexpr Vector2dF operator-(const Vector2dF& v) {
80 return Vector2dF(-v.x(), -v.y());
81}
82
83inline Vector2dF operator+(const Vector2dF& lhs, const Vector2dF& rhs) {
84 Vector2dF result = lhs;
85 result.Add(rhs);
86 return result;
87}
88
89inline Vector2dF operator-(const Vector2dF& lhs, const Vector2dF& rhs) {
90 Vector2dF result = lhs;
91 result.Add(-rhs);
92 return result;
93}
94
95// Return the cross product of two vectors.
96GFX_EXPORT double CrossProduct(const Vector2dF& lhs, const Vector2dF& rhs);
97
98// Return the dot product of two vectors.
99GFX_EXPORT double DotProduct(const Vector2dF& lhs, const Vector2dF& rhs);
100
101// Return a vector that is |v| scaled by the given scale factors along each
102// axis.
103GFX_EXPORT Vector2dF ScaleVector2d(const Vector2dF& v,
104 float x_scale,
105 float y_scale);
106
107// Return a vector that is |v| scaled by the given scale factor.
108inline Vector2dF ScaleVector2d(const Vector2dF& v, float scale) {
109 return ScaleVector2d(v, scale, scale);
110}
111
112// This is declared here for use in gtest-based unit tests but is defined in
113// the //ui/gfx:test_support target. Depend on that to use this in your unit
114// test. This should not be used in production code - call ToString() instead.
115void PrintTo(const Vector2dF& vector, ::std::ostream* os);
116
117} // namespace gfx
118
119#endif // UI_GFX_GEOMETRY_VECTOR2D_F_H_
void Add(const Vector2dF &other)
Definition vector2d_f.cc:21
constexpr float y() const
Definition vector2d_f.h:29
void Scale(float scale)
Definition vector2d_f.h:59
void set_x(float x)
Definition vector2d_f.h:27
void operator-=(const Vector2dF &other)
Definition vector2d_f.h:41
constexpr float x() const
Definition vector2d_f.h:26
void SetToMax(const Vector2dF &other)
Definition vector2d_f.h:48
void set_y(float y)
Definition vector2d_f.h:30
constexpr Vector2dF()
Definition vector2d_f.h:23
void operator+=(const Vector2dF &other)
Definition vector2d_f.h:40
void SetToMin(const Vector2dF &other)
Definition vector2d_f.h:43
constexpr Vector2dF(float x, float y)
Definition vector2d_f.h:24
GAsyncResult * result
#define GFX_EXPORT
Definition gfx_export.h:26
double y
double x
Definition insets.cc:10
Insets operator+(Insets lhs, const Insets &rhs)
Definition insets.h:175
double DotProduct(const Vector2dF &lhs, const Vector2dF &rhs)
Definition vector2d_f.cc:49
Vector2dF ScaleVector2d(const Vector2dF &v, float x_scale, float y_scale)
Definition vector2d_f.cc:54
void PrintTo(const Point &point, ::std::ostream *os)
Definition gfx_util.cc:74
Insets operator-(Insets lhs, const Insets &rhs)
Definition insets.h:180
bool operator==(const Point &lhs, const Point &rhs)
Definition point.h:96
double CrossProduct(const Vector2dF &lhs, const Vector2dF &rhs)
Definition vector2d_f.cc:44
bool operator!=(const Point &lhs, const Point &rhs)
Definition point.h:100
const Scalar scale