p5 icon indicating copy to clipboard operation
p5 copied to clipboard

Vector Magnitude is not Callable

Open ian-c-hamilton opened this issue 4 years ago • 2 comments

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 run() File "c:\Users\Ian\anaconda3\lib\site-packages\p5\sketch\userspace.py", line 166, in run app.run() File "c:\Users\Ian\anaconda3\lib\site-packages\vispy\app_default_app.py", line 62, in run return default_app.run() File "c:\Users\Ian\anaconda3\lib\site-packages\vispy\app\application.py", line 152, in run return self._backend._vispy_run() File "c:\Users\Ian\anaconda3\lib\site-packages\vispy\app\backends_glfw.py", line 194, in _vispy_run self._vispy_process_events() File "c:\Users\Ian\anaconda3\lib\site-packages\vispy\app\backends_glfw.py", line 183, in _vispy_process_events timer._tick() File "c:\Users\Ian\anaconda3\lib\site-packages\vispy\app\backends_glfw.py", line 516, in _tick self._vispy_timer._timeout() File "c:\Users\Ian\anaconda3\lib\site-packages\vispy\app\timer.py", line 168, in _timeout count=self.iter_count) File "c:\Users\Ian\anaconda3\lib\site-packages\vispy\util\event.py", line 455, in call self._invoke_callback(cb, event) File "c:\Users\Ian\anaconda3\lib\site-packages\vispy\util\event.py", line 475, in _invoke_callback self, cb_event=(cb, event)) << caught exception here: >> File "c:\Users\Ian\anaconda3\lib\site-packages\vispy\util\event.py", line 471, in _invoke_callback cb(event) File "c:\Users\Ian\anaconda3\lib\site-packages\p5\sketch\base.py", line 110, in on_timer self.draw_method() File "d:/Google Drive/Personal/Programming/The Nature of Code/1 - Vectors/Mouse Following Vector.py", line 24, in draw m = mouse.magnitude() TypeError: 'numpy.float32' object is not callable ERROR: Invoking <bound method Sketch.on_timer of <Sketch (Glfw) at 0x2316032b888>> for Event

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.

ian-c-hamilton avatar Nov 11 '20 19:11 ian-c-hamilton

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. )

tushar5526 avatar Nov 15 '20 15:11 tushar5526

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

villares avatar Dec 25 '20 02:12 villares