📄 41a8f659b99d001c1557a899b06c072e
字号:
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Copyright(c) 2004 Jordi Martin Perez
*/
package org.piratis.j2me.core;
/**
* BBox2D: surrounding box with basic square features
* used by sprite, canvas and so on.
*
* @author Jordi Mart韓
* @copyright Copyright (c) 2004
* @created 24-jun-2004
* @version $Id: BBox2D.java,v 1.4 2004/07/01 22:14:38 piratis Exp $
*/
public class BBox2D
{
/**
* BBox data. Public visibility to speed up access speed (without the
* overhead of a gets methods.
*/
public int x;
public int y;
public int width;
public int height;
/**
* Void constructor
*/
public BBox2D()
{
nullify();
}
/**
* Create a BBox based on another's size
* @param other the basis BBox
*/
public BBox2D(BBox2D other)
{
this(other.x, other.y, other.width, other.height);
}
/**
* Main constructor
* @param x top left corner X
* @param y top left corner Y
* @param width of the box
* @param height of the box
*/
public BBox2D(int x, int y, int width, int height)
{
super();
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
/**
* Gets the center on the X axis
* @return the X-axis center
*/
public int getCenterX()
{
return this.x + this.width / 2;
}
/**
* Gets the center on the Y axis
* @return the Y-axis center
*/
public int getCenterY()
{
return this.y + this.height / 2;
}
/**
* Check if the current BBox collides with another one.
* @param other
* @return whether the objects collide
*/
public boolean collide(BBox2D other)
{
return ((this.x <= (other.x + other.width)) && ((this.x + this.width) >= other.x)
&& (this.y <= (other.y + other.height)) && ((this.y + this.height) >= other.y));
}
/**
* Check if this BBox collides with given area
* @param oX top-left x-coordinate area corner
* @param oY top-left y-coordinate area corner
* @param oW area width
* @param oH area height
* @return whether the area and the BBox collide
*/
public boolean collide(int oX, int oY, int oW, int oH)
{
return ((this.x <= (oX + oW)) && ((this.x + this.width) >= oX)
&& (this.y <= (oY + oH)) && ((this.y + this.height) >= oY));
}
/**
* Merges the current bbox with another, returning the minimal bbox
* containing both (overwritting current one)
* @param other the merged bbox
* @return the minimal bbox containing both of them
*/
public BBox2D merge(BBox2D other)
{
// straight cases
if (this.isVoid())
{
this.x = other.x;
this.y = other.y;
this.width = other.width;
this.height = other.height;
}
else if (!this.isVoid() && !other.isVoid())
{
int maxX = Math.max(this.x + this.width, other.x + other.width);
int maxY = Math.max(this.y + this.height, other.y + other.height);
this.x = Math.min(this.x, other.x);
this.y = Math.min(this.y, other.y);
this.width = maxX - this.x;
this.height = maxY - this.y;
}
return this;
}
/**
* Checks if the bounding box is not null 0x0
* @return
*/
public boolean isVoid()
{
return ((this.width <= 0) && (this.height <= 0));
}
/**
* @return the area of the sprite (pixel^2)
*/
public int getArea()
{
return this.width*this.height;
}
/**
* Voids the current BBox2D
*/
public void nullify()
{
this.x = this.y = this.width = this.height = 0;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -