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 #include "ui/gfx/geometry/point.h"
6
7 #include "base/strings/stringprintf.h"
8 #include "build/build_config.h"
9 #include "ui/gfx/geometry/point_conversions.h"
10 #include "ui/gfx/geometry/point_f.h"
11
12 #if defined(OS_WIN)
13 #include <windows.h>
14 #elif defined(OS_IOS)
15 #include <CoreGraphics/CoreGraphics.h>
16 #elif defined(OS_MACOSX)
17 #include <ApplicationServices/ApplicationServices.h>
18 #endif
19
20 namespace gfx {
21
22 #if defined(OS_WIN)
Point(DWORD point)23 Point::Point(DWORD point) {
24 POINTS points = MAKEPOINTS(point);
25 x_ = points.x;
26 y_ = points.y;
27 }
28
Point(const POINT & point)29 Point::Point(const POINT& point) : x_(point.x), y_(point.y) {
30 }
31
operator =(const POINT & point)32 Point& Point::operator=(const POINT& point) {
33 x_ = point.x;
34 y_ = point.y;
35 return *this;
36 }
37 #elif defined(OS_MACOSX)
38 Point::Point(const CGPoint& point) : x_(point.x), y_(point.y) {
39 }
40 #endif
41
42 #if defined(OS_WIN)
ToPOINT() const43 POINT Point::ToPOINT() const {
44 POINT p;
45 p.x = x();
46 p.y = y();
47 return p;
48 }
49 #elif defined(OS_MACOSX)
ToCGPoint() const50 CGPoint Point::ToCGPoint() const {
51 return CGPointMake(x(), y());
52 }
53 #endif
54
SetToMin(const Point & other)55 void Point::SetToMin(const Point& other) {
56 x_ = x_ <= other.x_ ? x_ : other.x_;
57 y_ = y_ <= other.y_ ? y_ : other.y_;
58 }
59
SetToMax(const Point & other)60 void Point::SetToMax(const Point& other) {
61 x_ = x_ >= other.x_ ? x_ : other.x_;
62 y_ = y_ >= other.y_ ? y_ : other.y_;
63 }
64
ToString() const65 std::string Point::ToString() const {
66 return base::StringPrintf("%d,%d", x(), y());
67 }
68
ScaleToCeiledPoint(const Point & point,float x_scale,float y_scale)69 Point ScaleToCeiledPoint(const Point& point, float x_scale, float y_scale) {
70 if (x_scale == 1.f && y_scale == 1.f)
71 return point;
72 return ToCeiledPoint(ScalePoint(gfx::PointF(point), x_scale, y_scale));
73 }
74
ScaleToCeiledPoint(const Point & point,float scale)75 Point ScaleToCeiledPoint(const Point& point, float scale) {
76 if (scale == 1.f)
77 return point;
78 return ToCeiledPoint(ScalePoint(gfx::PointF(point), scale, scale));
79 }
80
ScaleToFlooredPoint(const Point & point,float x_scale,float y_scale)81 Point ScaleToFlooredPoint(const Point& point, float x_scale, float y_scale) {
82 if (x_scale == 1.f && y_scale == 1.f)
83 return point;
84 return ToFlooredPoint(ScalePoint(gfx::PointF(point), x_scale, y_scale));
85 }
86
ScaleToFlooredPoint(const Point & point,float scale)87 Point ScaleToFlooredPoint(const Point& point, float scale) {
88 if (scale == 1.f)
89 return point;
90 return ToFlooredPoint(ScalePoint(gfx::PointF(point), scale, scale));
91 }
92
ScaleToRoundedPoint(const Point & point,float x_scale,float y_scale)93 Point ScaleToRoundedPoint(const Point& point, float x_scale, float y_scale) {
94 if (x_scale == 1.f && y_scale == 1.f)
95 return point;
96 return ToRoundedPoint(ScalePoint(gfx::PointF(point), x_scale, y_scale));
97 }
98
ScaleToRoundedPoint(const Point & point,float scale)99 Point ScaleToRoundedPoint(const Point& point, float scale) {
100 if (scale == 1.f)
101 return point;
102 return ToRoundedPoint(ScalePoint(gfx::PointF(point), scale, scale));
103 }
104
105 } // namespace gfx
106