C#原链接:http://www.codeproject.com/Articles/19712/Mapping-Images-on-Spherical-Surfaces-Using-C
//java代码:不知道为什么改成java的精度降低了,水平有限啊
double phi0 = 0.0;
double phi1 = Math.PI;
double theta0 = 0.0;
double theta1 = 2.0*Math.PI;
double x0,y0,z0;
//计算经纬度
public static double MapCoordinate(double i1, double i2, double w1,double w2, double p){
return ((p - i1) / (i2 - i1)) * (w2 - w1) + w1;
}
public void draw(int radius,Graphics g){ //半径和Graphics上下文
for (int i = 0; i < w; i++) //图像宽度
{
for (int j = 0; j < h; j++) //图像高度
{
// map the angles from image coordinates
double theta = MapCoordinate(0.0, w - 1,theta1, theta0, i);//经度
double phi = MapCoordinate( 0.0, h - 1,phi0, phi1, j);//纬度
// find the cartesian coordinates //经纬度映射到大圆平面的座标
x0 = radius * Math.sin(phi) * Math.cos(theta);
y0 = radius * Math.sin(phi) * Math.sin(theta);
z0 = radius * Math.cos(phi);
// apply rotation around X and Y axis to reposition the sphere
RotX(1.5, y0, z0);
RotY(-2.5, x0, z0);
// plot only positive points
if (z0 > 0){
g.setColor(new Color(pix[i+j*w]));//pix[w*h]为图像的像素数组
int ix = (int)x0 + 150; //java可以用BufferedImage 对象的
int iy = (int)y0 + 150; //getRGB(0, 0,w , h, pix, 0, w)方法获得
g.fillOval(ix,iy,5,5);//没有找到setPoint函数用画圆代替了.
}
}
}
}
public void RotX(double angle, double y, double z)//旋转
{
double y1 = y * Math.cos(angle) - z * Math.sin(angle);
double z1 = y * Math.sin(angle) + z * Math.cos(angle);
y0 = y1;
z0 = z1;
}
public void RotY(double angle, double x, double z)
{
double x1 = x * Math.cos(angle) - z * Math.sin(angle);
double z1 = x * Math.sin(angle) + z * Math.cos(angle);
x0 = x1;
z0 = z1;
}
public void RotZ(double angle, double x, double y)
{
double x1 = x * Math.cos(angle) - y * Math.sin(angle);
double y1 = x * Math.sin(angle) + y * Math.cos(angle);
x0 = x1;
y0 = y1;
}