Android Open Source - misty Vector






From Project

Back to project page misty.

License

The source code is released under:

MIT License

If you think the Android project misty 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

package com.misty.math;
//from   w  w w  .ja v a2 s  .  c  om
public class Vector
{
  public float x = 0;
  public float y = 0;
  
  public Vector()
  {
    this(0, 0);
  }
  
  public Vector(float x, float y)
  {
    this.x = x;
    this.y = y;
  }
  
  public Vector(Vector other)
  {
    this.x = other.x;
    this.y = other.y;
  }
  
  public Vector copy()
  {
    return new Vector(this.x, this.y);
  }
  
  public Vector set(float x, float y)
  {
    this.x = x;
    this.y = y;
    
    return this;
  }
  
  public Vector set(Vector other)
  {
    return set(other.x, other.y);
  }
  
  public Vector add(float x, float y)
  {
    this.x += x;
    this.y += y;
    
    return this;
  }
  
  public Vector add(Vector other)
  {
    return add(other.x, other.y);
  }
  
  public Vector sub(float x, float y)
  {
    this.x -= x;
    this.y -= y;
    
    return this;
  }
  
  public Vector sub(Vector other)
  {
    return sub(other.x, other.y);
  }
  
  public Vector mul(float scalar)
  {
    this.x *= scalar;
    this.y *= scalar;
    
    return this;
  }
  
  public float length()
  {
    return (float)Math.sqrt((this.x * this.x) + (this.y * this.y));
  }
  
  public Vector normalize()
  {
    float len = length();
    
    if (len != 0)
    {
      this.x /= len;
      this.y /= len;
    }
    
    return this;
  }
  
  public float angle()
  {
    float angle = (float)Math.toDegrees(Math.atan2(this.y, this.x));
    
    if (angle < 0)
    {
      angle += 360;
    }
    
    return angle;
  }
  
  public Vector rotate(float angle)
  {
    float rad = (float)Math.toRadians(angle);
    
    float cos = (float)Math.cos(rad);
    float sin = (float)Math.sin(rad);
    
    float newX = (this.x * cos) - (this.y * sin);
    float newY = (this.x * sin) + (this.y * cos);
    
    this.x = newX;
    this.y = newY;
    
    return this;
  }
  
  public float distance(float x, float y)
  {
    float distX = this.x - x;
    float distY = this.y - y;
    
    return (float)Math.sqrt((distX * distX) + (distY * distY));
  }
  
  public float distance(Vector other)
  {
    return distance(other.x, other.y);
  }
  
  public float distSquared(float x, float y)
  {
    float distX = this.x - x;
    float distY = this.y - y;
    
    return (distX * distX) + (distY * distY);
  }
  
  public float distSquared(Vector other)
  {
    return distSquared(other.x, other.y);
  }
}




Java Source Code List

com.misty.audio.AudioManager.java
com.misty.debug.FPS.java
com.misty.debug.TimeCounter.java
com.misty.graphics.Animation.java
com.misty.graphics.Camera.java
com.misty.graphics.CollisionGrid.java
com.misty.graphics.Renderer.java
com.misty.graphics.ScreenResolution.java
com.misty.graphics.textures.TextureManager.java
com.misty.graphics.textures.Texture.java
com.misty.input.TouchEvent.java
com.misty.kernel.Alarm.java
com.misty.kernel.Engine.java
com.misty.kernel.Misty.java
com.misty.kernel.Process.java
com.misty.math.Rectangle.java
com.misty.math.Utils.java
com.misty.math.Vector.java
com.misty.utils.Assets.java