15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved. 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file. 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef UI_GFX_QUAD_F_H_ 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define UI_GFX_QUAD_F_H_ 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 8d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)#include <algorithm> 990dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles)#include <cmath> 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string> 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1258537e28ecd584eab876aee8be7156509866d23aTorne (Richard Coles)#include "ui/gfx/gfx_export.h" 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/gfx/point_f.h" 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "ui/gfx/rect_f.h" 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace gfx { 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// A Quad is defined by four corners, allowing it to have edges that are not 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// axis-aligned, unlike a Rect. 204e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)class GFX_EXPORT QuadF { 215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public: 222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) QuadF() {} 232a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) QuadF(const PointF& p1, const PointF& p2, const PointF& p3, const PointF& p4) 242a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) : p1_(p1), 252a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) p2_(p2), 262a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) p3_(p3), 272a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) p4_(p4) {} 282a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) 292a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) explicit QuadF(const RectF& rect) 302a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) : p1_(rect.x(), rect.y()), 312a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) p2_(rect.right(), rect.y()), 322a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) p3_(rect.right(), rect.bottom()), 332a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles) p4_(rect.x(), rect.bottom()) {} 345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void operator=(const RectF& rect); 365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void set_p1(const PointF& p) { p1_ = p; } 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void set_p2(const PointF& p) { p2_ = p; } 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void set_p3(const PointF& p) { p3_ = p; } 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void set_p4(const PointF& p) { p4_ = p; } 415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const PointF& p1() const { return p1_; } 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const PointF& p2() const { return p2_; } 445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const PointF& p3() const { return p3_; } 455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) const PointF& p4() const { return p4_; } 465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Returns true if the quad is an axis-aligned rectangle. 485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool IsRectilinear() const; 495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Returns true if the points of the quad are in counter-clockwise order. This 515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // assumes that the quad is convex, and that no three points are collinear. 525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool IsCounterClockwise() const; 535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Returns true if the |point| is contained within the quad, or lies on on 555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // edge of the quad. 565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) bool Contains(const gfx::PointF& point) const; 575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Returns a rectangle that bounds the four points of the quad. The points of 595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // the quad may lie on the right/bottom edge of the resulting rectangle, 605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // rather than being strictly inside it. 6190dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) RectF BoundingBox() const { 6290dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) float rl = std::min(std::min(p1_.x(), p2_.x()), std::min(p3_.x(), p4_.x())); 6390dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) float rr = std::max(std::max(p1_.x(), p2_.x()), std::max(p3_.x(), p4_.x())); 6490dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) float rt = std::min(std::min(p1_.y(), p2_.y()), std::min(p3_.y(), p4_.y())); 6590dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) float rb = std::max(std::max(p1_.y(), p2_.y()), std::max(p3_.y(), p4_.y())); 6690dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) return RectF(rl, rt, rr - rl, rb - rt); 6790dce4d38c5ff5333bea97d859d4e484e27edf0cTorne (Richard Coles) } 685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Add a vector to the quad, offseting each point in the quad by the vector. 705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void operator+=(const Vector2dF& rhs); 715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Subtract a vector from the quad, offseting each point in the quad by the 725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // inverse of the vector. 735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void operator-=(const Vector2dF& rhs); 745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Scale each point in the quad by the |scale| factor. 765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void Scale(float scale) { Scale(scale, scale); } 775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Scale each point in the quad by the scale factors along each axis. 795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) void Scale(float x_scale, float y_scale); 805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Returns a string representation of quad. 825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::string ToString() const; 835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private: 855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) PointF p1_; 865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) PointF p2_; 875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) PointF p3_; 885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) PointF p4_; 895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)inline bool operator==(const QuadF& lhs, const QuadF& rhs) { 925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return 935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) lhs.p1() == rhs.p1() && lhs.p2() == rhs.p2() && 945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) lhs.p3() == rhs.p3() && lhs.p4() == rhs.p4(); 955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)inline bool operator!=(const QuadF& lhs, const QuadF& rhs) { 985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) return !(lhs == rhs); 995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Add a vector to a quad, offseting each point in the quad by the vector. 1024e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)GFX_EXPORT QuadF operator+(const QuadF& lhs, const Vector2dF& rhs); 1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Subtract a vector from a quad, offseting each point in the quad by the 1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// inverse of the vector. 1054e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)GFX_EXPORT QuadF operator-(const QuadF& lhs, const Vector2dF& rhs); 1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} // namespace gfx 1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif // UI_GFX_QUAD_F_H_ 110