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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
|
package shoot;
import java.io.Serializable;
/**
* <b>Vector2f est un vecteur bidimensionnel prenant en argument des rééls </b>
*
* Cette classe n'est pas <i>immuable</i>
*
* @author Xyliaris
*/
public class Vector2f implements Serializable
{
public Vector2f(){}
public Vector2f( float x, float y )
{
this.x = x;
this.y = y;
}
public Vector2f(Vector2f vector)
{
this.x = new Float(vector.x()); // useless Float
this.y = new Float(vector.y()); // useless Float
}
public float x() {return this.x;}
public float y() {return this.y;}
public void setX(float x) {this.x = x;}
public void setY(float y) {this.y = y;}
public void addX(float x) {this.x += x;}
public void addY(float y) {this.y += y;}
public void subX(float x) {this.x -= x;}
public void subY(float y) {this.y -= y;}
public void mulX(float x) {this.x *= x;}
public void mulY(float y) {this.y *= y;}
public void divX(float x) {this.x /= x;}
public void divY(float y) {this.y /= y;}
public void setValues( float x, float y ) {this.x = x; this.y = y;}
public void set( Vector2f value ) {this.x = value.x(); this.y = value.y();}
public float bigger() {return this.x > this.y ? this.x : this.y;}
public float smaller() {return this.x > this.y ? this.y : this.x;}
public boolean biggerthan( Vector2f vector ) {return ( this.x > vector.x() && this.y > vector.y() );}
public boolean smallerthan( Vector2f vector ) {return ( this.x < vector.x() && this.y < vector.y() );}
public boolean awayfrom( Vector2f vector, float distance )
{
return Math.sqrt(Math.pow(this.x - vector.x(), 2) + Math.pow(this.y - vector.y(), 2)) > distance;
}
public float normalize( Vector2f vector ) {return (float) Math.sqrt(Math.pow(vector.x(), 2) + Math.pow(vector.y(), 2));}
/**
* Retourne un vecteur d'angle défini par la position actuel vers une position donné
*
* @see Vector2f#norme()
*/
public Vector2f shoot( Vector2f endpos )
{
Vector2f shoot = new Vector2f(endpos.x() - this.x, endpos.y - this.y );
float norme = this.normalize(shoot);
shoot.divide(norme);
return shoot;
}
public Vector2f randValueBetween( Vector2f value )
{
if( value.x() <= this.x && value.y() <= this.y )
return new Vector2f( value.x() + (float)Math.random() * (this.x - value.x()), value.y() + (float)Math.random() * (this.y - value.y()) );
if( value.x() <= this.x && value.y() >= this.y )
return new Vector2f( value.x() + (float)Math.random() * (this.x - value.x()), this.y + (float)Math.random() * (value.x() - this.y) );
if( value.x() >= this.x && value.y() <= this.y )
return new Vector2f( this.x + (float)Math.random() * (value.x() - this.x), value.y() + (float)Math.random() * (this.y - value.y()) );
return new Vector2f( this.x + (float)Math.random() * (value.x() - this.x), this.y + (float)Math.random() * (value.y() - this.y) );
}
public Vector2f scaledCopy( float value ) {return new Vector2f(this.x * value, this.y * value);}
public void divide( Vector2f value ) {this.x /= value.x(); this.y /= value.y();}
public void divide( float value ) {this.x /= value; this.y /= value;}
public void substract( Vector2f value ) {this.x -= value.x(); this.y -= value.y();}
public void substract( float value ) {this.x -= value; this.y -= value;}
public void add( Vector2f value ) {this.x += value.x(); this.y += value.y();}
public void add( float value ) {this.x += value; this.y += value;}
public void multiply( Vector2f value ) {this.x *= value.x(); this.y *= value.y();}
public void multiply( float value ) {this.x *= value; this.y *= value;}
public Vector2f pow( Vector2f v, float p ) {return new Vector2f((float)(Math.pow(v.x(), p)), (float) Math.pow(v.y(), p) );}
public Vector2f pow( Vector2f v, Vector2f p ) {return new Vector2f((float)(Math.pow(v.x(), p.x())), (float) Math.pow(v.y(), p.y()) );}
public String toString() {return new String( this.x + ":" + this.y );}
///////////////////
////// BUILD //////
///////////////////
/*
* * Pour plus de clarter et éviter les conflicts avec les methodes non statique,
* les methodes sont accessible via une énumération de celle-ci.
*
* @param method Méthodes disponibles : <\br> - add<\br> - substract<\br> - divide<\br> - multiply
* @param target Vecteur a affecter
*
*/
public static Vector2f vector_methods( Method method, Vector2f value, Vector2f target )
{
Vector2f vector = new Vector2f(target);
switch (method)
{
case ADD:
vector.add(value);
break;
case SUBSTRACT:
vector.substract(value);
break;
case DIVIDE:
vector.divide(value);
break;
case MULTIPLY:
vector.multiply(value);
break;
default:
break;
}
return vector;
}
private float x;
private float y;
private static final long serialVersionUID = 8318071254896859350L;
} |
Partager