Removed unimplemented GUI actions.
[tagua/yd.git] / src / point.cpp
blob16260a00666cd24c840acf102a17739b2240cb08
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 "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();
58 QString Point::numcol(int xsize) const {
59 if (x != -1)
60 return QString::number(xsize - x);
61 else
62 return QString();
65 QString Point::col() const {
66 if (x != -1) {
67 if(x >= 26)
68 return QChar(static_cast<char>(x - 26 + 'A'));
69 else
70 return QChar(static_cast<char>(x + 'a'));
72 else
73 return QString();
75 QString Point::alpharow() const {
76 if (y != -1) {
77 if(y >= 26)
78 return QChar(static_cast<char>(y - 26 + 'A'));
79 else
80 return QChar(static_cast<char>(y + 'a'));
82 else
83 return QString();
86 QString Point::toString(int ysize) const {
87 return col() + row(ysize);
90 Point Point::operator+(const Point& other) const {
91 return Point(x + other.x, y + other.y);
94 Point Point::operator+=(const Point& other) {
95 return *this = *this + other;
98 Point Point::operator-() const {
99 return Point(-x, -y);
102 Point Point::operator-(const Point& other) const {
103 return Point(x - other.x, y - other.y);
106 Point Point::operator*(int n) const {
107 return Point(x * n, y * n);
110 Point Point::operator/(int n) const {
111 return Point(x / n, y / n);
114 Point Point::div(int n) const {
115 return Point(x >= 0 ? x / n : x / n - 1,
116 y >= 0 ? y / n : y / n - 1);
119 bool Point::operator==(const Point& other) const {
120 return x == other.x && y == other.y;
123 bool Point::operator!=(const Point& other) const
125 return !(*this == other);
128 bool Point::operator<(const Point& other) const
130 return y < other.y || (y == other.y && x < other.x);
133 bool Point::operator<=(const Point& other) const
135 return y <= other.y || (y == other.y && x <= other.x);
138 bool Point::resembles(const Point& other) const
140 return (other.x == -1 || x == other.x) &&
141 (other.y == -1 || y == other.y);
144 Point::operator QPoint() const
146 return QPoint(x,y);
149 Point Point::normalizeInfinity() const
151 return Point(
152 normalizeInfinityHelper(x),
153 normalizeInfinityHelper(y)
157 double Point::norm() const
159 return sqrt((double)(x*x + y*y));
162 int Point::normalizeInfinityHelper(int n) const
164 if (n == 0)
165 return 0;
166 else
167 return n > 0 ? 1 : -1;
170 std::ostream& operator<<(std::ostream& os, const Point& p)
172 return os << "(" << (p.x == -1 ? QString("?") : QString::number(p.x))
173 << ", " << (p.y == -1 ? QString("?") : QString::number(p.y)) << ")";