Flutter Engine
The Flutter Engine
point_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#ifndef UI_GFX_GEOMETRY_POINT_F_H_
6#define UI_GFX_GEOMETRY_POINT_F_H_
7
8#include <iosfwd>
9#include <string>
10#include <tuple>
11
12#include "gfx/gfx_export.h"
13#include "point.h"
14#include "vector2d_f.h"
15
16namespace gfx {
17
18// A floating version of gfx::Point.
20 public:
21 constexpr PointF() : x_(0.f), y_(0.f) {}
22 constexpr PointF(float x, float y) : x_(x), y_(y) {}
23
24 constexpr explicit PointF(const Point& p)
25 : PointF(static_cast<float>(p.x()), static_cast<float>(p.y())) {}
26
27 constexpr float x() const { return x_; }
28 constexpr float y() const { return y_; }
29 void set_x(float x) { x_ = x; }
30 void set_y(float y) { y_ = y; }
31
32 void SetPoint(float x, float y) {
33 x_ = x;
34 y_ = y;
35 }
36
37 void Offset(float delta_x, float delta_y) {
38 x_ += delta_x;
39 y_ += delta_y;
40 }
41
42 void operator+=(const Vector2dF& vector) {
43 x_ += vector.x();
44 y_ += vector.y();
45 }
46
47 void operator-=(const Vector2dF& vector) {
48 x_ -= vector.x();
49 y_ -= vector.y();
50 }
51
52 void SetToMin(const PointF& other);
53 void SetToMax(const PointF& other);
54
55 bool IsOrigin() const { return x_ == 0 && y_ == 0; }
56
57 Vector2dF OffsetFromOrigin() const { return Vector2dF(x_, y_); }
58
59 // A point is less than another point if its y-value is closer
60 // to the origin. If the y-values are the same, then point with
61 // the x-value closer to the origin is considered less than the
62 // other.
63 // This comparison is required to use PointF in sets, or sorted
64 // vectors.
65 bool operator<(const PointF& rhs) const {
66 return std::tie(y_, x_) < std::tie(rhs.y_, rhs.x_);
67 }
68
69 void Scale(float scale) { Scale(scale, scale); }
70
71 void Scale(float x_scale, float y_scale) {
72 SetPoint(x() * x_scale, y() * y_scale);
73 }
74
75 // Returns a string representation of point.
76 std::string ToString() const;
77
78 private:
79 float x_;
80 float y_;
81};
82
83inline bool operator==(const PointF& lhs, const PointF& rhs) {
84 return lhs.x() == rhs.x() && lhs.y() == rhs.y();
85}
86
87inline bool operator!=(const PointF& lhs, const PointF& rhs) {
88 return !(lhs == rhs);
89}
90
91inline PointF operator+(const PointF& lhs, const Vector2dF& rhs) {
92 PointF result(lhs);
93 result += rhs;
94 return result;
95}
96
97inline PointF operator-(const PointF& lhs, const Vector2dF& rhs) {
98 PointF result(lhs);
99 result -= rhs;
100 return result;
101}
102
103inline Vector2dF operator-(const PointF& lhs, const PointF& rhs) {
104 return Vector2dF(lhs.x() - rhs.x(), lhs.y() - rhs.y());
105}
106
107inline PointF PointAtOffsetFromOrigin(const Vector2dF& offset_from_origin) {
108 return PointF(offset_from_origin.x(), offset_from_origin.y());
109}
110
111GFX_EXPORT PointF ScalePoint(const PointF& p, float x_scale, float y_scale);
112
113inline PointF ScalePoint(const PointF& p, float scale) {
114 return ScalePoint(p, scale, scale);
115}
116
117// This is declared here for use in gtest-based unit tests but is defined in
118// the //ui/gfx:test_support target. Depend on that to use this in your unit
119// test. This should not be used in production code - call ToString() instead.
120void PrintTo(const PointF& point, ::std::ostream* os);
121
122} // namespace gfx
123
124#endif // UI_GFX_GEOMETRY_POINT_F_H_
constexpr PointF(float x, float y)
Definition: point_f.h:22
void Offset(float delta_x, float delta_y)
Definition: point_f.h:37
bool operator<(const PointF &rhs) const
Definition: point_f.h:65
constexpr PointF()
Definition: point_f.h:21
constexpr float x() const
Definition: point_f.h:27
void SetPoint(float x, float y)
Definition: point_f.h:32
void operator-=(const Vector2dF &vector)
Definition: point_f.h:47
void set_y(float y)
Definition: point_f.h:30
void Scale(float x_scale, float y_scale)
Definition: point_f.h:71
constexpr float y() const
Definition: point_f.h:28
Vector2dF OffsetFromOrigin() const
Definition: point_f.h:57
void set_x(float x)
Definition: point_f.h:29
bool IsOrigin() const
Definition: point_f.h:55
void operator+=(const Vector2dF &vector)
Definition: point_f.h:42
constexpr PointF(const Point &p)
Definition: point_f.h:24
void Scale(float scale)
Definition: point_f.h:69
constexpr float y() const
Definition: vector2d_f.h:29
constexpr float x() const
Definition: vector2d_f.h:26
GAsyncResult * result
#define GFX_EXPORT
Definition: gfx_export.h:26
double y
double x
Definition: insets.cc:10
Point PointAtOffsetFromOrigin(const Vector2d &offset_from_origin)
Definition: point.h:120
Insets operator+(Insets lhs, const Insets &rhs)
Definition: insets.h:175
void PrintTo(const Point &point, ::std::ostream *os)
Definition: gfx_util.cc:74
PointF ScalePoint(const PointF &p, float x_scale, float y_scale)
Definition: point_f.cc:25
Insets operator-(Insets lhs, const Insets &rhs)
Definition: insets.h:180
bool operator==(const Point &lhs, const Point &rhs)
Definition: point.h:96
bool operator!=(const Point &lhs, const Point &rhs)
Definition: point.h:100
static std::string ToString(CompilerBackend::Type type)
Definition: reflector.cc:559
const Scalar scale