Java-Gaming.org Hi !
 Featured games (90) games approved by the League of Dukes Games in Showcase (754) Games in Android Showcase (229) games submitted by our members Games in WIP (842) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 vector formulas  (Read 3360 times) 0 Members and 1 Guest are viewing this topic.
rikqt

Senior Newbie

 « Posted 2014-03-26 12:28:17 »

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.
trollwarrior1
 « Reply #1 - Posted 2014-03-26 12:35:08 »

Hm.. I might be wrong, but the only thing I don't know is scalar.

 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34 `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);   }}`
Danny02
 « Reply #2 - Posted 2014-03-26 12:36:01 »

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

Senior Newbie

 « Reply #3 - Posted 2014-03-26 12:39:19 »

I knew formulas for those , but im not sure how to write for a unit vector ?

I think this is formula for scalar multiplication
 1  2  3  4  5  6 `public Vector2D scalarMulti(Vector2D vector, double scalar) {        return new Vector2D(                vector.x * scalar,                vector.y * scalar        );    }`
Danny02
 « Reply #4 - Posted 2014-03-26 12:43:16 »

here have spoon full of delicious wikipedia
trollwarrior1
 « Reply #5 - Posted 2014-03-26 12:47:37 »

So unit vector is vector with length of 1?
actual

JGO Coder

Medals: 25

 « Reply #6 - Posted 2014-03-26 12:50:49 »

Yup.
rikqt

Senior Newbie

 « Reply #7 - Posted 2014-03-26 13:07:08 »

What do u mean by that ? Should I multiply with 1 ?
saucymeatman
 « Reply #8 - Posted 2014-03-26 13:16:28 »

Try this :

 1  2  3  4  5  6  7  8  9  10  11  12  13  14 `//in Vector2public void add(Vector2 addWith) {boolean hasTriedWikipidea = false;boolean hasTriedGoogle = false;while(true) {if (!hasTriedWikipidea)     tryWikipidea();else if (!hasTriedGoogle)     tryGoogle();else     break;}tryJGO();}`
trollwarrior1
 « Reply #9 - Posted 2014-03-26 13:16:49 »

Basically you to pithagorean theory and you get 1.

So for red one: 0*0+1*1=1;
Green one: 1*1 + 0*0 = 1;
Blue: 0.7*0.7+0.7+0.7=1; (Not really 0.7, sin(45)=0.707(....))
rikqt

Senior Newbie

 « Reply #10 - Posted 2014-03-26 13:19:54 »

Basically you to pithagorean theory and you get 1.

So for red one: 0*0+1*1=1;
Green one: 1*1 + 0*0 = 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 ?
trollwarrior1
 « Reply #11 - Posted 2014-03-26 13:25:34 »

No idea. You just said something about unit vectors, you didn't mention what you need to do.
rikqt

Senior Newbie

 « Reply #12 - Posted 2014-03-26 13:28:23 »

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
matheus23

JGO Kernel

Medals: 138
Projects: 3

You think about my Avatar right now!

 « Reply #13 - Posted 2014-03-26 13:30:57 »

See my:
My development Blog:     | Or look at my RPG | Or simply my coding
http://matheusdev.tumblr.comRuins of Revenge  |      On Github
rikqt

Senior Newbie

 « Reply #14 - Posted 2014-03-26 13:33:47 »

I've seen many formulas, I even rememeber one like this for example:

 1 ` length = sqrt((ax * ax) + (ay * ay) + (az * az)) `

but im not sure if it is something like this for 2D:

 1 `sqrt(x * x + y * y)`
trollwarrior1
 « Reply #15 - Posted 2014-03-26 13:35:31 »

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.

 1 `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.

 1 `vec2 newvec = new vec2(vec.x/length, vec.y/length);`

If im not mistaking, you would get a vec of length 1.
rikqt

Senior Newbie

 « Reply #16 - Posted 2014-03-26 13:37:14 »

That's it, thanks!
Pages: [1]
 ignore  |  Print

 DesertCoockie (20 views) 2018-05-13 18:23:11 nelsongames (68 views) 2018-04-24 18:15:36 nelsongames (65 views) 2018-04-24 18:14:32 ivj94 (748 views) 2018-03-24 14:47:39 ivj94 (79 views) 2018-03-24 14:46:31 ivj94 (595 views) 2018-03-24 14:43:53 Solater (95 views) 2018-03-17 05:04:08 nelsongames (168 views) 2018-03-05 17:56:34 Gornova (378 views) 2018-03-02 22:15:33 buddyBro (1038 views) 2018-02-28 16:59:18
 Java Gaming Resourcesby philfrei2017-12-05 19:38:37Java Gaming Resourcesby philfrei2017-12-05 19:37:39Java Gaming Resourcesby philfrei2017-12-05 19:36:10Java Gaming Resourcesby philfrei2017-12-05 19:33:10List of Learning Resourcesby elect2017-03-13 14:05:44List of Learning Resourcesby elect2017-03-13 14:04:45SF/X Librariesby philfrei2017-03-02 08:45:19SF/X Librariesby philfrei2017-03-02 08:44:05
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org