# Working with vectors

Page 1 of 1

## 14 Replies - 954 Views - Last Post: 10 March 2010 - 07:26 PMRate Topic: 3 Votes //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'https://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=161195&amp;s=c4905c5ad59ac93cf5ff250a2a3adb44&md5check=' + ipb.vars['secure_hash'], cur_rating: 2, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Java Student

• D.I.C Regular

Reputation: 21
• Posts: 498
• Joined: 05-February 10

# Working with vectors

Posted 10 March 2010 - 04:17 PM

Im woundering if theres a way to work with a vector of an Image
OR Rectangle?
Take a look a this attachment and get a better picture at what im trying to
achieve.

#### Attached image(s)

Is This A Good Question/Topic? 0

## Replies To: Working with vectors

### #2 pbl

• There is nothing you can't do with a JTable

Reputation: 8378
• Posts: 31,956
• Joined: 06-March 08

## Re: Working with vectors

Posted 10 March 2010 - 04:34 PM

And what do you want to put in your Vector ?
The pixels ? Subimages ?

### #3 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12297
• Posts: 45,399
• Joined: 27-December 08

## Re: Working with vectors

Posted 10 March 2010 - 04:41 PM

@pbl: I think the OP is referring to a mathematical vector (direction and magnitude), not a java.util.Vector.

@Java Student: You can use the repaint() method inherited from Component in combination with a javax.swing.Timer to make animations. Basically, animations come down to modifying an image's position, redrawing it, and repainting. I know if you check out the Java Tutorials section, there are some good animation tutorials. I know both WolfCoder and pbl have especially good tutorials.

### #4 Java Student

• D.I.C Regular

Reputation: 21
• Posts: 498
• Joined: 05-February 10

## Re: Working with vectors

Posted 10 March 2010 - 04:51 PM

The vector as in if i wanted to do something like...
```if (obstacle.topvector collides with sprite.bottomvector) {
// then such and such....
}

```

@pbl Not that kind of vector =P
@macosxnerd101 Vector as in dir and magnitude, yes =)

Woundering if theres a method in Java to do this sort of thing?
i'v heard of it being done in c++

### #5 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12297
• Posts: 45,399
• Joined: 27-December 08

## Re: Working with vectors

Posted 10 March 2010 - 05:03 PM

You should take a look into using the java.awt.geom package, as it has tools you'll find useful. In glancing through this package, I think the Area class might be what you're looking for (at least in part), b/c it has the getBounds() and intersects() methods.

http://72.5.124.55/j...ge-summary.html

### #6 zim1985

• Grand Inquisitor

Reputation: 75
• Posts: 568
• Joined: 19-February 10

## Re: Working with vectors

Posted 10 March 2010 - 05:13 PM

I found this online....it might be helpful:
```public class Vector {

private int N;               // length of the vector
private double[] data;       // array of vector's components

// create the zero vector of length n
public Vector(int n) {
N = n;
data = new double[N];
}

// create a vector from an array
public Vector(double[] d) {
N = d.length;

// defensive copy so that client can't alter our copy of data[]
data = new double[N];
for (int i = 0; i < N; i++)
data[i] = d[i];
}

// create a vector from either an array or a vararg list
// this constructor uses Java's vararg syntax to support
// a constructor that takes a variable number of arguments, such as
// Vector x = new Vector(1.0, 2.0, 3.0, 4.0);
// Vector y = new Vector(5.0, 2.0, 4.0, 1.0);
/*
public Vector(double... d) {
N = d.length;

// defensive copy so that client can't alter our copy of data[]
data = new double[N];
for (int i = 0; i < N; i++)
data[i] = d[i];
}
*/
// return the length of the vector
public int length() {
return N;
}

// return the inner product of this Vector a and b
public double dot(Vector that) {
if (this.N != that.N) throw new RuntimeException("Dimensions don't agree");
double sum = 0.0;
for (int i = 0; i < N; i++)
sum = sum + (this.data[i] * that.data[i]);
return sum;
}

// return the Euclidean norm of this Vector
public double magnitude() {
return Math.sqrt(this.dot(this));
}

// return the Euclidean distance between this and that
public double distanceTo(Vector that) {
if (this.N != that.N) throw new RuntimeException("Dimensions don't agree");
return this.minus(that).magnitude();
}

// return this + that
public Vector plus(Vector that) {
if (this.N != that.N) throw new RuntimeException("Dimensions don't agree");
Vector c = new Vector(N);
for (int i = 0; i < N; i++)
c.data[i] = this.data[i] + that.data[i];
return c;
}

// return this + that
public Vector minus(Vector that) {
if (this.N != that.N) throw new RuntimeException("Dimensions don't agree");
Vector c = new Vector(N);
for (int i = 0; i < N; i++)
c.data[i] = this.data[i] - that.data[i];
return c;
}

// return the corresponding coordinate
public double cartesian(int i) {
return data[i];
}

// create and return a new object whose value is (this * factor)
public Vector times(double factor) {
Vector c = new Vector(N);
for (int i = 0; i < N; i++)
c.data[i] = factor * data[i];
return c;
}

// return the corresponding unit vector
public Vector direction() {
if (this.magnitude() == 0.0) throw new RuntimeException("Zero-vector has no direction");
return this.times(1.0 / this.magnitude());
}

// return a string representation of the vector
public String toString() {
String s = "";
for (int i = 0; i < N; i++)
s = s + data[i] + " ";
return s;
}

// test client
public static void main(String[] args) {
double[] xdata = { 1.0, 2.0, 3.0, 4.0 };
double[] ydata = { 5.0, 2.0, 4.0, 1.0 };
Vector x = new Vector(xdata);
Vector y = new Vector(ydata);

System.out.println("   x       = " + x);
System.out.println("   y       = " + y);

Vector z = x.plus(y);
System.out.println("   z       = " + z);

z = z.times(10.0);
System.out.println(" 10z       = " + z);

System.out.println("  |x|      = " + x.magnitude());
System.out.println(" <x, y>    = " + x.dot(y));
System.out.println("dist(x, y) = " + x.distanceTo(y));
System.out.println("dir(x)     = " + x.direction());

}
}

```

Vector.java

This post has been edited by zim1985: 10 March 2010 - 05:13 PM

### #7 Dogstopper

Reputation: 2965
• Posts: 11,222
• Joined: 15-July 08

## Re: Working with vectors

Posted 10 March 2010 - 05:19 PM

In addition, you could actually use a Rectangle to do collision for you. It's actually really easy. If you have a bounding box around each figure (as in the figure and the box, all you have to do is call rectangle.isColliding(otherRect). If that is true, then they collide. That shortness the code quite significantly.

Here is a code snippet that I took from my tutorial:
```        // box is this bounding rectangle, other.getBounds()
// grabs the other rectangle.
// Then it is a simple matter of testing intersections...
public boolean collides(Ball other) {
if (box.intersects(other.getBounds()))
return true;
else
return false;
}

```

Here is the full tutorial:
http://www.dreaminco...&showentry=2189

This post has been edited by Dogstopper: 10 March 2010 - 05:19 PM
Reason for edit:: broken tag

### #8 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12297
• Posts: 45,399
• Joined: 27-December 08

## Re: Working with vectors

Posted 10 March 2010 - 05:19 PM

Wrong type of Vector. The OP is asking about direction and magnitude vector (you may have seen it in physics, Precalc or Calculus BC if you've had those classes), not dynamic array Vector.

### #9 zim1985

• Grand Inquisitor

Reputation: 75
• Posts: 568
• Joined: 19-February 10

## Re: Working with vectors

Posted 10 March 2010 - 05:26 PM

macosxnerd101, on 10 March 2010 - 03:19 PM, said:

Wrong type of Vector. The OP is asking about direction and magnitude vector (you may have seen it in physics, Precalc or Calculus BC if you've had those classes), not dynamic array Vector.

That Vector.java thing calculates magnitude and direction and such. I'm in Calculus AB, so I know what a Vector is...that said, I'm pretty sure that's it.

### #10 Java Student

• D.I.C Regular

Reputation: 21
• Posts: 498
• Joined: 05-February 10

## Re: Working with vectors

Posted 10 March 2010 - 05:28 PM

@Dogstopper Yeah i used that for a different game but i want to go a little
more indepth and deal with each 4 vectors of a rectangle to explore different
options when making games
@macosxnerd101 java.awt.geom package has alot of interusting methods in it
thanks for pointing that out!

But yes, dealing with each individual vector is what im trying to achieve

This post has been edited by Java Student: 10 March 2010 - 05:32 PM

### #11 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 12297
• Posts: 45,399
• Joined: 27-December 08

## Re: Working with vectors

Posted 10 March 2010 - 05:30 PM

My mistake. I saw an array inside the class, and I assumed dynamic array. I re-read and you're absolutely right.

### #12 Dogstopper

Reputation: 2965
• Posts: 11,222
• Joined: 15-July 08

## Re: Working with vectors

Posted 10 March 2010 - 05:42 PM

zim1985, on 10 March 2010 - 07:13 PM, said:

I found this online....it might be helpful:
...

However, also remember that teachers could call it cheating...just beware

Quote

@Dogstopper Yeah i used that for a different game but i want to go a little
more indepth and deal with each 4 vectors of a rectangle to explore different
options when making games

What do you mean? How much more? The bounding rectangle method works well! Professional games use fine-tuned bounding rectangles...

This post has been edited by Dogstopper: 10 March 2010 - 05:44 PM

### #13 Java Student

• D.I.C Regular

Reputation: 21
• Posts: 498
• Joined: 05-February 10

## Re: Working with vectors

Posted 10 March 2010 - 06:16 PM

Dogstopper, on 10 March 2010 - 04:42 PM, said:

However, also remember that teachers could call it cheating...just beware

No worries im a
next year (gr.12) is when were allowed to take Computer Science 30AP
It just so happens that today we chose our course selection...so i'v
checked off CompSciAP, i can't wait

Dogstopper said:

What do you mean? How much more? The bounding rectangle method works well! Professional games use fine-tuned bounding rectangles...

Say your sprite jumped and you wanted to hit the enemy from the bottom of the enemy's
rectangle, how would the game tell if you hit enemys rect from the left side, right side
or top?

This post has been edited by Java Student: 10 March 2010 - 06:17 PM

### #14 Dogstopper

Reputation: 2965
• Posts: 11,222
• Joined: 15-July 08

## Re: Working with vectors

Posted 10 March 2010 - 06:56 PM

I see, yah, I would go with macosxnerd101's suggestion here and look into java.awt.geom and particularly java.awt.geom.Rectangle2D

### #15 Java Student

• D.I.C Regular

Reputation: 21
• Posts: 498
• Joined: 05-February 10

## Re: Working with vectors

Posted 10 March 2010 - 07:26 PM

Dogstopper, on 10 March 2010 - 05:56 PM, said:

I see, yah, I would go with macosxnerd101's suggestion here and look into java.awt.geom and particularly java.awt.geom.Rectangle2D

Ah ok, i,ll look into Rectangle2D too

This post has been edited by Java Student: 10 March 2010 - 07:27 PM