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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
| package math;
import java.io.Serializable;
import java.util.concurrent.ThreadLocalRandom;
/**
* <b>Vector2f est un vecteur bidimensionnel prenant en argument des rééls </b>
*
* Cette classe n'est pas <i>immuable</i>, voir IVector2f
*
* @author Xyliaris
*/
public class Vector2f extends VectorPrimal implements Serializable
{
public Vector2f(){}
public Vector2f( float x, float y )
{
this.x = x;
this.y = y;
}
public Vector2f(Vector2f vector)
{
this.x = vector.x();
this.y = vector.y();
}
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 float sum() {return this.x + this.y;}
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 morePositiveValueThan( Vector2f vector) {return this.x + this.y > vector.x() + vector.y();}
public boolean moreNegativeValueThan( Vector2f vector) {return this.x + this.y < vector.x() + 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 float randRange()
{
return (float) ThreadLocalRandom.current().nextDouble(this.smaller(), this.bigger());
}
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 divide( float x, float y ) {this.x /= x; this.y /= y;}
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 substract( float x, float y ) {this.x -= x; this.y -= y;}
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 add( float x, float y ) {this.x += x; this.y += y;}
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 void multiply( float x, float y ) {this.x *= x; this.y *= y;}
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 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;
}
public static Vector2f methods( Method method, float 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;
}
public static float methods( Method method, Vector2f target )
{
switch (method)
{
case ADD:
return target.randRange();
case BIGGER:
return target.bigger();
case SMALLER:
return target.smaller();
case RANDRANGE:
return target.randRange();
default:
break;
}
return 0f;
}
private float x;
private float y;
private static final long serialVersionUID = 8318071254896859350L;
} |