Skip to content

Would like a safe version of vectorops set_magnitude #17

@rchristie

Description

@rchristie

set_magnitude gives a division by zero if v is a zero vector:

def set_magnitude(v, mag):
    """
    return: Vector v with magnitude set to mag.
    """
    scale = mag / magnitude(v)
    return mult(v, scale)

Could either change the above to the following or make it a separate function set_magnitude_zero():

old_mag = magnitude(v)
if old_mag > 0.0:
    return mult(v, mag / old_mag)
return copy.copy(v)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions