package com.jleo.pickitup;
import org.apache.commons.collections.list.TreeList;
import java.math.BigDecimal;
import java.util.*;
public class Norm{
public static double normrnd(double mu, double sigma) {
double N = 12;
double x = 0, temp = N;
do {
x = 0;
for (int i = 0; i < N; i++)
x = x + (Math.random());
x = (x - temp / 2) / (Math.sqrt(temp / 12));
x = mu + x * Math.sqrt(sigma);
} while (x <= 0);
return x;
}
public static double[] addInv(double[] r) {
double[] result = new double[r.length * 2];
for (int i = 0; i < result.length; i += 2) {
result[i] = r[i / 2];
result[i + 1] = -r[i / 2];
}
return result;
}
public static double[] normrnd(double sigma, int num) {
double[] r = new double[num];
for (int i = 0; i < num; i++) {
r[i] = normrnd(0, sigma);
}
return addInv(r);
}
public static Point[] generateMap(int x1, int x2, int y1, int y2, int numberOfDots,int numberOfBlock) {
double mu = 3;
double sigma = 1;
int[] x = new int[numberOfDots];
int[] y = new int[numberOfDots];
int xDevideBy = (x2 - x1)/numberOfBlock;
int yDevideBy = (y2 - y1)/numberOfBlock;
Point[] points = new Point[numberOfDots];
for (int i = 0; i < numberOfDots; i++) {
x[i] = (int) ((normrnd(mu, sigma)-3)*((x2-x1)/6)+(x1+x2)/2);
y[i] = (int) ((normrnd(mu, sigma)-3)*((y2-y1)/6)+(y1+y2)/2);
points[i] = new Point(x[i], y[i], (x[i]-x1)/xDevideBy,(y[i]-y1)/yDevideBy);
}
return points;
}
public static String[] getNeighbours(String zoneId){
int zonex = Integer.valueOf(zoneId.split(",")[0]);
int zoney = Integer.valueOf(zoneId.split(",")[1]);
String[] neighbours = new String[9];
int idx = 0;
for(int x= zonex-1;x<=zonex+1;x++){
for(int y= zoney-1;y<=zoney+1;y++){
neighbours[idx] = x+","+y;
idx++;
}
}
return neighbours;
}
public static void main(String[] args) {
int x1 = 31095278;
int x2 = 31334871;
int y1 = 121256104;
int y2 = 121629639;
int numberOfDots = 50000000;
int numberOfBlock = 150;
Point[] points = Norm.generateMap(x1, x2, y1, y2, numberOfDots, numberOfBlock);
Map<String, List<Point>> blockMap = new HashMap<String, List<Point>>();
for(int i=0; i<points.length; i++){
Point p = points[i];
String zoneId = p.zonex+","+p.zoney;
if(!blockMap.containsKey(zoneId)){
List<Point> ps = new ArrayList<Point>();
ps.add(p);
blockMap.put(zoneId,ps);
} else{
blockMap.get(zoneId).add(p);
}
}
long t1 = System.currentTimeMillis();
String zoneid = points[1245].zonex + "," + points[1245].zoney;
String[] neighbours = getNeighbours(zoneid);
List<Point> pointsInNeighbours = new ArrayList<Point>();
for(int i=0;i<neighbours.length; i++){
if(blockMap.containsKey(neighbours[i]))
pointsInNeighbours.addAll(blockMap.get(neighbours[i]));
}
TreeMap tm = new TreeMap();
for(int i=0; i<pointsInNeighbours.size(); i++){
if(pointsInNeighbours.get(i) == points[1245])
continue;
tm.put(points[1245].distanceTo(pointsInNeighbours.get(i)), pointsInNeighbours.get(i));
}
long t2 = System.currentTimeMillis();
System.out.println(t2-t1);
System.out.println(tm.firstKey());
long t3 = System.currentTimeMillis();
tm = new TreeMap();
for(int i=0; i<points.length; i++){
if(points[i] == points[1245])
continue;
tm.put(points[1245].distanceTo(points[i]), points[i]);
}
long t4 = System.currentTimeMillis();
System.out.println(t4-t3);
System.out.println(tm.firstKey());
}
}
aas
assas
最新推荐文章于 2024-04-30 16:43:26 发布