📄 trackrectangle.java
字号:
package net.aetherial.gis.our.auto.check.repeattrk;
import org.w3c.dom.*;
import net.aetherial.gis.surface.ItemValue;
import net.aetherial.gis.publicuse.PointLineDistance;
/**
* <p>Title: </p>
*
* <p>Description: </p>
*
* <p>Copyright: Copyright (c) 2004</p>
*
* <p>Company: </p>
*
* @author 王爱国
* @version 1.0
*/
public class TrackRectangle {
/**
* 0.001度 = 100米
* 0.0002度 = 20米
*/
public static final double NEAR_DISTANCE = 0.0002;
/**
* 左右上下角
*/
protected double left = 0.0;
protected double right = 0.0;
protected double top = 0.0;
protected double bottom = 0.0;
/**
* 航迹
*/
private Node track = null;
public TrackRectangle() {
}
public TrackRectangle(Node track) {
this.track = track;
this.get_L_R_T_B();
}
public void setTrack(Node track) {
this.track = track;
this.get_L_R_T_B();
}
private void get_L_R_T_B() {
NodeList nl = ItemValue.getTracksPoint(this.track);
if (nl != null) {
for (int i = 0; i < nl.getLength(); i++) {
this.CheckThisPoint(Double.parseDouble(ItemValue.getTracksPointX(nl.
item(i))), Double.parseDouble(ItemValue.getTracksPointY(nl.item(i))));
}
}
/**
* 方框扩大
*/
this.left = this.left - this.NEAR_DISTANCE;
this.right = this.right + this.NEAR_DISTANCE;
this.top = this.top + this.NEAR_DISTANCE;
this.bottom = this.bottom - this.NEAR_DISTANCE;
//System.out.println("left:" + left + ",right:" + right + ",top:" + top + ",bottom:" + bottom);
}
protected void CheckThisPoint(double x, double y) {
/**
* 判断最左
*/
if (this.left == 0.0) {
this.left = x;
}
else {
if (this.left > x) {
this.left = x;
}
}
/**
* 判断最右
*/
if (this.right == 0.0) {
this.right = x;
}
else {
if (this.right < x) {
this.right = x;
}
}
/**
* 判断最上面
*/
if (this.top == 0.0) {
this.top = y;
}
else {
if (this.top < y) {
this.top = y;
}
}
/**
* 判断最下面
*/
if (this.bottom == 0.0) {
this.bottom = y;
}
else {
if (this.bottom > y) {
this.bottom = y;
}
}
}
/**
* 是否相交
*/
public boolean isIntersect(TrackRectangle tr) {
if (this.isPointInIt(tr.getLeft(), tr.getTop(), this)) {
return true;
}
else if (this.isPointInIt(tr.getRight(), tr.getTop(), this)) {
return true;
}
else if (this.isPointInIt(tr.getLeft(), tr.getBottom(), this)) {
return true;
}
else if (this.isPointInIt(tr.getRight(), tr.getBottom(), this)) {
return true;
} //另外一个 矩形的四角是否在本矩形范围内
else if (this.isPointInIt(this.getLeft(), this.getTop(), tr)) {
return true;
}
else if (this.isPointInIt(this.getRight(), this.getTop(), tr)) {
return true;
}
else if (this.isPointInIt(this.getLeft(), this.getBottom(), tr)) {
return true;
}
else if (this.isPointInIt(this.getRight(), this.getBottom(), tr)) {
return true;
} //本矩形的四角是否在另外一个矩形范围内
else if (this.isRectangle_Insect_Like_ADD(tr)) {
return true;
}
else {
return false;
}
}
/**
* 本矩形是否与@param tr矩形互相包含
*/
public boolean isContain(TrackRectangle tr) {
if ( (this.getLeft() >= tr.getLeft()) && (this.getTop() >= tr.getTop()) &&
(this.getBottom() <= tr.getBottom()) && (this.getRight() <= tr.getRight())) {
return true;
}
else if ( (this.getLeft() <= tr.getLeft()) && (this.getTop() <= tr.getTop()) &&
(this.getBottom() >= tr.getBottom()) &&
(this.getRight() >= tr.getRight())) {
return true;
}
else {
return false;
}
}
/**
* 给定任意点P(x,y)
* 得到点到本对象实例的距离
*/
public double getDistanceWithThisRectangle(double x, double y) {
PointLineDistance pd = new PointLineDistance();
pd.setPoint0(x, y);
/**
* 如果在左右之间夹住,则返回点P到方框上面,或者下面横线的最小值
*/
if ( ( (x < this.left) && (x > this.right)) ||
( (x > this.left) && (x < this.right))) {
pd.setPoint1(this.left, this.top);
pd.setPoint2(this.right, this.top);
double d1 = pd.getDis();
pd.setPoint1(this.left, this.bottom);
pd.setPoint2(this.right, this.bottom);
double d2 = pd.getDis();
return Math.min(d1, d2);
/**
* 如果在上下之间夹住,则返回点P到方框左面,或者右面竖线的最小值
*/
}
else if ( ( (y < this.top) && (y > this.bottom)) ||
( (y > this.top) && (y < this.bottom))) {
pd.setPoint1(this.left, this.top);
pd.setPoint2(this.left, this.bottom);
double d1 = pd.getDis();
pd.setPoint1(this.right, this.top);
pd.setPoint2(this.right, this.bottom);
double d2 = pd.getDis();
return Math.min(d1, d2);
}
else {
/**
* 如果不在方框上下之间,或者左右之间,则求点P到方框四个点最小距离
*/
double d1 = Math.sqrt( (x - this.left) * (x - this.left) +
(y - this.top) * (y - this.top));
double d2 = Math.sqrt( (x - this.left) * (x - this.left) +
(y - this.bottom) * (y - this.bottom));
double d3 = Math.sqrt( (x - this.right) * (x - this.right) +
(y - this.top) * (y - this.top));
double d4 = Math.sqrt( (x - this.right) * (x - this.right) +
(y - this.bottom) * (y - this.bottom));
double min = 100000.0;
min = Math.min(min, d1);
min = Math.min(min, d2);
min = Math.min(min, d3);
min = Math.min(min, d4);
return min;
}
}
/**
* 一个矩形的角在另一个矩形范围内
*/
public static final boolean isPointInIt(double x, double y, TrackRectangle tr) {
if ( (x >= tr.getLeft()) && (x <= tr.getRight()) && (y >= tr.getBottom()) &&
(y <= tr.getTop())) {
return true;
}
else {
return false;
}
}
/**
* 两个矩形十字相交
*/
private boolean isRectangle_Insect_Like_ADD(TrackRectangle tr) {
if ( (tr.getLeft() >= this.left) && (tr.getLeft() <= this.right) &&
(tr.getRight() >= this.left) && (tr.getRight() <= this.right)) {
if ( (this.top >= tr.getBottom()) && (this.top <= tr.getTop()) &&
(this.bottom >= tr.getBottom()) && (this.bottom <= tr.getTop())) {
return true;
}
}
return false;
}
public double getTop() {
return top;
}
public double getRight() {
return right;
}
public double getLeft() {
return left;
}
public double getBottom() {
return bottom;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -