我正在尝试编写一个应用程序来显示给定日期、时间和位置的太阳路径。我已经计算出太阳位置的计算结果,但我需要计算手机的主摄像头是否正对着太阳(或者太阳在某个日期和时间的位置),然后显示太阳的图像在手机屏幕上与现实生活中的位置相对应。
最好的方法是什么?我知道这与以某种方式转换手机的旋转矩阵并以某种方式使用手机相机的 FOV 有关。
或者,如果有人知道如何计算手机主摄像头的方位角和仰角,我可以(也许?)从那里算出其余的。
我不会撒谎我不知道如何解决这个问题,我问了 chatGPT 并得到了这样的东西:
fun getScreenCoords(R: FloatArray, azimuth: Double, elevation: Double): Pair<Float, Float> {
// Convert the azimuth and elevation to radians
val azimuthRadians = Math.toRadians(azimuth.toDouble())
val elevationRadians = Math.toRadians(elevation.toDouble())
// Calculate the vector pointing to the sun in 3D space
val x = Math.cos(azimuthRadians) * Math.cos(elevationRadians)
val y = Math.sin(azimuthRadians) * Math.cos(elevationRadians)
val z = Math.sin(elevationRadians)
// Create a float array containing the vector coordinates
val vector = floatArrayOf(x.toFloat(), y.toFloat(), z.toFloat(), 1.0f)
// Create a new float array to hold the transformed coordinates
val transformedVector = FloatArray(4)
// Multiply the rotation matrix by the vector to transform it to the device coordinate system
Matrix.multiplyMV(transformedVector, 0, R, 0, vector, 0)
// Normalize the transformed vector by dividing by its W coordinate
transformedVector[0] /= transformedVector[3]
transformedVector[1] /= transformedVector[3]
transformedVector[2] /= transformedVector[3]
// Get the screen dimensions
val displayMetrics = Resources.getSystem().displayMetrics
val screenWidth = displayMetrics.widthPixels
val screenHeight = displayMetrics.heightPixels
// Convert the normalized vector coordinates to screen coordinates
val xScreen = ((transformedVector[0] + 1.0f) / 2.0f * screenWidth)
val yScreen = ((1.0f - transformedVector[1]) / 2.0f * screenHeight)
// Return the screen coordinates as a Pair
return Pair(xScreen, yScreen)
}
这种功能在于您可以四处移动手机,图像会以某种可预测的方式移动,但肯定还有很长的路要走。