I have to write a 2D Vector class in Java, that supports the operation of the vector addition subtraction, scalar multiplication, and division and taking the length and unit vector, where each method returns a new Vector.
I would be grateful if someone could provide a resource link or write this formulas.
Hm… I might be wrong, but the only thing I don’t know is scalar.
public class vec2 {
public float x, y;
public vec2(float x, float y) {
this.x = x;
this.y = y;
}
public int getX() {
return (int) x;
}
public int getY() {
return (int) y;
}
public vec2 multiply(float mult) {
return new vec2(x * mult, y * mult);
}
public vec2 devide(float dev) {
return new vec2(x / dev, y / dev);
}
public vec2 multiply(vec2 vec) {
return new vec2(x * vec.x, y * vec.y);
}
public vec2 devide(vec2 vec) {
return new vec2(x / vec.x, y / vec.y);
}
}
No, pls don’t provide him with resources. Not that there aren’t any vector classes just a single mouse click away on the internez. This such a trivial “problem” that you should just write them on you own, it will cost you 5min of your life.
I knew formulas for those :), but im not sure how to write for a unit vector ?
I think this is formula for scalar multiplication
public Vector2D scalarMulti(Vector2D vector, double scalar) {
return new Vector2D(
vector.x * scalar,
vector.y * scalar
);
}
So unit vector is vector with length of 1?
Yup.
What do u mean by that ? Should I multiply with 1 ?
Try this :
//in Vector2
public void add(Vector2 addWith) {
boolean hasTriedWikipidea = false;
boolean hasTriedGoogle = false;
while(true) {
if (!hasTriedWikipidea)
tryWikipidea();
else if (!hasTriedGoogle)
tryGoogle();
else
break;
}
tryJGO();
}
Basically you to pithagorean theory and you get 1.
So for red one: 00+11=1;
Green one: 11 + 00 = 1;
Blue: 0.7*0.7+0.7+0.7=1; (Not really 0.7, sin(45)=0.707(…))
So my method should be boolean and check if it is positive or what ?
No idea. You just said something about unit vectors, you didn’t mention what you need to do.
I didn’t understand also what they mean by that,
[quote]2D Vector class in Java, that supports the operation of the vector addition subtraction, scalar multiplication, and division and taking the length and unit vector
[/quote]
‘Making the unit vector’ is also called normalization that might help you with google-ing
I’ve seen many formulas, I even rememeber one like this for example:
length = sqrt((ax * ax) + (ay * ay) + (az * az))
but im not sure if it is something like this for 2D:
sqrt(x * x + y * y)
Maybe normalize? What that does is take any vector and convert it to length 1 vector.
I had found a good article about what it means, but can’t find it, so gonna write it here.
To normalize a vector, first find the length of current vector.
float length = sqrt(vec.x*vec.x + vec.y*vec.y);
Now that you have length of the vector, divide each vector component by the length.
vec2 newvec = new vec2(vec.x/length, vec.y/length);
If im not mistaking, you would get a vec of length 1.
That’s it, thanks!