alexbol99 / flatten-js

Javascript library for 2d geometry
MIT License
535 stars 56 forks source link

Vector.add(Point) should return a point not a vector #167

Closed DonaldDuck313 closed 3 months ago

DonaldDuck313 commented 4 months ago

If I run the following code in the Javascript console:

new Vector(0, -1).add(new Point(5, 5))

It returns

Vector {x: 5, y: 4}

I would expect it to return a point. I need to translate a point by a vector and get a new point, how do I do that?

Also, in general, functions for converting between points and vectors would be very useful. It would be nice if you could add a toPoint method to the Vector class or something equivalent.

edemaine commented 4 months ago

I'm guessing new Point(5, 5).add(new Vector(0, -1)) will do what you what. I agree that the order probably shouldn't matter though; a point plus a vector should be a point.

alexbol99 commented 4 months ago

Function add in class Vector is supposed to summarize two vectors. It defined as following

add(v: Vector): Vector;

If you want to translate point by vector, use translate method of point:

const translated_point = new Point(5, 5).translate(new Vector(0, -1))

The result will be a Point

DonaldDuck313 commented 4 months ago

If you want to translate point by vector, use translate method of point:

const translated_point = new Point(5, 5).translate(new Vector(0, -1))

The result will be a Point

Thanks, I didn't know about that. It's not in the documentation for the Point class, could you please add it?

alexbol99 commented 4 months ago

Translate method and other transformations are part of the Shape base class, all classes are inherited from it. So in the docs they are also in Shape, because docs are auto generated.