⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mshape.java

📁 坦克游戏
💻 JAVA
字号:
/*
 * Created on 2005-3-19
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package shape;

/**
 * @author AnSen
 * 
 * TODO To change the template for this generated type comment go to Window -
 * Preferences - Java - Code Style - Code Templates
 */
public abstract class MShape implements IShape {
	protected boolean isLineAcross(double x0, double y0, double x1, double y1,
			double x2, double y2, double x3, double y3) {
		double sx1 = x1 - x0;
		double sy1 = y1 - y0;
		double sx2 = x3 - x2;
		double sy2 = y3 - y2;

		double s = ((y0 - y2) * sx1 + (x2 - x0) * sy1)
				/ (sy2 * sx1 - sx2 * sy1);
		double t = ((y0 - y2) * sx2 - (x0 - x2) * sy2)
				/ (sy2 * sx1 - sy1 * sx2);
		if (s >= 0 && s <= 1 && t >= 0 && t <= 1) {
			double ax = x0 + t * sx1;
			double ay = y0 + t * sy1;

			return true;
		}
		return false;
	}
	
	public boolean isSuperpose(int left, int top, int width, int height) {
		Rectangle rect=new Rectangle(left,top,width,height,false);
		return isSuperpose(rect);
	}
	
	/* (non-Javadoc)
	 * @see shape.IShape#isSuperpose(double, double, double)
	 */
	public boolean isSuperpose(double x, double y, double radius) {
		Rectangle rect=new Rectangle(x-radius,y-radius,
							radius*2,radius*2,false);
		return isSuperpose(rect);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see shape.IShape#isSuperpose(shape.IShape)
	 */
	public boolean isSuperpose(IShape shape) {
		if (shape instanceof Polygon) {
			if(isAcrossShape((Polygon) shape)){
				return true;
			}
		} else if (shape instanceof Rectangle) {
			if(isAcrossShape((Rectangle) shape)){
				return true;
			}
		}
		if(isConatin(shape)){
			return true;
		}else if(shape.isConatin(this)){
			return true;
		}else{
			return false;
		}
	}

	protected boolean isAcrossShape(Polygon shape){
		for (int i = 0; i < shape.npoints - 1; i++) {
			if (isAcross(shape.xdps[i], shape.ydps[i],
					shape.xdps[i + 1],shape.ydps[i + 1])) {
				return true;
			}
		}
		if (isAcross(shape.xdps[shape.npoints - 1], shape.ydps[shape.npoints - 1],
				shape.xdps[0], shape.ydps[0])) {
			return true;
		}
		return false;
	}

	protected boolean isAcrossShape(Rectangle shape) {
		if(isAcross(shape.x,shape.y,shape.x+shape.width,shape.y)){
			return true;
		}else if(isAcross(shape.x,shape.y,shape.x,shape.y+shape.height)){
			return true;
		}else if(isAcross(shape.x+shape.width,shape.y
				,shape.x+shape.width,shape.y+shape.height)){
			return true;
		}else if(isAcross(shape.x,shape.y+shape.height
				,shape.x+shape.width,shape.y+shape.height)){
			return true;
		}
		return false;
	}
	
	/* (non-Javadoc)
	 * @see shape.IShape#isAcross(shape.IShape)
	 */
	public boolean isAcross(IShape shape) {
		boolean bAcross=false;
		if(shape instanceof Polygon){
			Polygon pshp=(Polygon)shape;
			for(int i=0;i<pshp.npoints-1;i++){
				bAcross=isAcross(pshp.xdps[i],pshp.ydps[i],
						pshp.xdps[i+1],pshp.ydps[i+1]);
				if(bAcross){
					return true;
				}
			}
			bAcross=isAcross(pshp.xdps[0],pshp.ydps[0],
					pshp.xdps[pshp.npoints-1],pshp.ydps[pshp.npoints-1]);
			if(bAcross){
				return true;
			}
		}else if(shape instanceof Rectangle){
			Rectangle rect=(Rectangle)shape;
			if (isAcross( rect.x, rect.y, rect.x+rect.width, rect.y)) {
				return true;
			}
			if (isAcross(rect.x, rect.y,rect.x, rect.y+ rect.height)) {
				return true;
			}
			if (isAcross(rect.x + rect.width, rect.y, 
					rect.x + rect.width, rect.y + rect.height)) {
				return true;
			}
			if (isAcross(rect.x, rect.y + rect.height,
					rect.x + rect.width, rect.y + rect.height)) {
				return true;
			}
		}
		return false;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -