Java 最有效的实现方法是什么?
我有一个1公里的正方形,我把它分成100米,正反两面Java 最有效的实现方法是什么?,java,math,graphics,performance,Java,Math,Graphics,Performance,我有一个1公里的正方形,我把它分成100米,正反两面 __________y__________ | | | | | | | | | |---------|---------| x | | | | | |
__________y__________
| | |
| | |
| | |
|---------|---------| x
| | |
| | |
|_________|_________|
基本上,我在做一个正方形,就像上面的一样。我把它分成100米的正方形,在x轴的正方向和负方向。然后我在各自的坐标中绘制每个点,我计划通过一系列if/else语句(至少50,最多100)抛出一个(x,y)值,但我担心这会有多昂贵。有没有更有效的方法来做我想做的事情
下面是一个我计划如何做的例子
if(tlat < lat && tlat > lat - 89930.7){
//point is within 100 meters on lat
//On the NEGATIVE SIDE.
if(tlng > lng && tlng < lng + 147999.8){
//point is within 100 meters on lat NEGATIVE &&
//withing 100 meters on lng POSITIVE
layers.addcube(highlng100p, lowlng100p, yhigh, ylow, highlat100p, lowlat100p);
highlng100p = highlng100p + 5;
lowlng100p = lowlng100p + 5;
highlat100p = highlat100p + 5;
lowlat100p = lowlat100p + 5;
}else if(tlng < lng && tlng > lng - 147999.8){
//point is within 100 meters on lat NEGATIVE &&
//withing 100 meters on lng NEGATIVE
layers.addcube(highlat100n, lowlat100n, yhigh, ylow, highlng100n, lowlng100n);
highlat100n = highlat100n + 5;
lowlat100n = lowlat100n + 5;
highlng100n = highlng100n + 5;
lowlat100n = lowlat100n + 5;
}else if(tlng > lng && tlng < lng + 295999.6){
//point is within 200 meters on lat NEGATIVE &&
//withing 200 meters on lng POSITIVE
layers.addcube(highlat200n, lowlat200n, yhigh, ylow, highlng200p, lowlng200n);
highlat200n = highlat200n + 5;
lowlat200n = lowlat200n + 5;
highlng200p = highlng200p + 5;
lowlng200p = lowlng200p + 5;
}else if(tlng < lng && tlng > lng - 295999.6){
//point is within 200 meters on lat NEGATIVE &&
//withing 200 meters on lng NEGATIVE
layers.addcube(highlat200n, lowlat200n, yhigh, ylow, highlng200n, lowlng200n);
highlat200n = highlat200n + 5;
lowlat200n = lowlat200n + 5;
highlng200n = highlng200n + 5;
lowlng200n = lowlng200n + 5;
}else if(tlng > lng && tlng < lng + 443999.4){
//point is within 300 meters on lat NEGATIVE &&
//withing 300 meters on lng POSITIVE
layers.addcube(highlat300n, lowlat300n, yhigh, ylow, highlng300p, lowlng300p);
highlat300n = highlat300n + 5;
lowlat300n = lowlat300n + 5;
highlng300p = highlng300p + 5;
lowlng300p = lowlng300p + 5;
}else if(tlng < lng && tlng > lng - 443999.4){
//point is within 300 meters on lat NEGATIVE &&
//withing 300 meters on lng NEGATIVE
layers.addcube(highlat300n, lowlat300n, yhigh, ylow, highlng300n, lowlng300n);
highlat300n = highlat300n + 5;
lowlat300n = lowlat300n + 5;
highlng300n = highlng300n + 5;
lowlng300n = lowlng300n + 5;
} else if(tlng > lng && tlng < lng + 591999.2){
//point is within 400 meters on lng
//on the POSITIVE SIDE
layers.addcube(highlat400n, lowlat400n, yhigh, ylow, highlng400p, lowlng400p);
highlat400n = highlat400n + 5;
lowlat400n = lowlat400n + 5;
highlng400p = highlng400p + 5;
lowlng400p = lowlng400p + 5;
}else if(tlng < lng && tlng > lng - 591999.2){
//point is within 400 meters on lng
//on the NEGATIVE SIDE
layers.addcube(highlat400n, lowlat400n, yhigh, ylow, highlng400n, lowlng400n);
highlat400n = highlat400n + 5;
lowlat400n = lowlat400n + 5;
highlng400n = highlng400n + 5;
lowlng400n = lowlng400n + 5;
}else if(tlng > lng && tlng < lng + 739999){
//point is within 500 meters on lng
//on the POSITIVE SIDE
layers.addcube(highlat500n, lowlat500n, yhigh, ylow, highlng500p, lowlng500p);
highlat500n = highlat500n + 5;
lowlat500n = lowlat500n + 5;
highlng500p = highlng500p + 5;
lowlng500p = lowlng500p + 5;
}else if(tlng < lng && tlng > lng - 739999){
//point is within 500 meters on lng
//on the NEGATIVE SIDE
layers.addcube(highlat500n, lowlat500n, yhigh, ylow, highlng500n, lowlng500n);
highlat500n = highlat500n + 5;
lowlat500n = lowlat500n + 5;
highlng500n = highlng500n + 5;
lowlng500n = lowlng500n + 5;
}
}
if(tlatlat-89930.7){
//该点位于纬度100米以内
//消极的一面。
如果(液化天然气>液化天然气和液化天然气<液化天然气+147999.8){
//点位于横向负100米范围内&&
//在液化天然气正极100米处
层。添加立方体(高LNG100P、低NG100P、高yhigh、低ylow、高LAT100P、低LAT100P);
高LNG100P=高LNG100P+5;
lowlng100p=lowlng100p+5;
highlat100p=highlat100p+5;
lowlat100p=lowlat100p+5;
}否则,如果(tlnglng-147999.8){
//点位于横向负100米范围内&&
//在液化天然气负压100米处
layers.addcube(高纬度100N、低纬度100N、高纬度、低纬度、高纬度100N、低纬度100N);
highlat100n=highlat100n+5;
lowlat100n=lowlat100n+5;
高LNG100N=高LNG100N+5;
lowlat100n=lowlat100n+5;
}否则,如果(液化天然气>液化天然气和液化天然气<液化天然气+295999.6){
//点位于横向负200米范围内&&
//在液化天然气正极200米处
layers.addcube(高纬度200N、低纬度200N、高纬度、低纬度、高纬度200P、低纬度200N);
highlat200n=highlat200n+5;
lowlat200n=lowlat200n+5;
高LNG200P=高LNG200P+5;
lowlng200p=lowlng200p+5;
}否则,如果(tlnglng-295999.6){
//点位于横向负200米范围内&&
//在液化天然气负极上200米处
layers.addcube(高纬度200N、低纬度200N、高纬度、低纬度、高纬度200N、低纬度200N);
highlat200n=highlat200n+5;
lowlat200n=lowlat200n+5;
高LNG200N=高LNG200N+5;
lowlng200n=lowlng200n+5;
}否则,如果(液化天然气>液化天然气和液化天然气<液化天然气+443999.4){
//点位于横向负300米范围内&&
//在液化天然气正极300米处
层。添加立方体(高纬度300N、低纬度300N、高纬度、低纬度、高纬度300P、低纬度300P);
highlat300n=highlat300n+5;
lowlat300n=lowlat300n+5;
高LNG300P=高LNG300P+5;
lowlng300p=lowlng300p+5;
}否则,如果(tlnglng-443999.4){
//点位于横向负300米范围内&&
//在液化天然气负极上300米处
layers.addcube(高纬度300N、低纬度300N、高纬度、低纬度、高纬度300N、低纬度300N);
highlat300n=highlat300n+5;
lowlat300n=lowlat300n+5;
高LNG300N=高LNG300N+5;
lowlng300n=lowlng300n+5;
}其他条件(液化天然气>液化天然气和液化天然气<液化天然气+591999.2){
//点位于液化天然气上400米以内
//积极的一面
layers.addcube(高纬度400N、低纬度400N、高纬度、低纬度、高纬度400P、低纬度400P);
highlat400n=highlat400n+5;
lowlat400n=lowlat400n+5;
高LNG400P=高LNG400P+5;
lowlng400p=lowlng400p+5;
}其他条件(tlnglng-591999.2){
//点位于液化天然气上400米以内
//消极的一面
layers.addcube(高纬度400N、低纬度400N、高纬度、低纬度400N、高纬度400N、低纬度400N);
highlat400n=highlat400n+5;
lowlat400n=lowlat400n+5;
高LNG400N=高LNG400N+5;
lowlng400n=lowlng400n+5;
}否则,如果(液化天然气>液化天然气和液化天然气<液化天然气+739999){
//点位于液化天然气上500米以内
//积极的一面
层。添加立方体(高纬度500N、低纬度500N、高纬度、低纬度、高纬度500P、低纬度500P);
highlat500n=highlat500n+5;
lowlat500n=lowlat500n+5;
高LNG500P=高LNG500P+5;
lowlng500p=lowlng500p+5;
}如果出现其他情况(tlnglng-739999){
//点位于液化天然气上500米以内
//消极的一面
层。添加立方体(高纬度500N、低纬度500N、高纬度、低纬度、高纬度500N、低纬度500N);
highlat500n=highlat500n+5;
lowlat500n=lowlat500n+5;
高LNG500N=高LNG500N+5;
lowlng500n=lowlng500n+5;
}
}
如果有人能帮我使我想做的事情更有效率,我将不胜感激!
谢谢,一种方法是制作一个单元格网格,并使用点到原点的距离和单元格的大小来计算点进入哪个单元格。计算单元的公式是O(1)-因此其效率不取决于网格的大小。这是对一些未编译且未经测试的代码的粗略尝试,因此您可能必须对其进行修改以使其正常工作:
public class SquareGrid
{
/** Lower, left corner of the grid */
private Point2D m_origin;
private double m_cellSize;
private List<List<List<Point2D>>> m_cells;
public SquareGrid(Point2D origin, int numberOfCellsPerSide, double cellSize)
{
m_origin = origin;
m_cellSize = cellSize;
m_cells = new ArrayList<List<List<Point2D>>>(numberOfCellsPerSide);
for (int i = 0; i < numberOfCellsPerSide; i++)
{
List<List<Point2D>> row = new ArrayList<List<Point2D>>(numberOfCellsPerSide);
m_cells.add(row);
for (int j = 0; j < numberOfCellsPerSide; j++)
{
row.add(new ArrayList<Point2D>>());
}
}
}
public void add(Point2D point)
{
int xCell = Math.floor((point.getX() - m_origin.getX()) / m_cellSize);
int yCell = Math.floor((point.getY() - m_origin.getY()) / m_cellSize);
m_cells.get(xCell).get(yCell).add(point);
}
}
公共类SquareGrid
{
/**网格左下角*/
私有点2D m_原点;
私人双m_单元大小;
私有列表m_单元;
公共方格线(点2D原点,int numberOfCellsPerSide,双倍cellSize)
{
m_原点=原点