Skip to content

Conversation

PlebusSupremus1234
Copy link

In this PR, I added two new methods: isParallelTo and isPerpendicularTo.

As the names suggest, they check whether the vector is parallel or perpendicular to another vector.

Comment on lines +1273 to +1280
if (vec2.x === 0) return this.x === 0;
if (vec2.y === 0) return this.y === 0;

var kx = this.x / vec2.x;
var ky = this.y / vec2.y;

if (kx !== ky || ky !== 0) return false;
else return true;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the current code will always return false unless vec2.y === Infinity

the better way to do this and avoid division:

if you put the vectors as columns of a matrix, the columns are parallel if the matrix is linearly dependent, so we can just check if the determinant of the matrix is 0. this gives x1 * y2 - x2 * y1 = 0 which can be rewritten as x1 * y2 = x2 * y1. this also covers the edge case you have at the top because multiplying by 0 will just give 0.

Suggested change
if (vec2.x === 0) return this.x === 0;
if (vec2.y === 0) return this.y === 0;
var kx = this.x / vec2.x;
var ky = this.y / vec2.y;
if (kx !== ky || ky !== 0) return false;
else return true;
return this.x * vec2.y === this.y * vec2.x;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants