package test.xt.hb.map;

import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.List;

public class MapTools {
	/**
	 * 判断当前位置是否在围栏内
	 * 
	 * @param mobilelocationEntity
	 * @param enclosureList
	 * @return
	 */
	public static boolean isInPolygon(MobilelocationEntity mobilelocationEntity, List<Enclosure> enclosureList) {
		double p_x = mobilelocationEntity.getLongitude();
		double p_y = mobilelocationEntity.getLatitude();
		Point2D.Double point = new Point2D.Double(p_x, p_y);

		List<Point2D.Double> pointList = new ArrayList<Point2D.Double>();

		for (Enclosure enclosure : enclosureList) {
			double polygonPoint_x = enclosure.getLongitude();
			double polygonPoint_y = enclosure.getLatitude();
			Point2D.Double polygonPoint = new Point2D.Double(polygonPoint_x, polygonPoint_y);
			pointList.add(polygonPoint);
		}
		MapTools test = new MapTools();
		return test.checkWithJdkGeneralPath(point, pointList);
	}

	/**
	 * 返回一个点是否在一个多边形区域内
	 * 
	 * @param point
	 * @param polygon
	 * @return
	 */
	private boolean checkWithJdkGeneralPath(Point2D.Double point, List<Point2D.Double> polygon) {
		java.awt.geom.GeneralPath p = new java.awt.geom.GeneralPath();

		Point2D.Double first = polygon.get(0);
		p.moveTo(first.x, first.y);
		polygon.remove(0);
		for (Point2D.Double d : polygon) {
			p.lineTo(d.x, d.y);
		}

		p.lineTo(first.x, first.y);

		p.closePath();

		return p.contains(point);

	}
}

class Enclosure {
	private double longitude, latitude;

	public double getLatitude() {
		return latitude;
	}

	public double getLongitude() {
		return longitude;
	}

	public void setLongitude(double longitude) {
		this.longitude = longitude;
	}

	public void setLatitude(double latitude) {
		this.latitude = latitude;
	}
}

class MobilelocationEntity {
	private double longitude, latitude;

	public double getLatitude() {
		return latitude;
	}

	public double getLongitude() {
		return longitude;
	}

	public void setLongitude(double longitude) {
		this.longitude = longitude;
	}

	public void setLatitude(double latitude) {
		this.latitude = latitude;
	}
}
