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

📄 polygon.java

📁 坦克游戏
💻 JAVA
字号:
/*
 * Created on 2005-1-9
 *
 * shape
 */
package shape;

import java.util.StringTokenizer;

/**
 * @author AnSen
 * 
 * Polygon
 */
public class Polygon extends MShape {
	public boolean isFill = false;

	public double[] xdps;

	public double[] ydps;

	public double[] angls;

	public double[] radius;

	public int npoints = 0;
	
	private double centerX, centerY;

	/**
	 * @param xpoints
	 * @param ypoints
	 * @param npoints
	 * @param isFill
	 * @param centerX
	 * @param centerY
	 */
	public Polygon(double[] xpoints, double[] ypoints, int npoints,
			boolean isFill, double centerX, double centerY) {

		this.centerX=centerX;
		this.centerY=centerY;
		
		xdps = xpoints;
		ydps = ypoints;

		this.npoints = npoints;
		this.isFill = isFill;

		angls = new double[npoints];
		radius = new double[npoints];

		double dhlHeight, dhlWidth;

		for (int i = 0; i < npoints; i++) {
			dhlHeight = ypoints[i] - centerY;
			dhlWidth = xpoints[i] - centerX;

			angls[i] = Math.atan2(dhlHeight, dhlWidth);
			radius[i] = Math.sqrt(dhlHeight * dhlHeight + dhlWidth * dhlWidth);
		}
	}
	
	public java.awt.Polygon getAwtPolygon(int left,int top){
		int[] xpoints = new int[npoints];
		int[] ypoints = new int[npoints];
		for (int i = 0; i < npoints; i++) {
			xpoints[i] = (int)Math.ceil(xdps[i] - left);
			ypoints[i] = (int)Math.ceil(ydps[i] - top);
		}
		java.awt.Polygon plg=new java.awt.Polygon(xpoints,ypoints,npoints);
		return plg;
	}

	public void setLocation(double x, double y, int i) {
		xdps[i] = x;
		ydps[i] = y;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see shape.IShape#setLocation(double, double)
	 */
	public void setLocation(double x, double y) {
		offset(x, y);

	}

	public void offset(double x, double y) {
		for (int i = 0; i < npoints; i++) {
			xdps[i] += x;
			ydps[i] += y;
		}
		centerX+=x;
		centerY+=y;
	}

	public void addPoint(int x, int y) {
		if (npoints == xdps.length) {
			double tmp[];

			tmp = new double[npoints * 2];
			System.arraycopy(xdps, 0, tmp, 0, npoints);
			xdps = tmp;

			tmp = new double[npoints * 2];
			System.arraycopy(ydps, 0, tmp, 0, npoints);
			ydps = tmp;
		}
		xdps[npoints] = x;
		ydps[npoints] = y;
		npoints++;
	}

	private void arrayCopy(int[] src, double[] dec, int size) {
		if (src.length < size || dec.length < size) {
			System.err.println("error array from shape.Polygon");
			return;
		}
		for (int i = 0; i < size; i++) {
			dec[i] = src[i];
		}
	}

	private void arrayCopy(double[] src, int[] dec, int size) {
		if (src.length < size || dec.length < size) {
			System.err.println("error array from shape.Polygon");
			return;
		}
		for (int i = 0; i < size; i++) {
			dec[i] = (int) Math.ceil(src[i]);
		}
	}

	public void rotate(double dDir, double centerX, double centerY) {
		for (int i = 0; i < npoints; i++) {
			xdps[i] = centerX + Math.cos(dDir + angls[i]) * radius[i];
			ydps[i] = centerY + Math.sin(dDir + angls[i]) * radius[i];
		}
	}

	public void move(double dDirect, double dDistance) {
		double offsetX=Math.cos(dDirect) * dDistance;
		double offsetY=Math.sin(dDirect) * dDistance;
		for (int i = 0; i < npoints; i++) {
			setLocation(xdps[i] + offsetX, ydps[i] + offsetY, i);
		}
		centerX+=offsetX;
		centerY+=offsetY;
	}

	public static Polygon parsePolygon(String src) {
		StringTokenizer stk = new StringTokenizer(src, " ");
		StringTokenizer stkPt;
		Polygon polObj = null;
		int i = 0;
		int npoints = stk.countTokens();
		double[] xpoints = new double[npoints];
		double[] ypoints = new double[npoints];
		while (stk.countTokens() > 0) {
			stkPt = new StringTokenizer(stk.nextToken(), ",");
			if (stkPt.countTokens() != 2) {
				return null;//error points
			}
			xpoints[i] = Double.parseDouble(stkPt.nextToken());
			ypoints[i] = Double.parseDouble(stkPt.nextToken());
			//System.out.println(xpoints[i]+","+ypoints[i]);
			i++;
		}
		try {
			polObj = new Polygon(xpoints, ypoints, npoints, false, 0, 0);
		} catch (Exception e) {/* ignore */
		}
		return polObj;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see shape.IShape#isAcross(double, double, double, double)
	 */
	public boolean isAcross(double x1, double y1, double x2, double y2) {
		for (int k = 0; k < npoints - 1; k++) {
			if (isLineAcross(x1, y1, x2, y2, 
					xdps[k], ydps[k], xdps[k + 1],ydps[k + 1])) {
				return true;
			}
		}
		if (isLineAcross(x1, y1, x2, y2, 
				xdps[npoints - 1], ydps[npoints - 1],xdps[0], ydps[0])) {
			return true;
		}
		return false;
	}
	
	public Rectangle getBoundBox(){
		double x=Integer.MAX_VALUE;
		double y=Integer.MAX_VALUE;
		double width,height;
		double maxX=0,maxY=0;
		for(int i=0;i<npoints;i++){
			if(x>xdps[i]){
				x=xdps[i];
			}
			if(y>ydps[i]){
				y=ydps[i];
			}
			if(maxX<xdps[i]){
				maxX=xdps[i];
			}
			if(maxY<ydps[i]){
				maxY=ydps[i];
			}
		}
		width=maxX-x;
		height=maxY-y;
		Rectangle rect=new Rectangle(x,y,width,height,false);
		return rect;
	}
	
	
	/* (non-Javadoc)
	 * @see shape.IShape#isConatin(shape.IShape)
	 */
	public boolean isConatin(IShape shape) {
		Rectangle rect=null;
		Rectangle myRect=this.getBoundBox();
		if(shape instanceof Polygon){
			Polygon pshp=(Polygon)shape;
			rect=pshp.getBoundBox();
		}else if(shape instanceof Rectangle){
			rect=(Rectangle)shape;
		}
		if(rect.x>myRect.x&&rect.y>myRect.y&&
				rect.width<myRect.width&&rect.height<myRect.height){
			return true;
		}else{
			return false;
		}
	}
	
	public Polygon cloneShape(){
		double[] xpoints=new double[npoints];
		double[] ypoints=new double[npoints];
		System.arraycopy(xdps,0,xpoints,0,npoints);
		System.arraycopy(ydps,0,ypoints,0,npoints);
		return new Polygon(xpoints,ypoints,npoints,false,centerX,centerY);
	}
	
	
}

⌨️ 快捷键说明

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