Moved global.* to mastersettings.*, and converted many error messages to use the...
[tagua/yd.git] / src / point.cpp
blob0a12f8668fdf48db298e9ddd096af29a05e2acff
1 /*
2 Copyright (c) 2006 Paolo Capriotti <p.capriotti@sns.it>
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 "common.h"
15 #include "point.h"
17 Point::Point(int x, int y)
18 : x(x), y(y)
23 Point::Point(const QPoint& p)
24 : x(p.x()), y(p.y())
29 Point::Point()
34 Point::Point(const QString& str, int ysize) {
35 x = y = -1;
36 int length = str.length();
37 if(length == 0)
38 return;
39 if(str[0].isLetter()) {
40 char c = str[0].toAscii();
41 if(c >= 'a' && c <= 'z')
42 x = c-'a';
43 else if(c >= 'A' && c <= 'Z')
44 x = c-'A';
45 if(length>1)
46 y = ysize - str.mid(1).toInt();
48 else
49 y = ysize - str.toInt();
52 QString Point::row(int ysize) const {
53 if (y != -1)
54 return QString::number(ysize - y);
55 else
56 return QString();
59 QString Point::col() const {
60 if (x != -1) {
61 if(x >= 26)
62 return QChar(static_cast<char>(x - 26 + 'A'));
63 else
64 return QChar(static_cast<char>(x + 'a'));
66 else
67 return QString();
70 QString Point::toString(int ysize) const {
71 return col() + row(ysize);
74 Point Point::operator+(const Point& other) const {
75 return Point(x + other.x, y + other.y);
78 Point Point::operator+=(const Point& other) {
79 return *this = *this + other;
82 Point Point::operator-() const {
83 return Point(-x, -y);
86 Point Point::operator-(const Point& other) const {
87 return Point(x - other.x, y - other.y);
90 Point Point::operator*(int n) const {
91 return Point(x * n, y * n);
94 Point Point::operator/(int n) const {
95 return Point(x / n, y / n);
98 Point Point::div(int n) const {
99 return Point(x >= 0 ? x / n : x / n - 1,
100 y >= 0 ? y / n : y / n - 1);
103 bool Point::operator==(const Point& other) const {
104 return x == other.x && y == other.y;
107 bool Point::operator!=(const Point& other) const
109 return !(*this == other);
112 bool Point::operator<(const Point& other) const
114 return y < other.y || (y == other.y && x < other.x);
117 bool Point::operator<=(const Point& other) const
119 return y <= other.y || (y == other.y && x <= other.x);
122 bool Point::resembles(const Point& other) const
124 return (other.x == -1 || x == other.x) &&
125 (other.y == -1 || y == other.y);
128 Point::operator QPoint() const
130 return QPoint(x,y);
133 Point Point::normalizeInfinity() const
135 return Point(
136 normalizeInfinityHelper(x),
137 normalizeInfinityHelper(y)
141 double Point::norm() const
143 return sqrt((double)(x*x + y*y));
146 int Point::normalizeInfinityHelper(int n) const
148 if (n == 0)
149 return 0;
150 else
151 return n > 0 ? 1 : -1;
154 std::ostream& operator<<(std::ostream& os, const Point& p)
156 return os << "(" << (p.x == -1 ? QString("?") : QString::number(p.x))
157 << ", " << (p.y == -1 ? QString("?") : QString::number(p.y)) << ")";