Android Open Source - misty Rectangle






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  a 2s .c  o m*/
public class Rectangle
{
  public float x = 0;
  public float y = 0;
  public float width = 0;
  public float height = 0;
  
  public Rectangle(float x, float y, float width, float height)
  {
    this.x = x;
    this.y = y;
    this.width = width;
    this.height = height;
  }
  
  public Rectangle(Rectangle rect)
  {
    this.x = rect.x;
    this.y = rect.y;
    this.width = rect.width;
    this.height = rect.height;
  }
  
  public Rectangle set(float x, float y, float width, float height)
  {
    this.x = x;
    this.y = y;
    this.width = width;
    this.height = height;
    
    return this;
  }
  
  public float getRight()
  {
    return this.x + this.width;
  }
  
  public float getTop()
  {
    return this.y + this.height;
  }
  
  public Rectangle setX(float x)
  {
    this.x = x;
    
    return this;
  }
  
  public Rectangle setY(float y)
  {
    this.y = y;
    
    return this;
  }
  
  public Rectangle setWidth(float width)
  {
    this.width = width;
    
    return this;
  }
  
  public Rectangle setHeight(float height)
  {
    this.height = height;
    
    return this;
  }
  
  public Vector getPosition()
  {
    return new Vector(this.x, this.y);
  }
  
  public Rectangle setPosition(Vector point)
  {
    this.x = point.x;
    this.y = point.y;
    
    return this;
  }
  
  public Rectangle setPosition(float x, float y)
  {
    this.x = x;
    this.y = y;
    
    return this;
  }
  
  public Rectangle setSize(float width, float height)
  {
    this.width = width;
    this.height = height;
    
    return this;
  }
  
  public Rectangle setSize(float sizeXY)
  {
    this.width = sizeXY;
    this.height = sizeXY;
    
    return this;
  }
  
  public Vector getSize(Vector size)
  {
    return size.set(this.width, this.height);
  }
  
  public boolean contains(float x, float y)
  {
    return (this.x <= x) && (this.x + this.width >= x) && (this.y <= y) && (this.y + this.height >= y);
  }
  
  public boolean contains(Vector point)
  {
    return contains(point.x, point.y);
  }
  
  public boolean contains(Rectangle rectangle)
  {
    float xmin = rectangle.x;
    float xmax = xmin + rectangle.width;
    
    float ymin = rectangle.y;
    float ymax = ymin + rectangle.height;
    
    return ((xmin > this.x && xmin < this.x + this.width) && (xmax > this.x && xmax < this.x + this.width)) && ((ymin > this.y && ymin < this.y + this.height) && (ymax > this.y && ymax < this.y + this.height));
  }
  
  public boolean intersects(Rectangle rectangle)
  {
    return (this.x < rectangle.getRight()) && (getRight() > rectangle.x) && (this.y < rectangle.getTop()) && (getTop() > rectangle.y);
  }
  
  public boolean intersect(Rectangle rectangle)
  {
    if (intersects(rectangle))
    {
      if (this.x < rectangle.x)
      {
        float newWidth = getRight() - rectangle.x;
        this.x = rectangle.x;
        this.width = newWidth;
      }
      
      if (getRight() > rectangle.getRight())
      {
        this.width = rectangle.getRight() - this.x;
      }
      
      if (this.y < rectangle.y)
      {
        float newHeight = getTop() - rectangle.y;
        this.y = rectangle.y;
        this.height = newHeight;
      }
      
      if (getTop() > rectangle.getTop())
      {
        this.height = rectangle.getTop() - this.y;
      }
      
      return true;
    }
    
    return false;
  }
  
  public Rectangle set(Rectangle rect)
  {
    this.x = rect.x;
    this.y = rect.y;
    this.width = rect.width;
    this.height = rect.height;
    
    return this;
  }
  
  public Rectangle merge(Rectangle rect)
  {
    float minX = Math.min(this.x, rect.x);
    float maxX = Math.max(this.x + this.width, rect.x + rect.width);
    this.x = minX;
    this.width = maxX - minX;
    
    float minY = Math.min(this.y, rect.y);
    float maxY = Math.max(this.y + this.height, rect.y + rect.height);
    this.y = minY;
    this.height = maxY - minY;
    
    return this;
  }
  
  public Rectangle merge(float x, float y)
  {
    float minX = Math.min(this.x, x);
    float maxX = Math.max(this.x + this.width, x);
    this.x = minX;
    this.width = maxX - minX;
    
    float minY = Math.min(this.y, y);
    float maxY = Math.max(this.y + this.height, y);
    this.y = minY;
    this.height = maxY - minY;
    
    return this;
  }
  
  public Rectangle merge(Vector Point)
  {
    return merge(Point.x, Point.y);
  }
  
  public Rectangle merge(Vector[] vecs)
  {
    float minX = this.x;
    float maxX = this.x + this.width;
    float minY = this.y;
    float maxY = this.y + this.height;
    
    for (Vector v : vecs)
    {
      minX = Math.min(minX, v.x);
      maxX = Math.max(maxX, v.x);
      minY = Math.min(minY, v.y);
      maxY = Math.max(maxY, v.y);
    }
    
    this.x = minX;
    this.width = maxX - minX;
    this.y = minY;
    this.height = maxY - minY;
    
    return this;
  }
  
  public float getAspectRatio()
  {
    return (this.height == 0) ? Float.NaN : this.width / this.height;
  }
  
  public Vector getCenter()
  {
    return new Vector(this.x + this.width / 2, this.y + this.height / 2);
  }
  
  public Rectangle setCenter(float x, float y)
  {
    setPosition(x - this.width / 2, y - this.height / 2);
    
    return this;
  }
  
  public Rectangle setCenter(Vector point)
  {
    setPosition(point.x - this.width / 2, point.y - this.height / 2);
    
    return this;
  }
  
  public float area()
  {
    return this.width * this.height;
  }
  
  public float perimeter()
  {
    return 2 * (this.width + this.height);
  }
}




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