point_f.cc 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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_f.h"
  5. #include <cmath>
  6. #include "base/check.h"
  7. #include "base/strings/stringprintf.h"
  8. #include "build/build_config.h"
  9. #if BUILDFLAG(IS_IOS)
  10. #include <CoreGraphics/CoreGraphics.h>
  11. #elif BUILDFLAG(IS_MAC)
  12. #include <ApplicationServices/ApplicationServices.h>
  13. #endif
  14. namespace gfx {
  15. #if BUILDFLAG(IS_APPLE)
  16. PointF::PointF(const CGPoint& p) : PointF(p.x, p.y) {}
  17. CGPoint PointF::ToCGPoint() const {
  18. return CGPointMake(x(), y());
  19. }
  20. #endif
  21. void PointF::SetToMin(const PointF& other) {
  22. x_ = std::min(x_, other.x_);
  23. y_ = std::min(y_, other.y_);
  24. }
  25. void PointF::SetToMax(const PointF& other) {
  26. x_ = std::max(x_, other.x_);
  27. y_ = std::max(y_, other.y_);
  28. }
  29. bool PointF::IsWithinDistance(const PointF& rhs,
  30. const float allowed_distance) const {
  31. DCHECK(allowed_distance > 0);
  32. float diff_x = x_ - rhs.x();
  33. float diff_y = y_ - rhs.y();
  34. float distance = std::sqrt(diff_x * diff_x + diff_y * diff_y);
  35. return distance < allowed_distance;
  36. }
  37. std::string PointF::ToString() const {
  38. return base::StringPrintf("%g,%g", x(), y());
  39. }
  40. PointF ScalePoint(const PointF& p, float x_scale, float y_scale) {
  41. PointF scaled_p(p);
  42. scaled_p.Scale(x_scale, y_scale);
  43. return scaled_p;
  44. }
  45. } // namespace gfx