📄 geoginfo.java
字号:
/**
*
*/
package cn.tsinghua.ag5.CarModel;
//import java.math.*;
import cn.tsinghua.ag5.CarModel.*;
/**
* @author wfq
*
*/
public class GeogInfo {
/**
*
*/
public GeogInfo() {
// TODO Auto-generated constructor stub
}
public static final int s_roadPartNum = 30;
public static RoadParts s_road[];//= new RoadParts[s_roadPartNum];
private static double s_maxLeft;
private static double s_maxRight;
private static double s_maxTop;
private static double s_maxBottom;
private static final double s_margin = 300f;
public static final double s_curRad = 100;
public static void genRoad()
{
s_road = new RoadParts[s_roadPartNum];
s_road[0] = new RoadParts();
//不可用内部类
s_road[0].strCur = true;
s_road[0].length = (100 + 100 * Math.random());
s_road[0].startX = 0;
s_road[0].startY = 0;
s_road[0].endX = Math.round(s_road[0].length);
s_road[0].endY = 0;
for( int i = 1; i < s_roadPartNum; i++ )
{
s_road[i] = new RoadParts();
s_road[i].strCur = Math.random() > 0.3 ? false : true;
if(s_road[i].strCur )
{
s_road[i].length = (100 + 100 * Math.random());
s_road[i].startX = s_road[i-1].endX;
s_road[i].startY = s_road[i-1].endY;
// s_road[i].endX = (int)Math.round(s_road[i].startX + s_road[i].length
// * Math.cos(2 * Math.PI * Math.random()));
// s_road[i].endY = (int)Math.round(s_road[i].startY + s_road[i].length
// * Math.sin(2 * Math.PI * Math.random()));
if(s_road[i-1].strCur)
{
s_road[i].endX = s_road[i].startX + (s_road[i-1].endX - s_road[i-1].startX) * s_road[i].length / s_road[i-1].length;
s_road[i].endY = s_road[i].startY + (s_road[i-1].endY - s_road[i-1].startY) * s_road[i].length / s_road[i-1].length;
}else
{
double angle = tangentDegree(s_road[i-1].centerX, s_road[i-1].centerY, s_road[i-1].endX, s_road[i-1].endY, s_road[i-1].turn);
s_road[i].endX = (s_road[i].startX + s_road[i].length * Math.cos(angle));
s_road[i].endY = (s_road[i].startY + s_road[i].length * Math.sin(angle));
}
}else
{
s_road[i].startX = s_road[i-1].endX;
s_road[i].startY = s_road[i-1].endY;
s_road[i].curRadius = (200 - s_curRad + s_curRad * Math.random());
//can not achieve 180 degree
s_road[i].angle = (20 + 160 * Math.random());
s_road[i].turn = Math.random() > 0.5 ? true : false;
double t_arctanyx; //alfa 上一个路段的切线或直线本身与x轴正方向的夹角
if(s_road[i-1].strCur)
{
t_arctanyx = tangentDegree(s_road[i-1].startX, s_road[i-1].startY, s_road[i-1].endX, s_road[i-1].endY);
}else
{
t_arctanyx= tangentDegree(s_road[i-1].centerX, s_road[i-1].centerY, s_road[i-1].endX, s_road[i-1].endY, s_road[i-1].turn);
}
if(s_road[i].turn)
{
s_road[i].centerX = (s_road[i].startX + s_road[i].curRadius * Math.cos(t_arctanyx + Math.PI/2));
s_road[i].centerY = (s_road[i].startY + s_road[i].curRadius * Math.sin(t_arctanyx + Math.PI/2));
}else
{
s_road[i].centerX = (s_road[i].startX + s_road[i].curRadius * Math.cos(t_arctanyx - Math.PI/2));
s_road[i].centerY = (s_road[i].startY + s_road[i].curRadius * Math.sin(t_arctanyx - Math.PI/2));
}
double t_angleEnd = Math.atan2(s_road[i].startY - s_road[i].centerY, s_road[i].startX-s_road[i].centerX);
t_angleEnd += s_road[i].turn ? Math.toRadians(s_road[i].angle) : - Math.toRadians(s_road[i].angle);
s_road[i].endX = (s_road[i].centerX + s_road[i].curRadius * Math.cos(t_angleEnd));
s_road[i].endY = (s_road[i].centerY + s_road[i].curRadius * Math.sin(t_angleEnd));
}
}
//计算四维参数
for(RoadParts i : s_road)
{
if(i.startX > s_maxRight) s_maxRight = i.startX;
if(i.endX > s_maxRight) s_maxRight = i.endX;
if(i.startX < s_maxLeft) s_maxLeft = i.startX;
if(i.endX < s_maxLeft) s_maxLeft = i.endX;
if(i.startY > s_maxTop) s_maxTop = i.startY;
if(i.startY < s_maxBottom) s_maxBottom = i.startY;
if(i.endY > s_maxTop) s_maxTop = i.endY;
if(i.endY < s_maxBottom) s_maxBottom = i.endY;
}
s_maxRight += s_margin;
s_maxLeft -= s_margin;
s_maxTop += s_margin;
s_maxBottom -= s_margin;
MyGUI.s_isRoadCreated = true;
}
/**
* 如果是直线返回其本身与x轴正方向的夹角
* 如果是曲线,返回其逆时针方向切线与x轴正方向的夹角 -pi -> pi
* @return
* 输入为两个端点,或是中心和圆上一点
*/
public static double tangentDegree( double x0start, double y0start, double xend, double yend)
{
// if(p_strCur)
// {
return Math.atan2(yend-y0start, xend-x0start);
// }else
// {
// double tmp = Math.atan2(yend - y0start, xend - x0start) + Math.PI / 2;
// return tmp > Math.PI ? tmp - 2 * Math.PI : tmp;
// }
}
public static double tangentDegree( double x0start, double y0start, double xend, double yend, boolean p_turn)
{
double tmp = Math.atan2(yend - y0start, xend - x0start) + (p_turn ? Math.PI / 2 : - Math.PI / 2);
if(tmp > Math.PI)
tmp -= 2 * Math.PI;
if(tmp <= - Math.PI)
tmp += 2 * Math.PI ;
return tmp;
}
/**
* @return the maxLeft
*/
public static double getMaxLeft() {
return s_maxLeft;
}
/**
* @return the maxRight
*/
public static double getMaxRight() {
return s_maxRight;
}
/**
* @return the maxTop
*/
public static double getMaxTop() {
return s_maxTop;
}
/**
* @return the maxBottom
*/
public static double getMaxBottom() {
return s_maxBottom;
}
}
class RoadParts
{
boolean strCur; //直道true,弯道false
double length; //100-200, 弯道无此属性
double startX;
double startY;
double endX;
double endY;
double curRadius; //100-200
double angle; //20-180 逆时针 此处为角度,其他都为弧度
boolean turn; //左true,右false
double centerX;
double centerY;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -