point.cc 2.6 KB

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