new 4475edb243ed4627f4c5f2c470ca40b3def034d4
[tagua/yd.git] / src / core / point.cpp
blob9bfeeae9e60f4906333eba1e826e550a8d5e7259
1 /*
2 Copyright (c) 2006 Paolo Capriotti <p.capriotti@gmail.com>
3 (c) 2006 Maurizio Monge <maurizio.monge@kdemail.net>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9 */
11 #include <cmath>
12 #include <iostream>
13 #include <QString>
14 #include "point.h"
16 Point::Point(int x, int y)
17 : x(x), y(y)
22 Point::Point(const QPoint& p)
23 : x(p.x()), y(p.y())
28 Point::Point()
33 Point::Point(const QString& str, int ysize) {
34 x = y = -1;
35 int length = str.length();
36 if(length == 0)
37 return;
38 if(str[0].isLetter()) {
39 char c = str[0].toAscii();
40 if(c >= 'a' && c <= 'z')
41 x = c-'a';
42 else if(c >= 'A' && c <= 'Z')
43 x = c-'A';
44 if(length>1)
45 y = ysize - str.mid(1).toInt();
47 else
48 y = ysize - str.toInt();
51 QString Point::row(int ysize) const {
52 if (y != -1)
53 return QString::number(ysize - y);
54 else
55 return QString();
57 QString Point::numcol(int xsize) const {
58 if (x != -1)
59 return QString::number(xsize - x);
60 else
61 return QString();
64 QString Point::col() const {
65 if (x != -1) {
66 if(x >= 26)
67 return QChar(static_cast<char>(x - 26 + 'A'));
68 else
69 return QChar(static_cast<char>(x + 'a'));
71 else
72 return QString();
74 QString Point::alpharow() const {
75 if (y != -1) {
76 if(y >= 26)
77 return QChar(static_cast<char>(y - 26 + 'A'));
78 else
79 return QChar(static_cast<char>(y + 'a'));
81 else
82 return QString();
85 QString Point::toString(int ysize) const {
86 return col() + row(ysize);
89 Point Point::operator+(const Point& other) const {
90 return Point(x + other.x, y + other.y);
93 Point Point::operator+=(const Point& other) {
94 return *this = *this + other;
97 Point Point::operator-() const {
98 return Point(-x, -y);
101 Point Point::operator-(const Point& other) const {
102 return Point(x - other.x, y - other.y);
105 Point Point::operator*(int n) const {
106 return Point(x * n, y * n);
109 Point Point::operator/(int n) const {
110 return Point(x / n, y / n);
113 Point Point::div(int n) const {
114 return Point(x >= 0 ? x / n : x / n - 1,
115 y >= 0 ? y / n : y / n - 1);
118 bool Point::operator==(const Point& other) const {
119 return x == other.x && y == other.y;
122 bool Point::operator!=(const Point& other) const
124 return !(*this == other);
127 bool Point::operator<(const Point& other) const
129 return y < other.y || (y == other.y && x < other.x);
132 bool Point::operator<=(const Point& other) const
134 return y <= other.y || (y == other.y && x <= other.x);
137 bool Point::resembles(const Point& other) const
139 return (other.x == -1 || x == other.x) &&
140 (other.y == -1 || y == other.y);
143 Point::operator QPoint() const
145 return QPoint(x,y);
148 Point Point::normalizeInfinity() const
150 return Point(
151 normalizeInfinityHelper(x),
152 normalizeInfinityHelper(y)
156 double Point::norm() const
158 return sqrt((double)(x*x + y*y));
161 int Point::normalizeInfinityHelper(int n) const
163 if (n == 0)
164 return 0;
165 else
166 return n > 0 ? 1 : -1;
169 QDebug operator<<(QDebug dbg, const Point& p)
171 dbg << "(" << (p.x == -1 ? "?" : qPrintable(QString::number(p.x)))
172 << ", " << (p.y == -1 ? "?" : qPrintable(QString::number(p.y))) << ")";
173 return dbg;