Android Open Source - minecraft-connect-4 Rectangle






From Project

Back to project page minecraft-connect-4.

License

The source code is released under:

MIT License

If you think the Android project minecraft-connect-4 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 (C) 2014 Aleksandr Belkin. All Rights Reserved. */
package sq.squ1rr.mcc4.gl;
/*from   w ww  .ja v a  2  s . co m*/
/**
 * Same as android.Rect but upside down, best for OpenGL. Also stores the
 * coordinates the way that is more suitable for OpenGL calculations.
 * @author Aleksandr Belkin
 */
public class Rectangle {
    /** offset on X */
    private float offsetX;
    
    /** offset on Y */
    private float offsetY;
    
    /** rectangle width */
    private float width;
    
    /** rectangle height */
    private float height;
    
    /**
     * Create empty rectangle
     */
    Rectangle() {
        setSize(0f, 0f);
        setOffset(0f, 0f);
    }
    
    /**
     * Creates rectangle with zero offset
     * @param width
     * @param height
     */
    public Rectangle(float width, float height) {
        setSize(width, height);
        setOffset(0f, 0f);
    }
    
    /**
     * Creates rectangle of given size on a given offset
     * @param ox
     * @param oy
     * @param w
     * @param h
     */
    public Rectangle(float ox, float oy, float w, float h) {
        setSize(w, h);
        setOffset(ox, oy);
    }
    
    /**
     * Copy constructor
     * @param rect
     */
    public Rectangle(Rectangle rect) {
        setSize(rect.getWidth(), rect.getHeight());
        setOffset(rect.getX(), rect.getY());
    }
    
    /**
     * Returns offset on X
     * @return
     */
    public float getX() {
        return offsetX;
    }
    
    /**
     * Returns offset on Y
     * @return
     */
    public float getY() {
        return offsetY;
    }
    
    /**
     * Returns width
     * @return
     */
    public float getWidth() {
        return width;
    }
    
    /**
     * Returns height
     * @return
     */
    public float getHeight() {
        return height;
    }
    
    /**
     * Returns X coordinate of the left line
     * @return
     */
    public float getLeft() {
        return offsetX;
    }
    
    /**
     * Returns X coordinate of the right line
     * @return
     */
    public float getRight() {
        return offsetX + width;
    }
    
    /**
     * Returns Y coordinate of the top line
     * @return
     */
    public float getTop() {
        return offsetY + height;
    }
    
    /**
     * Returns Y coordinate of the bottom line
     * @return
     */
    public float getBottom() {
        return offsetY;
    }
    
    /**
     * Sets width and height of the rectangle
     * @param _width
     * @param _height
     */
    public void setSize(float _width, float _height) {
        width = _width;
        height = _height;
    }
    
    /**
     * Sets the width of the rectangle
     * @param _width
     */
    public void setWidth(float _width) {
        width = _width;
    }
    
    /**
     * Sets the height of the rectangle
     * @param _height
     */
    public void setHeight(float _height) {
        height = _height;
    }
    
    /**
     * Sets X and Y offset of the rectangle
     * @param ox
     * @param oy
     */
    public void setOffset(float ox, float oy) {
        offsetX = ox;
        offsetY = oy;
    }
    
    /**
     * Sets offset of the rectangle on X
     * @param ox
     */
    public void setX(float ox) {
        offsetX = ox;
    }
    
    /**
     * Sets offset of the rectangle on Y
     * @param oy
     */
    public void setY(float oy) {
        offsetY = oy;
    }
    
    /**
     * Shrinks rectangles by the factor {m} with centre in the centre of the
     * rectangle.
     * @param m
     */
    public void shrink(float m) {
        float newWidth = width / m;
        float newHeight = height / m;
        
        offsetX += Math.abs(width - newWidth) / 2;
        offsetY += Math.abs(height - newHeight) / 2;
        
        width = newWidth;
        height = newHeight;
    }
    
    /**
     * Move left line of the rectangle {ex} pixels left
     * @param ex
     */
    public void expandLeft(float ex) {
        width += ex;
        offsetX -= ex;
    }
    
    /**
     * Move right line of the rectangle {ex} pixels right
     * @param ex
     */
    public void expandRight(float ex) {
        width += ex;
    }
    
    /**
     * Move bottom line of the rectangle {ex} pixels down
     * @param ex
     */
    public void expandDown(float ex) {
        height += ex;
        offsetY -= ex;
    }
    
    /**
     * Move top line of the rectangle {ex} pixels up
     * @param ex
     */
    public void expandUp(float ex) {
        height += ex;
    }
    
    /**
     * Return true if (x, y) is within the rectangle
     * @param x
     * @param y
     * @return
     */
    public boolean contains(float x, float y) {
        if(x < getLeft() || x > getRight()) return false;
        if(y < getBottom() || y > getTop()) return false;
        return true;
    }
}




Java Source Code List

sq.squ1rr.mcc4.AboutMenu.java
sq.squ1rr.mcc4.BaseActivity.java
sq.squ1rr.mcc4.GameActivity.java
sq.squ1rr.mcc4.MainMenuActivity.java
sq.squ1rr.mcc4.MainMenu.java
sq.squ1rr.mcc4.MenuLayout.java
sq.squ1rr.mcc4.OptionsMenu.java
sq.squ1rr.mcc4.QuickGameMenu.java
sq.squ1rr.mcc4.StatsMenu.java
sq.squ1rr.mcc4.Stats.java
sq.squ1rr.mcc4.ai.Ai.java
sq.squ1rr.mcc4.ai.EasyAi.java
sq.squ1rr.mcc4.ai.HardAi.java
sq.squ1rr.mcc4.ai.NormalAi.java
sq.squ1rr.mcc4.ai.PeacefulAi.java
sq.squ1rr.mcc4.board.BoardDialogue.java
sq.squ1rr.mcc4.board.BoardLogic.java
sq.squ1rr.mcc4.board.BoardRenderer.java
sq.squ1rr.mcc4.board.BoardView.java
sq.squ1rr.mcc4.board.GameBoard.java
sq.squ1rr.mcc4.board.Texture.java
sq.squ1rr.mcc4.gl.Rectangle.java
sq.squ1rr.mcc4.gl.SpriteBatch.java
sq.squ1rr.mcc4.gl.SpriteString.java
sq.squ1rr.mcc4.gl.Sprite.java
sq.squ1rr.mcc4.layout.LayoutManager.java
sq.squ1rr.mcc4.layout.McButton.java
sq.squ1rr.mcc4.layout.McGroup.java
sq.squ1rr.mcc4.layout.McSelector.java
sq.squ1rr.mcc4.layout.McStyle.java
sq.squ1rr.mcc4.layout.McText.java
sq.squ1rr.mcc4.layout.McToggler.java
sq.squ1rr.mcc4.layout.McToken.java
sq.squ1rr.mcc4.rules.GameRules.java
sq.squ1rr.mcc4.rules.Player.java
sq.squ1rr.mcc4.rules.Rule.java
sq.squ1rr.mcc4.util.GlobalConstants.java