Skip to content

Commit 07b52e0

Browse files
authored
Merge pull request #796 from jf---/compas-795
fixes #795 Unetary support for vectors
2 parents cd878ec + 644ee0c commit 07b52e0

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010

1111
### Added
1212

13+
* Added a `invert` and `inverted` method `compas.geometry.Vector`.
14+
* Added unetary `__neg__` operator for `compas.geometry.Vector`
15+
1316
### Changed
1417

1518
### Removed

src/compas/geometry/primitives/vector.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,9 @@ def __pow__(self, n):
255255
"""
256256
return Vector(self.x ** n, self.y ** n, self.z ** n)
257257

258+
def __neg__(self):
259+
return self.scaled(-1.0)
260+
258261
def __iadd__(self, other):
259262
"""Add the components of the other vector to this vector.
260263
@@ -676,6 +679,41 @@ def unitized(self):
676679
v.unitize()
677680
return v
678681

682+
def invert(self):
683+
"""Invert the direction of this vector
684+
685+
Note
686+
____
687+
a negation of a vector is similar to inverting a vector
688+
689+
Examples
690+
--------
691+
>>> u = Vector(1.0, 0.0, 0.0)
692+
>>> v = u.copy()
693+
>>> u.invert()
694+
>>> u == v
695+
False
696+
>>> u.invert()
697+
>>> u == v
698+
True
699+
>>> v == --v
700+
True
701+
"""
702+
self.scale(-1.)
703+
704+
def inverted(self):
705+
"""Returns a inverted copy of this vector
706+
707+
Examples
708+
--------
709+
>>> u = Vector(1.0, 0.0, 0.0)
710+
>>> v = u.inverted()
711+
>>> w = u + v
712+
>>> w.length
713+
0.0
714+
"""
715+
return self.scaled(-1.)
716+
679717
def scale(self, n):
680718
"""Scale this vector by a factor n.
681719

0 commit comments

Comments
 (0)