4、直线图元建立空的实体
一部分的直线图元已经挂接上了多线段图元,在begObj、endObj上。对于还为空的点,需要创建空的节点实例。如果两个直线是连接的,则在他们中间只建立一个节点,可以直接挂接。
package com.ysystems.ycad.lib.yxxf;
public class YxxfEntNode extends YxxfEntHeader implements YxxfEnt {
public String id = null;//图元的id
public double x = 0;
public double y = 0;
public String text = null;//内部的显示文本
public YxxfEntNode(){
}
public void list(){}
public void draw(YxxfGfxContext gc) {
}
public void calc(Yxxf D){
}
public void calcPoint(Yxxf D){
}
}
public Vector nodeVector = new Vector();//用于存放节点,在Yxxf类中
public void calcPoint(Yxxf D){
if(begObj != null && endObj != null) return;//已经有了,不用再处理
//先遍历已经有的了
for(int i=0;i<</span>D.nodeVector.size();i++){
YxxfEntNode node = (YxxfEntNode)D.nodeVector.get(i);
if(begObj == null){//设置起始节点
if(Math.abs(node.x-begpnt.x)<</span>MIN_DIS && Math.abs(node.y-begpnt.y)<</span>MIN_DIS){
begObj = node;
break;
}
}
if(endObj == null){//设置结束节点
if(Math.abs(node.x-endpnt.x)<</span>MIN_DIS && Math.abs(node.y-endpnt.y)<</span>MIN_DIS){
endObj = node;
break;
}
}
}
//如果没有
if(begObj == null){//设置起始节点
YxxfEntNode node = new YxxfEntNode();
node.id = D.getEntId();
node.x = begpnt.x;
node.y = begpnt.y;
D.nodeVector.add(node);
begObj = node;
}
if(endObj == null){//设置结束节点
YxxfEntNode node = new YxxfEntNode();
node.id = D.getEntId();
node.x = endpnt.x;
node.y = endpnt.y;
D.nodeVector.add(node);
endObj = node;
}
}
5、直线数据的输出
至此,直线以及其他图元的关系已经建立完毕,可以输出直线的数据了。
String begId="",begText="",endId="",endText="";
if(begObj.getClass() == YxxfEntNode.class){
YxxfEntNode node = (YxxfEntNode)begObj;
begId = node.id;
begText = node.text;
}else if(begObj.getClass() == YxxfEntLwpolyline.class){
YxxfEntLwpolyline node = (YxxfEntLwpolyline)begObj;
begId = node.id;
begText = node.text;
}
if(endObj.getClass() == YxxfEntNode.class){
YxxfEntNode node = (YxxfEntNode)endObj;
endId = node.id;
endText = node.text;
}else if(endObj.getClass() == YxxfEntLwpolyline.class){
YxxfEntLwpolyline node = (YxxfEntLwpolyline)endObj;
endId = node.id;
endText = node.text;
}
if(begText!=null){
begText = begText.replace("{\\fSimSun|b0|i0|c134|p2;","");
begText = begText.replace("\\P","");
begText = begText.replace("}","");
}
if(endText!=null){
endText = endText.replace("{\\fSimSun|b0|i0|c134|p2;","");
endText = endText.replace("\\P","");
endText = endText.replace("}","");
}
String ret = "{"
+ "\"joint0\":\"" + begId + "\""
+ ",\"jointname0\":\"" + begText + "\""
+ ",\"joint1\":\""+endId+"\""
+ ",\"jointname1\":\""+endText+"\""
+ "}";
return ret;
示例数据输出结果为:
{"joint0":"1","jointname0":"锦西路锅炉房","joint1":"11","jointname1":"11"}
{"joint0":"11","jointname0":"11","joint1":"12","jointname1":"12"}
{"joint0":"12","jointname0":"12","joint1":"13","jointname1":"13"}
{"joint0":"14","jointname0":"14","joint1":"13","jointname1":"13"}
{"joint0":"6","jointname0":"石油换热站","joint1":"15","jointname1":"15"}
{"joint0":"16","jointname0":"16","joint1":"8","jointname1":"省供销换热站"}
{"joint0":"17","jointname0":"17","joint1":"14","jointname1":"14"}
6、点到直线的垂直距离计算方法
有三个点a、b、c,ab组成一条直线, 怎么求出c点到ab直线的垂直距离呢?
先求ab的斜率 lfSlope0 = (yb-ya)/(xa-xb);小于1的为横向的。大于1的为纵向的。等于1为45度倾斜,可以认为是横向的。
其垂线的斜率为lfSlope1 = - 1/lfSlope0;
垂足坐标:
x=(cy-ay+ax*lfSlope0-cx*lfSlope1)/(lfSlope0-lfSlope1);
y=cy+(x-cx)*lfSlope1;
然后计算垂足与c之间的距离
d=sqrt((cy-y)*(cy-y)+(cy-x)*(cx-x));