p5
p5 copied to clipboard
Vector Magnitude is not Callable
Describe the bug Docs have vector magnitude function as "Vector.mag()". Currently implemented as "Vector.magnitude". When adding the additional "()" like all other vector functions, an error is given. See below.
To Reproduce Run code:
from p5 import *
def setup():
size(200, 200)
def draw():
background(200)
# Two vectors, one for the mouse location and one of the center
# of the window
mouse = Vector(mouse_x, mouse_y)
center = Vector(width / 2, height / 2)
# Vector subtraction!
mouse = mouse - center
# The magnitude (i.e., the length) of a vector can be accessed by
# the mag() function. Here it is used as the width of a rectangle
# drawn at the top of the window.
m = mouse.magnitude()
fill(0)
rect((0, 0), m, 10)
# Draw a line to represent the vector
translate(center.x, center.y)
line((0, 0), (mouse.x, mouse.y))
if __name__ == '__main__':
run()
Expected behavior Should calculate magnitude.
Error Traceback
WARNING: Traceback (most recent call last):
File "d:/Google Drive/Personal/Programming/The Nature of Code/1 - Vectors/Mouse Following Vector.py", line 34, in
System information:
- p5 release (version number or latest commit): 0.7.0
- Python version: 3.7.6 Anaconda
- Operating system: Windows 10
Additional context Add any other context about the problem here.
https://github.com/p5py/p5/blob/master/p5/pmath/vector.py#L327
Use mouse.magnitude instead of mouse.magnitude() as magnitude is defined as a property of the class Vector.
( If you know C++ / Java think of magnitude as a variable name instead of a function of the class Vector. )
couldn't we also have .mag()?
Perhaps adding something like:
def mag(self):
return self.magnitude
Just for more compatibility with the Processing PVector class? https://py.processing.org/reference/PVector_mag.html