michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #ifndef MOZILLA_GFX_BASEPOINT_H_ michael@0: #define MOZILLA_GFX_BASEPOINT_H_ michael@0: michael@0: #include michael@0: #include "mozilla/Attributes.h" michael@0: michael@0: namespace mozilla { michael@0: namespace gfx { michael@0: michael@0: /** michael@0: * Do not use this class directly. Subclass it, pass that subclass as the michael@0: * Sub parameter, and only use that subclass. This allows methods to safely michael@0: * cast 'this' to 'Sub*'. michael@0: */ michael@0: template michael@0: struct BasePoint { michael@0: T x, y; michael@0: michael@0: // Constructors michael@0: MOZ_CONSTEXPR BasePoint() : x(0), y(0) {} michael@0: MOZ_CONSTEXPR BasePoint(T aX, T aY) : x(aX), y(aY) {} michael@0: michael@0: void MoveTo(T aX, T aY) { x = aX; y = aY; } michael@0: void MoveBy(T aDx, T aDy) { x += aDx; y += aDy; } michael@0: michael@0: // Note that '=' isn't defined so we'll get the michael@0: // compiler generated default assignment operator michael@0: michael@0: bool operator==(const Sub& aPoint) const { michael@0: return x == aPoint.x && y == aPoint.y; michael@0: } michael@0: bool operator!=(const Sub& aPoint) const { michael@0: return x != aPoint.x || y != aPoint.y; michael@0: } michael@0: michael@0: Sub operator+(const Sub& aPoint) const { michael@0: return Sub(x + aPoint.x, y + aPoint.y); michael@0: } michael@0: Sub operator-(const Sub& aPoint) const { michael@0: return Sub(x - aPoint.x, y - aPoint.y); michael@0: } michael@0: Sub& operator+=(const Sub& aPoint) { michael@0: x += aPoint.x; michael@0: y += aPoint.y; michael@0: return *static_cast(this); michael@0: } michael@0: Sub& operator-=(const Sub& aPoint) { michael@0: x -= aPoint.x; michael@0: y -= aPoint.y; michael@0: return *static_cast(this); michael@0: } michael@0: michael@0: Sub operator*(T aScale) const { michael@0: return Sub(x * aScale, y * aScale); michael@0: } michael@0: Sub operator/(T aScale) const { michael@0: return Sub(x / aScale, y / aScale); michael@0: } michael@0: michael@0: Sub operator-() const { michael@0: return Sub(-x, -y); michael@0: } michael@0: michael@0: // Round() is *not* rounding to nearest integer if the values are negative. michael@0: // They are always rounding as floor(n + 0.5). michael@0: // See https://bugzilla.mozilla.org/show_bug.cgi?id=410748#c14 michael@0: Sub& Round() { michael@0: x = static_cast(floor(x + 0.5)); michael@0: y = static_cast(floor(y + 0.5)); michael@0: return *static_cast(this); michael@0: } michael@0: michael@0: }; michael@0: michael@0: } michael@0: } michael@0: michael@0: #endif /* MOZILLA_GFX_BASEPOINT_H_ */