Prepare 1.0 alpha3 release.
[tagua/yd.git] / src / point.cpp
blobda2535aa558da9524170afa46598a61cf194dafe
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 <QDebug>
13 #include <QString>
14 #include "common.h"
15 #include "point.h"
17 Point::Point(int x, int y)
18 : x(x), y(y) {
22 Point::Point(const QPoint& p)
23 : x(p.x()), y(p.y()) {
27 Point::Point() {
31 Point::Point(const QString& str, int ysize) {
32 x = y = -1;
33 int length = str.length();
34 if(length == 0)
35 return;
36 if(str[0].isLetter()) {
37 char c = str[0].toAscii();
38 if(c >= 'a' && c <= 'z')
39 x = c-'a';
40 else if(c >= 'A' && c <= 'Z')
41 x = c-'A';
42 if(length>1)
43 y = ysize - str.mid(1).toInt();
45 else
46 y = ysize - str.toInt();
49 QString Point::row(int ysize) const {
50 if (y != -1)
51 return QString::number(ysize - y);
52 else
53 return QString();
55 QString Point::numcol(int xsize) const {
56 if (x != -1)
57 return QString::number(xsize - x);
58 else
59 return QString();
62 QString Point::col() const {
63 if (x != -1) {
64 if(x >= 26)
65 return QChar(static_cast<char>(x - 26 + 'A'));
66 else
67 return QChar(static_cast<char>(x + 'a'));
69 else
70 return QString();
72 QString Point::alpharow() const {
73 if (y != -1) {
74 if(y >= 26)
75 return QChar(static_cast<char>(y - 26 + 'A'));
76 else
77 return QChar(static_cast<char>(y + 'a'));
79 else
80 return QString();
83 QString Point::toString(int ysize) const {
84 return col() + row(ysize);
87 Point Point::operator+(const Point& other) const {
88 return Point(x + other.x, y + other.y);
91 Point Point::operator+=(const Point& other) {
92 return *this = *this + other;
95 Point Point::operator-() const {
96 return Point(-x, -y);
99 Point Point::operator-(const Point& other) const {
100 return Point(x - other.x, y - other.y);
103 Point Point::operator*(int n) const {
104 return Point(x * n, y * n);
107 Point Point::operator/(int n) const {
108 return Point(x / n, y / n);
111 Point Point::div(int n) const {
112 return Point(x >= 0 ? x / n : x / n - 1,
113 y >= 0 ? y / n : y / n - 1);
116 bool Point::operator==(const Point& other) const {
117 return x == other.x && y == other.y;
120 bool Point::operator!=(const Point& other) const {
121 return !(*this == other);
124 bool Point::operator<(const Point& other) const {
125 return y < other.y || (y == other.y && x < other.x);
128 bool Point::operator<=(const Point& other) const {
129 return y <= other.y || (y == other.y && x <= other.x);
132 bool Point::resembles(const Point& other) const {
133 return (other.x == -1 || x == other.x) &&
134 (other.y == -1 || y == other.y);
137 Point::operator QPoint() const {
138 return QPoint(x,y);
141 Point Point::normalizeInfinity() const {
142 return Point(
143 normalizeInfinityHelper(x),
144 normalizeInfinityHelper(y)
148 double Point::norm() const {
149 return sqrt((double)(x*x + y*y));
152 int Point::normalizeInfinityHelper(int n) const {
153 if (n == 0)
154 return 0;
155 else
156 return n > 0 ? 1 : -1;
159 QDebug operator<<(QDebug dbg, const Point& p) {
160 dbg << "(" << (p.x == -1 ? QString("?") : QString::number(p.x))
161 << ", " << (p.y == -1 ? QString("?") : QString::number(p.y)) << ")";
162 return dbg;