利用C#实现绘制出地球旋转效果
作者:微小冷 时间:2022-10-01 14:21:27
将方形的图像映射到正方形上似乎并没有什么难度,所以接下来要做的是把图像映射到球面上。
而球的参数方程为
x=rcosϕcosθ
y=rcosϕsinθ
z=rsinϕ
由于r的值是恒定的,所以生成球的关键参数就是θ, φ而把地图贴在球上就相当于把图像坐标(x,y)映射到坐标θ,φ上。
其中地图为
private MeshGeometry3D SetEarth(int numx, int numz, double r=3)
{
MeshGeometry3D mesh = new MeshGeometry3D();
double dTh = 2 * Math.PI / numx;
double dPhi = Math.PI / numz;
double X(double th, double phi) => r * Math.Sin(phi) * Math.Cos(th);
double Y(double th, double phi) => r * Math.Sin(phi) * Math.Sin(th);
double Z(double phi) => r * Math.Cos(phi);
// Make the points.
for (int i = 0; i <= numx; i++)
for (int j = 0; j <= numz; j++)
{
var th = i * dTh;
var phi = j * dPhi;
mesh.Positions.Add(new Point3D(X(th, phi), Y(th, phi), Z(phi)));
mesh.TextureCoordinates.Add(new Point(th, phi));
}
// 生成三角形
for (int i = 0; i < numx; i++)
for (int j = 0; j < numz; j++)
{
int i1 = i * (numz + 1) + j;
int i2 = i1 + 1;
int i3 = i2 + (numz + 1);
int i4 = i3 - 1;
mesh.TriangleIndices.Add(i1);
mesh.TriangleIndices.Add(i2);
mesh.TriangleIndices.Add(i3);
mesh.TriangleIndices.Add(i1);
mesh.TriangleIndices.Add(i3);
mesh.TriangleIndices.Add(i4);
}
return mesh;
}
生成的地球为
如果生成之后地球躺平了,可以调整一下LookDirection。
如果仅仅是这样,那显然是不行的,因为只看到一个二维的圆形,无法让人觉得这是个地球,接下来就要让这个球转起来。
方法是新建一个Timer,
public MainWindow()
{
InitializeComponent();
initialize3D();
timer.Interval = TimeSpan.FromMilliseconds(5);
timer.Tick += Timer_Tick;
timer.Start();
}
然后随着时间而旋转
double thAngle = 0;
private void Timer_Tick(object? sender, EventArgs e)
{
thAngle = thAngle > Math.PI ? 0 : thAngle + 0.1;
Rotation3D rotation = new AxisAngleRotation3D(new Vector3D(0,0,1), thAngle);
var rot = new RotateTransform3D(rotation, new Point3D(0,0,0));
Point3D[] pts = mesh.Positions.ToArray();
rot.Transform(pts);
mesh.Positions = new Point3DCollection(pts);
}
结果为
来源:https://tinycool.blog.csdn.net/article/details/124281244
标签:C#,地球,旋转
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#实现char字符数组与字符串相互转换的方法
2022-01-18 05:28:52
详解java.lang.reflect.Modifier.isInterface()方法
2023-07-27 18:25:25
C# 创建高精度定时器的示例
2023-03-09 12:35:58
![](https://img.aspxhome.com/file/2023/4/76434_0s.png)
java的引用类型的详细介绍
2022-05-15 09:34:40
详解SpringBoot通用配置文件(不定时更新)
2022-12-01 09:07:04
BeanDefinitionRegistryPostProcessor如何动态注册Bean到Spring
2023-11-24 12:56:16
![](https://img.aspxhome.com/file/2023/1/59941_0s.jpg)
通过FeignClient调用微服务提供的分页对象IPage报错的解决
2022-01-27 20:19:23
![](https://img.aspxhome.com/file/2023/7/63057_0s.png)
springcloud eureka切换nacos的配置方法
2022-05-19 01:58:47
WPF实现XAML转图片的示例详解
2023-09-13 21:38:51
![](https://img.aspxhome.com/file/2023/3/80093_0s.png)
详解eclipse下创建第一个spring boot项目
2022-01-02 10:36:13
![](https://img.aspxhome.com/file/2023/7/78357_0s.png)
springboot读取application.yaml文件数据的方法
2023-09-06 05:29:24
![](https://img.aspxhome.com/file/2023/6/63086_0s.jpg)
解决MyEclipse10.7部署报错抛空指针异常问题的方法
2023-10-14 23:52:34
![](https://img.aspxhome.com/file/2023/5/82315_0s.jpg)
Java面向对象之猜拳游戏
2022-10-29 21:26:45
![](https://img.aspxhome.com/file/2023/8/80208_0s.jpg)
java开发分布式服务框架Dubbo原理机制详解
2023-01-04 19:53:01
![](https://img.aspxhome.com/file/2023/0/71210_0s.png)
SpringCloud Feign多参数传递及需要注意的问题
2022-05-25 11:11:58
![](https://img.aspxhome.com/file/2023/8/71848_0s.png)
Java DecimalFormat 保留小数位及四舍五入的陷阱介绍
2023-11-09 04:49:33
JAVA SFTP文件上传、下载及批量下载实例
2023-02-11 14:31:46
Android中加入名片扫描功能实例代码
2023-07-26 12:16:15
![](https://img.aspxhome.com/file/2023/2/86002_0s.png)
Java 线程池ExecutorService详解及实例代码
2022-09-02 17:07:24
一文带你了解Java万物之基之Object类
2023-10-09 01:58:28
![](https://img.aspxhome.com/file/2023/9/67119_0s.png)