Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
PyCommandCenter v2
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Jonas Bonnaudet
PyCommandCenter v2
Commits
0a55a6f1
Commit
0a55a6f1
authored
6 years ago
by
David Bergström
Browse files
Options
Downloads
Patches
Plain Diff
Add tutorial on coordinates
parent
97102d78
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
docs/coordinates.rst
+83
-0
83 additions, 0 deletions
docs/coordinates.rst
docs/index.rst
+1
-0
1 addition, 0 deletions
docs/index.rst
with
84 additions
and
0 deletions
docs/coordinates.rst
0 → 100644
+
83
−
0
View file @
0a55a6f1
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...
This diff is collapsed.
Click to expand it.
docs/index.rst
+
1
−
0
View file @
0a55a6f1
...
...
@@ -14,6 +14,7 @@ This is a Python library based on the CommandCenter bot for implementing bots fo
idabot
unit
types
coordinates
constants
Short overview
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment