Android Open Source - droidengine2d Vector2






From Project

Back to project page droidengine2d.

License

The source code is released under:

Apache License

If you think the Android project droidengine2d listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*   Copyright 2013-2014 Miguel Vicente Linares
 */*w w  w .j a v  a  2s. c om*/
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.miviclin.droidengine2d.util.math;

/**
 * 2D Vector.
 * 
 * @author Miguel Vicente Linares
 * 
 */
public class Vector2 {

  private float x;
  private float y;

  /**
   * Creates a new Vector2.
   * 
   * @param x
   * @param y
   */
  public Vector2(float x, float y) {
    this.x = x;
    this.y = y;
  }

  /**
   * Sets the specified values to this Vector2.
   * 
   * @param x
   * @param y
   * @return this Vector2
   */
  public final Vector2 set(float x, float y) {
    this.x = x;
    this.y = y;
    return this;
  }

  /**
   * Sets the specified values to this Vector2.
   * 
   * @param vector Vector2 whose values will be copied to this Vector2.
   */
  public final Vector2 set(Vector2 vector) {
    this.x = vector.x;
    this.y = vector.y;
    return this;
  }

  /**
   * Copies the specified vector into the output vector.
   * 
   * @param output Output vector.
   * @param vector Vector whose coordinates will be copied into the output vector.
   */
  public static final void copy(Vector2 output, Vector2 vector) {
    output.x = vector.x;
    output.y = vector.y;
  }

  /**
   * Adds the specified scalar to both coordinates of this vector.
   * 
   * @param scalar Scalar.
   * @return this Vector2
   */
  public final Vector2 add(float scalar) {
    this.x += scalar;
    this.y += scalar;
    return this;
  }

  /**
   * Adds the specified scalar to both coordinates of the specified vector and stores the result in the output vector.
   * 
   * @param output Output vector.
   * @param vector Vector.
   * @param scalar Scalar.
   */
  public static final void add(Vector2 output, Vector2 vector, float scalar) {
    output.x = vector.x + scalar;
    output.y = vector.y + scalar;
  }

  /**
   * Adds the specified vector to this vector.
   * 
   * @param x Value to be added to the X coordinate of this vector.
   * @param y Value to be added to the Y coordinate of this vector.
   * @return this Vector2
   */
  public final Vector2 add(float x, float y) {
    this.x += x;
    this.y += y;
    return this;
  }

  /**
   * Adds the specified vector to this vector.
   * 
   * @param vector Vector to be added to this vector.
   * @return this Vector2
   */
  public final Vector2 add(Vector2 vector) {
    this.x += vector.x;
    this.y += vector.y;
    return this;
  }

  /**
   * Adds v2 to v1 and stores the result in the output vector.
   * 
   * @param output Result.
   * @param v1 Vector 1.
   * @param v2 Vector 2.
   */
  public static final void add(Vector2 output, Vector2 v1, Vector2 v2) {
    output.x = v1.x + v2.x;
    output.y = v1.y + v2.y;
  }

  /**
   * Substracts the specified scalar from all coordinates of this vector.
   * 
   * @param scalar Scalar.
   * @return this Vector2
   */
  public final Vector2 subtract(float scalar) {
    this.x -= scalar;
    this.y -= scalar;
    return this;
  }

  /**
   * Substracts the specified scalar from all coordinates of the specified vector and stores the result in the output
   * vector.
   * 
   * @param output Result.
   * @param vector Vector.
   * @param scalar Scalar.
   */
  public static final void subtract(Vector2 output, Vector2 vector, float scalar) {
    output.x = vector.x - scalar;
    output.y = vector.y - scalar;
  }

  /**
   * Substracts the specified vector from all coordinates of this vector.
   * 
   * @param x Value to be substracted from the X coordinate of this vector.
   * @param y Value to be substracted from the Y coordinate of this vector.
   * @return this Vector2
   */
  public final Vector2 subtract(float x, float y) {
    this.x -= x;
    this.y -= y;
    return this;
  }

  /**
   * Substracts the specified vector from this vector.
   * 
   * @param vector Vector to be substracted from this vector.
   * @return this Vector2
   */
  public final Vector2 subtract(Vector2 vector) {
    this.x -= vector.x;
    this.y -= vector.y;
    return this;
  }

  /**
   * Substracts v2 from v1 and stores the result in the output vector.
   * 
   * @param output Result.
   * @param v1 Vector 1.
   * @param v2 Vector 2.
   */
  public static final void subtract(Vector2 output, Vector2 v1, Vector2 v2) {
    output.x = v1.x - v2.x;
    output.y = v1.y - v2.y;
  }

  /**
   * Multiplies this vector by the specified scalar.
   * 
   * @param scalar Scalar.
   * @return this Vector2
   */
  public final Vector2 multiply(float scalar) {
    this.x *= scalar;
    this.y *= scalar;
    return this;
  }

  /**
   * Multiplies the specified vector by the specified scalar and stores the result in the output vector.
   * 
   * @param output Result.
   * @param vector Vector.
   * @param scalar Scalar.
   */
  public static final void multiply(Vector2 output, Vector2 vector, float scalar) {
    output.x = vector.x * scalar;
    output.y = vector.y * scalar;
  }

  /**
   * Multiplies this vector by the specified vector.
   * 
   * @param x The X coordinate of this vector will be multiplied by this value.
   * @param y The Y coordinate of this vector will be multiplied by this value.
   * @return this Vector2
   */
  public final Vector2 multiply(float x, float y) {
    this.x *= x;
    this.y *= y;
    return this;
  }

  /**
   * Multiplies this vector by the specified vector.
   * 
   * @param vector Vector.
   * @return this Vector2
   */
  public final Vector2 multiply(Vector2 vector) {
    this.x *= vector.x;
    this.y *= vector.y;
    return this;
  }

  /**
   * Multiplies v1 by v2 and stores the result in the output vector.
   * 
   * @param output Result.
   * @param v1 Vector 1.
   * @param v2 Vector 2.
   */
  public static final void multiply(Vector2 output, Vector2 v1, Vector2 v2) {
    output.x = v1.x * v2.x;
    output.y = v1.y * v2.y;
  }

  /**
   * Divides this vector by the specified scalar.
   * 
   * @param scalar Scalar.
   * @return this Vector2
   */
  public final Vector2 divide(float scalar) {
    this.x /= scalar;
    this.y /= scalar;
    return this;
  }

  /**
   * Divides the specified vector by the specified scalar and stores the result in the output vector.
   * 
   * @param output Resultado de la operacion
   * @param vector Vector original
   * @param scalar Scalar.
   */
  public static final void divide(Vector2 output, Vector2 vector, float scalar) {
    output.x = vector.x / scalar;
    output.y = vector.y / scalar;
  }

  /**
   * Divides this vector by the specified vector.
   * 
   * @param x The X coordinate of this vector will be divided by this value.
   * @param y The Y coordinate of this vector will be multiplied by this value.
   * @return this Vector2
   */
  public final Vector2 divide(float x, float y) {
    this.x /= x;
    this.y /= y;
    return this;
  }

  /**
   * Divides this vector by the specified vector.
   * 
   * @param vector Vector.
   * @return this Vector2
   */
  public final Vector2 divide(Vector2 vector) {
    this.x /= vector.x;
    this.y /= vector.y;
    return this;
  }

  /**
   * Divides v1 by the v2 and stores the result in the output vector.
   * 
   * @param output Result.
   * @param v1 Vector 1.
   * @param v2 Vector 2.
   */
  public static final void divide(Vector2 output, Vector2 v1, Vector2 v2) {
    output.x = v1.x / v2.x;
    output.y = v1.y / v2.y;
  }

  /**
   * Performs the dot product of this vector and the specified vector and stores the result in this vector.
   * 
   * @param x X coordinate of the specified vector.
   * @param y Y coordinate of the specified vector.
   * @return dot product of v1 and v2
   */
  public final float dotProduct(float x, float y) {
    return this.x * x + this.y * y;
  }

  /**
   * Performs the dot product of this vector and the specified vector and stores the result in this vector.
   * 
   * @param vector Vector.
   * @return dot product of v1 and v2
   */
  public final float dotProduct(Vector2 vector) {
    return x * vector.x + y * vector.y;
  }

  /**
   * Performs the dot product of v1 and v2.
   * 
   * @param v1 Vector 1.
   * @param v2 Vector 2.
   * @return dot product of v1 and v2
   */
  public static final float dotProduct(Vector2 v1, Vector2 v2) {
    return v1.x * v2.x + v1.y * v2.y;
  }

  /**
   * Calculates the length of this vector.
   * 
   * @return length of this vector
   */
  public final float length() {
    return (float) Math.sqrt(x * x + y * y);
  }

  /**
   * Calculates the length of the specified vector.
   * 
   * @param vector Vector.
   * @return length of the specified vector
   */
  public static final float length(Vector2 vector) {
    return (float) Math.sqrt(vector.x * vector.x + vector.y * vector.y);
  }

  /**
   * Sets the length of this vector.<br>
   * 
   * @param length New length.
   * @return this Vector2
   */
  public final Vector2 setLength(float length) {
    if (length < 0) {
      throw new IllegalArgumentException("The specified length must be positive or zero.");
    }
    normalize();
    multiply(length);
    return this;
  }

  /**
   * Normalizes this vector.
   * 
   * @return this Vector2
   */
  public final Vector2 normalize() {
    float length = (float) Math.sqrt(x * x + y * y);
    if (length == 0) {
      return this;
    }
    return this.divide(length);
  }

  /**
   * Normalizes the specified vector and stores the result in the output vector.
   * 
   * @param output Result.
   * @param vector Vector.
   */
  public static final void normalize(Vector2 output, Vector2 vector) {
    float length = Vector2.length(vector);
    if (length == 0) {
      Vector2.copy(output, vector);
      return;
    }
    Vector2.copy(output, vector.divide(length));
  }

  /**
   * Converts the coordinates of this vector to their absolute values.
   * 
   * @return this Vector2
   */
  public final Vector2 abs() {
    this.x = Math.abs(this.x);
    this.y = Math.abs(this.y);
    return this;
  }

  /**
   * Converts the coordinates of the specified vector to their absolute values and stores the result in the output
   * vector.
   * 
   * @param output Result.
   * @param vector Vector.
   */
  public static final void abs(Vector2 output, Vector2 vector) {
    output.x = Math.abs(vector.x);
    output.y = Math.abs(vector.y);
  }

  /**
   * Returns the X coordinate of this vector.
   * 
   * @return X
   */
  public final float getX() {
    return x;
  }

  /**
   * Sets the value of the X coordinate of this vector.
   * 
   * @param x New value.
   */
  public final void setX(float x) {
    this.x = x;
  }

  /**
   * Returns the Y coordinate of this vector.
   * 
   * @return Y
   */
  public final float getY() {
    return y;
  }

  /**
   * Sets the value of the Y coordinate of this vector.
   * 
   * @param y New value.
   */
  public final void setY(float y) {
    this.y = y;
  }

  @Override
  public String toString() {
    return "(" + x + ", " + y + ")";
  }

}




Java Source Code List

com.miviclin.droidengine2d.AbstractGame.java
com.miviclin.droidengine2d.EngineActivity.java
com.miviclin.droidengine2d.EngineLock.java
com.miviclin.droidengine2d.Engine.java
com.miviclin.droidengine2d.GameThread.java
com.miviclin.droidengine2d.Game.java
com.miviclin.droidengine2d.audio.MusicPlayer.java
com.miviclin.droidengine2d.audio.SoundManager.java
com.miviclin.droidengine2d.gamestate.GameStateAdapter.java
com.miviclin.droidengine2d.gamestate.GameStateManager.java
com.miviclin.droidengine2d.gamestate.GameStateNotRegisteredException.java
com.miviclin.droidengine2d.gamestate.GameState.java
com.miviclin.droidengine2d.gamestate.OnGameStateChangeListener.java
com.miviclin.droidengine2d.graphics.Color.java
com.miviclin.droidengine2d.graphics.DefaultRenderer.java
com.miviclin.droidengine2d.graphics.EngineRenderer.java
com.miviclin.droidengine2d.graphics.GLDebugger.java
com.miviclin.droidengine2d.graphics.GLRenderer.java
com.miviclin.droidengine2d.graphics.GLView.java
com.miviclin.droidengine2d.graphics.Graphics.java
com.miviclin.droidengine2d.graphics.animation.AnimationFrame.java
com.miviclin.droidengine2d.graphics.animation.AnimationStateAdapter.java
com.miviclin.droidengine2d.graphics.animation.AnimationStateListener.java
com.miviclin.droidengine2d.graphics.animation.Animation.java
com.miviclin.droidengine2d.graphics.cameras.Camera.java
com.miviclin.droidengine2d.graphics.cameras.OrthographicCamera.java
com.miviclin.droidengine2d.graphics.material.BlendingOptions.java
com.miviclin.droidengine2d.graphics.material.ColorMaterial.java
com.miviclin.droidengine2d.graphics.material.Material.java
com.miviclin.droidengine2d.graphics.material.TextureColorMaterial.java
com.miviclin.droidengine2d.graphics.material.TextureHsvMaterial.java
com.miviclin.droidengine2d.graphics.material.TextureMaterial.java
com.miviclin.droidengine2d.graphics.material.TransparentTextureMaterial.java
com.miviclin.droidengine2d.graphics.material.UnsupportedMaterialException.java
com.miviclin.droidengine2d.graphics.mesh.ColorMaterialBatchRenderer.java
com.miviclin.droidengine2d.graphics.mesh.Geometry.java
com.miviclin.droidengine2d.graphics.mesh.GraphicsBatchRenderer.java
com.miviclin.droidengine2d.graphics.mesh.RectangleBatchGeometry.java
com.miviclin.droidengine2d.graphics.mesh.RectangleBatchRenderer.java
com.miviclin.droidengine2d.graphics.mesh.TextureColorMaterialBatchRenderer.java
com.miviclin.droidengine2d.graphics.mesh.TextureHsvMaterialBatchRenderer.java
com.miviclin.droidengine2d.graphics.mesh.TextureMaterialBatchRendererBase.java
com.miviclin.droidengine2d.graphics.mesh.TextureMaterialBatchRenderer.java
com.miviclin.droidengine2d.graphics.mesh.TransparentTextureMaterialBatchRenderer.java
com.miviclin.droidengine2d.graphics.shader.ShaderProgramException.java
com.miviclin.droidengine2d.graphics.shader.ShaderProgram.java
com.miviclin.droidengine2d.graphics.shader.ShaderVars.java
com.miviclin.droidengine2d.graphics.text.BitmapFont.java
com.miviclin.droidengine2d.graphics.text.FontChar.java
com.miviclin.droidengine2d.graphics.text.Font.java
com.miviclin.droidengine2d.graphics.text.UndefinedCharacterException.java
com.miviclin.droidengine2d.graphics.texture.TextureAtlas.java
com.miviclin.droidengine2d.graphics.texture.TextureManager.java
com.miviclin.droidengine2d.graphics.texture.TexturePackerAtlas.java
com.miviclin.droidengine2d.graphics.texture.TextureRegion.java
com.miviclin.droidengine2d.graphics.texture.Texture.java
com.miviclin.droidengine2d.input.DefaultKeyEventProcessor.java
com.miviclin.droidengine2d.input.GameInputManager.java
com.miviclin.droidengine2d.input.GameStateInputManager.java
com.miviclin.droidengine2d.input.KeyEventInfo.java
com.miviclin.droidengine2d.input.KeyEventProcessor.java
com.miviclin.droidengine2d.input.KeyProcessor.java
com.miviclin.droidengine2d.input.MotionEventProcessor.java
com.miviclin.droidengine2d.input.TouchProcessor.java
com.miviclin.droidengine2d.input.sensor.AccelerometerValuesListener.java
com.miviclin.droidengine2d.input.sensor.Accelerometer.java
com.miviclin.droidengine2d.input.sensor.SensorUtilities.java
com.miviclin.droidengine2d.resources.AssetsLoader.java
com.miviclin.droidengine2d.util.ActivityUtilities.java
com.miviclin.droidengine2d.util.MutexLock.java
com.miviclin.droidengine2d.util.PrimitiveTypeSize.java
com.miviclin.droidengine2d.util.TransformUtilities.java
com.miviclin.droidengine2d.util.Transform.java
com.miviclin.droidengine2d.util.math.Matrix4.java
com.miviclin.droidengine2d.util.math.MatrixFix.java
com.miviclin.droidengine2d.util.math.Vector2.java
com.miviclin.droidengine2d.util.math.Vector3.java
com.miviclin.droidengine2d.util.time.TimeConstants.java
com.miviclin.droidengine2d.util.time.TimeCounter.java