package com.persagy.adm.diagram.core.util;

import com.persagy.adm.diagram.core.model.base.XY;
import org.locationtech.jts.geom.*;

/**
 * 几何计算工具类
 * @author zhaoyk
 */
public class GeomUtil {

	private static GeometryFactory geometryFactory = new GeometryFactory();

	public static Polygon getRect(XY location, XY size) {
		Coordinate[] cs = new Coordinate[5];
		cs[0] = new Coordinate(location.x, location.y);
		cs[1] = new Coordinate(location.x + size.x, location.y);
		cs[2] = new Coordinate(location.x + size.x, location.y + size.y);
		cs[3] = new Coordinate(location.x, location.y + size.y);
		cs[4] = cs[0];
		return geometryFactory.createPolygon(cs);
	}

	public static LineString getLine(XY[] points) {
		Coordinate[] cs = new Coordinate[points.length];
		for (int i = 0; i < cs.length; i++) {
			cs[i] = new Coordinate(points[i].x, points[i].y);
		}
		return geometryFactory.createLineString(cs);
	}

	public static Point getPoint(XY xy){
		return geometryFactory.createPoint(new Coordinate(xy.x, xy.y));
	}

	public static void main(String[] args) {
		Point p1 = getPoint(new XY(1, 1));
		LineString line = getLine(new XY[]{new XY(2, 0), new XY(2, 4)});
		double d = p1.distance(line);
		System.out.println(d);
	}

}
