# Vector implementation

• April 19th, 2012, 11:20 AM
poolman81
Vector implementation
Hi,

I'm currently studying Java and have an assignment to create a pool game. I have the bulk of the work done but I am having issues with implementing 2d vectors. I have not been shown this and am trying to work my way through it but cannot find anything which gives suitable explanations for the 2d vector which I am trying to create. I have written a vector class which I have displayed below but need to implement it in my main class. I need to divide the table into a number of squares of my choosing. I then must perform addition of the vectors and determine the new position of the ball objects. I hope someone can help me as I am at a loss at the moment. Also if there is anything else which I should add suggestions will be greatly appreciated. If anyone wants to look at my other classes then they are more than welcome to.

poolMan81

Code :

```import java.awt.*; import java.util.*;   //Vector class which contains the code for performing the collisions between the balls public class Vector2 {   private double x, y;   public Vector2(double x, double y){   this.x = x; this.y = y;   }   public double getX(){   return this.x;   }   public double getY(){   return this.y;   }   public void setVector2(double x, double y) {   this.x = x; this.y = y;   }   public double magnitude(){   return Math.sqrt(x * x + y * y);   }   public static Vector2 addVector (Vector2 a, Vector2 b) {   double sumX = a.getX() + b.getX(); double sumY = a.getY() + b.getY(); return new Vector2 (sumX, sumY);   }   public static Vector2 scaleVector(double scale, Vector2 d) {   return new Vector2 (scale * d.getX(), scale * d.getY());   }```
• April 19th, 2012, 11:34 AM
KevinWorkman
Re: Vector implementation
I'm honestly not sure what your actual question is. You might consider posting an SSCCE that demonstrates exactly what problem you're encountering.
• April 19th, 2012, 12:02 PM
poolman81
Re: Vector implementation
Sorry about that. My question really is how do I go about implementing this code within my main and or other classes in order to divide my table into vectors? These vectors will then be used to determine the position of the balls before the next shot is taken. I have attached my code which contains 3 classes. Please ignore the comments as I have to enter them for my project.
• April 19th, 2012, 12:05 PM
KevinWorkman
Re: Vector implementation
People are pretty wary of downloading zip files like that. An SSCCE should be able to be copied and pasted into our own editors so we can see what you're talking about.

But I would expect you would have to create instances of your Vector class, passing in the appropriate information. Recommended reading: Creating Objects (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
• April 19th, 2012, 12:11 PM
Norm
Re: Vector implementation
Quote:

2d vector which I am trying to create.
Can you explain what a two dimensional vector is? Would it be the same as the Java SE Vector class except it would take two index values in place of one? For example:
public E get(int index1, index2)
public boolean add(int index, E e)
• April 19th, 2012, 12:24 PM
poolman81
Re: Vector implementation
Thanks that's a great document. I understand how to give the initial coordinates but how do you create the size of each square and the amount of squares on the plane? Is this done individually with the add() method? Sorry if this seems like a stupid question but I've never used vectors before so just trying to get my head around it.

• April 19th, 2012, 12:29 PM
poolman81
Re: Vector implementation
Yes a 2d vector takes to values. The x and y coordinates of an object. As for the similarities of the 2 I can't say as I'm new to vectors myself.
• April 19th, 2012, 12:31 PM
Norm
Re: Vector implementation
You definition of vector seems closer to that of a point. The x,y location of something.
• April 19th, 2012, 12:36 PM
poolman81
Re: Vector implementation
Yeah a point can be used too for my problem but vectors were suggested by my lecturer.
• April 19th, 2012, 12:43 PM
Norm
Re: Vector implementation
I guess you can call it whatever you want.
• June 3rd, 2012, 06:28 AM
erdy_rezki
Re: Vector implementation
owrite lets gonna change
• June 3rd, 2012, 06:45 AM
erdy_rezki
Re: Vector implementation
it work bro