40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński/**
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński * \file
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński * \brief Cartesian point / 2D vector with integer coordinates
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński *//*
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński * Copyright 2011 Krzysztof Kosiński <tweenk.pl@gmail.com>
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński *
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński * This library is free software; you can redistribute it and/or
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński * modify it either under the terms of the GNU Lesser General Public
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński * License version 2.1 as published by the Free Software Foundation
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński * (the "LGPL") or, at your option, under the terms of the Mozilla
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński * Public License Version 1.1 (the "MPL"). If you do not alter this
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński * notice, a recipient may use your version of this file under either
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński * the MPL or the LGPL.
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński *
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński * You should have received a copy of the LGPL along with this library
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński * in the file COPYING-LGPL-2.1; if not, write to the Free Software
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński * You should have received a copy of the MPL along with this library
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński * in the file COPYING-MPL-1.1
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński *
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński * The contents of this file are subject to the Mozilla Public License
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński * Version 1.1 (the "License"); you may not use this file except in
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński * compliance with the License. You may obtain a copy of the License at
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński * http://www.mozilla.org/MPL/
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński *
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński * OF ANY KIND, either express or implied. See the LGPL or the MPL for
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński * the specific language governing rights and limitations.
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński */
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński#ifndef LIB2GEOM_SEEN_INT_POINT_H
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński#define LIB2GEOM_SEEN_INT_POINT_H
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński#include <stdexcept>
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński#include <boost/operators.hpp>
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński#include <2geom/coord.h>
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosińskinamespace Geom {
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński/**
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński * @brief Two-dimensional point with integer coordinates.
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński *
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński * This class is an exact equivalent of Point, except it stores integer coordinates.
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński * Integer points are useful in contexts related to rasterized graphics, for example
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński * for bounding boxes when rendering SVG.
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński *
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński * @see Point
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński * @ingroup Primitives */
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosińskiclass IntPoint
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński : boost::additive< IntPoint
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński , boost::totally_ordered< IntPoint
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński > >
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński{
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński IntCoord _pt[2];
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosińskipublic:
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński /// @name Creating integer points
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński /// @{
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński IntPoint() { }
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński IntPoint(IntCoord x, IntCoord y) {
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński _pt[X] = x;
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński _pt[Y] = y;
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński }
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński /// @}
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński /// @name Access the coordinates of a point
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński /// @{
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński IntCoord operator[](unsigned i) const {
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński if ( i > Y ) throw std::out_of_range("index out of range");
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński return _pt[i];
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński }
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński IntCoord &operator[](unsigned i) {
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński if ( i > Y ) throw std::out_of_range("index out of range");
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński return _pt[i];
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński }
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński IntCoord operator[](Dim2 d) const { return _pt[d]; }
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński IntCoord &operator[](Dim2 d) { return _pt[d]; }
d6519bf53baba32bd74436ad9c85f1fa2c6b6ae9Krzysztof Kosiński
d6519bf53baba32bd74436ad9c85f1fa2c6b6ae9Krzysztof Kosiński IntCoord x() const throw() { return _pt[X]; }
d6519bf53baba32bd74436ad9c85f1fa2c6b6ae9Krzysztof Kosiński IntCoord &x() throw() { return _pt[X]; }
d6519bf53baba32bd74436ad9c85f1fa2c6b6ae9Krzysztof Kosiński IntCoord y() const throw() { return _pt[Y]; }
d6519bf53baba32bd74436ad9c85f1fa2c6b6ae9Krzysztof Kosiński IntCoord &y() throw() { return _pt[Y]; }
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński /// @}
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński /// @name Vector-like arithmetic operations
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński /// @{
a16a494f042310ee849a6f717ffea70846f1f22cKrzysztof Kosiński IntPoint operator-() const {
a16a494f042310ee849a6f717ffea70846f1f22cKrzysztof Kosiński IntPoint ret(-_pt[X], -_pt[Y]);
a16a494f042310ee849a6f717ffea70846f1f22cKrzysztof Kosiński return ret;
a16a494f042310ee849a6f717ffea70846f1f22cKrzysztof Kosiński }
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński IntPoint &operator+=(IntPoint const &o) {
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński _pt[X] += o._pt[X];
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński _pt[Y] += o._pt[Y];
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński return *this;
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński }
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński IntPoint &operator-=(IntPoint const &o) {
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński _pt[X] -= o._pt[X];
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński _pt[Y] -= o._pt[Y];
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński return *this;
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński }
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński /// @}
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński /// @name Various utilities
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński /// @{
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński /** @brief Equality operator. */
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński bool operator==(IntPoint const &in_pnt) const {
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński return ((_pt[X] == in_pnt[X]) && (_pt[Y] == in_pnt[Y]));
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński }
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński /** @brief Lexicographical ordering for points.
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński * Y coordinate is regarded as more significant. When sorting according to this
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński * ordering, the points will be sorted according to the Y coordinate, and within
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński * points with the same Y coordinate according to the X coordinate. */
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński bool operator<(IntPoint const &p) const {
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński return ( ( _pt[Y] < p[Y] ) ||
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński (( _pt[Y] == p[Y] ) && ( _pt[X] < p[X] )));
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński }
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński /// @}
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński /** @brief Lexicographical ordering functor.
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński * @param d The more significant dimension */
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński template <Dim2 d> struct LexLess;
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński /** @brief Lexicographical ordering functor.
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński * @param d The more significant dimension */
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński template <Dim2 d> struct LexGreater;
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński /** @brief Lexicographical ordering functor with runtime dimension. */
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński struct LexLessRt {
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński LexLessRt(Dim2 d) : dim(d) {}
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński inline bool operator()(IntPoint const &a, IntPoint const &b) const;
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński private:
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński Dim2 dim;
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński };
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński /** @brief Lexicographical ordering functor with runtime dimension. */
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński struct LexGreaterRt {
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński LexGreaterRt(Dim2 d) : dim(d) {}
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński inline bool operator()(IntPoint const &a, IntPoint const &b) const;
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński private:
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński Dim2 dim;
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński };
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński};
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosińskitemplate<> struct IntPoint::LexLess<X> {
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński bool operator()(IntPoint const &a, IntPoint const &b) const {
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński return a[X] < b[X] || (a[X] == b[X] && a[Y] < b[Y]);
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński }
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński};
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosińskitemplate<> struct IntPoint::LexLess<Y> {
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński bool operator()(IntPoint const &a, IntPoint const &b) const {
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński return a[Y] < b[Y] || (a[Y] == b[Y] && a[X] < b[X]);
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński }
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński};
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosińskitemplate<> struct IntPoint::LexGreater<X> {
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński bool operator()(IntPoint const &a, IntPoint const &b) const {
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński return a[X] > b[X] || (a[X] == b[X] && a[Y] > b[Y]);
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński }
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński};
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosińskitemplate<> struct IntPoint::LexGreater<Y> {
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński bool operator()(IntPoint const &a, IntPoint const &b) const {
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński return a[Y] > b[Y] || (a[Y] == b[Y] && a[X] > b[X]);
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński }
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński};
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosińskiinline bool IntPoint::LexLessRt::operator()(IntPoint const &a, IntPoint const &b) const {
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński return dim ? IntPoint::LexLess<Y>()(a, b) : IntPoint::LexLess<X>()(a, b);
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński}
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosińskiinline bool IntPoint::LexGreaterRt::operator()(IntPoint const &a, IntPoint const &b) const {
76addc201c409e81eaaa73fe27cc0f79c4db097cKrzysztof Kosiński return dim ? IntPoint::LexGreater<Y>()(a, b) : IntPoint::LexGreater<X>()(a, b);
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński}
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński} // namespace Geom
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński#endif // !SEEN_GEOM_INT_POINT_H
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński/*
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński Local Variables:
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński mode:c++
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński c-file-style:"stroustrup"
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński indent-tabs-mode:nil
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński fill-column:99
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński End:
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński*/
40742313779ee5e43be93a9191f1c86412cf183bKrzysztof Kosiński// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :