Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
Coordinates
===========
The library uses 2 types of coordinate classes. One for integers and one for
floats, these are called :class:`library.Point2DI` and
:class:`library.Point2D`.
Point2DI
--------
.. class:: library.Point2DI
These points are used for defining the location of tiles, which are used when
placing buildings in Starcraft.
Not many operators are defined for this class, only equality, inequality and
converting to strings are defined.
Point2D
-------
.. class:: library.Point2D
Instances of this class is used to represent the location of Units and
support many operations for general purpose calculation.
Example
~~~~~~~
.. code:: python
from library import Point2D, Point2DI
# Creating two points
p1 = Point2D() # Defaults to (0, 0)
p2 = Point2D(1, 2)
# Adding two points
p1 += p2
# Multiplying a point with a scalar
p2 *= 3
# Creating a new point, defined as the sum of p1 and p2
p3 = p1 + p2
print(p1)
print(p2)
print(p3)
# The same works for Point2DI
pi1 = Point2DI(1, 2)
pi2 = Point2DI(1, 2)
# However, we can only compare them. There are no operators for addition,
# subtraction or multiplication.
print(pi1 == pi2) # prints: True
It is also possible to define custom operators for these points, as shown
below:
.. code:: python
from math import sqrt
from library import Point2D
p1 = Point2D(3, 4)
p2 = Point2D(1, 2)
# Defining a custom @ operator, like this
Point2D.__matmul__ = lambda self, other: self.x * other.x + self.y * other.y
print(p1 @ p2) # prints: 11.0
# Or maybe a distance operator:
Point2D.distance = lambda self, other: sqrt((self.x - other.x)**2 + (self.y - other.y)**2)
print(p1.distance(p2)) # prints: 2.8284...