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
)
23 Point::Point(const QPoint
& p
)
34 Point::Point(const QString
& str
, int ysize
) {
36 int length
= str
.length();
39 if(str
[0].isLetter()) {
40 char c
= str
[0].toAscii();
41 if(c
>= 'a' && c
<= 'z')
43 else if(c
>= 'A' && c
<= 'Z')
46 y
= ysize
- str
.mid(1).toInt();
49 y
= ysize
- str
.toInt();
52 QString
Point::row(int ysize
) const {
54 return QString::number(ysize
- y
);
58 QString
Point::numcol(int xsize
) const {
60 return QString::number(xsize
- x
);
65 QString
Point::col() const {
68 return QChar(static_cast<char>(x
- 26 + 'A'));
70 return QChar(static_cast<char>(x
+ 'a'));
75 QString
Point::alpharow() const {
78 return QChar(static_cast<char>(y
- 26 + 'A'));
80 return QChar(static_cast<char>(y
+ 'a'));
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 {
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
149 Point
Point::normalizeInfinity() const
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
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
)) << ")";