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.
17 Point::Point(int x
, int y
)
22 Point::Point(const QPoint
& p
)
23 : x(p
.x()), y(p
.y()) {
31 Point::Point(const QString
& str
, int ysize
) {
33 int length
= str
.length();
36 if(str
[0].isLetter()) {
37 char c
= str
[0].toAscii();
38 if(c
>= 'a' && c
<= 'z')
40 else if(c
>= 'A' && c
<= 'Z')
43 y
= ysize
- str
.mid(1).toInt();
46 y
= ysize
- str
.toInt();
49 QString
Point::row(int ysize
) const {
51 return QString::number(ysize
- y
);
55 QString
Point::numcol(int xsize
) const {
57 return QString::number(xsize
- x
);
62 QString
Point::col() const {
65 return QChar(static_cast<char>(x
- 26 + 'A'));
67 return QChar(static_cast<char>(x
+ 'a'));
72 QString
Point::alpharow() const {
75 return QChar(static_cast<char>(y
- 26 + 'A'));
77 return QChar(static_cast<char>(y
+ 'a'));
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 {
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 {
141 Point
Point::normalizeInfinity() const {
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 {
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
)) << ")";