zoukankan      html  css  js  c++  java
  • UWP开发中的方向传感器

      在UWP开发中,我们能使用的到方向有三种: OrientationSensor下的四元数;Compass罗盘的HeadingMagneticNorth;以及SimpleOrientationSensor。

      先说下SimpleOrientationSensor,这个是用在比较简单的情况下,它只能读取6个方向:旋转0,90,180,270,FaceUP,FaceDown。

            var simpleOrien = SimpleOrientationSensor.GetDefault().GetCurrentOrientation();
                switch (simpleOrien)
                {
                    case SimpleOrientation.Facedown:
                        // To do ...
                        break;
                    case SimpleOrientation.Faceup:
                        // To do ...
                        break;
                    case SimpleOrientation.NotRotated:
                        // To do ...
                        break;
                    case SimpleOrientation.Rotated180DegreesCounterclockwise:
                        // To do ...
                        break;
                    case SimpleOrientation.Rotated270DegreesCounterclockwise:
                        // To do ...
                        break;
                    case SimpleOrientation.Rotated90DegreesCounterclockwise:
                        // To do ...
                        break;
                }

    罗盘:

            var compass = Compass.GetDefault().GetCurrentReading();
                var angle= compass.HeadingMagneticNorth;

    使用很简单,但是它读取的似乎是你行进的方向,也就是说假如你站着不动的话,读数是没有意义的。

    方向传感器:

            var _sensor = OrientationSensor.GetDefault();
                var quaternion = _sensor.GetCurrentReading().Quaternion;
                var heading = 180 * Math.Atan2(2 * (quaternion.W * quaternion.Z + quaternion.X * quaternion.Y), 1 - 2 * (quaternion.Y * quaternion.Y + quaternion.Z * quaternion.Z)) / Math.PI;

    这个可能要稍微麻烦一点,因为它涉及到四元数,有关四元数的知识请自行google或翻书

  • 相关阅读:
    【css】容器撑满浏览器--- height:100%
    【实践】js六道有趣的题
    【Canvas】树冠
    asp.net上传Excel文件到服务端进行读取
    HttpContext.Current多线程调用
    abstract修饰符,具体类与抽象类的区别
    如何快速恢复MyEclipse的默认主题
    日历源代码
    for语句应用:乘法表
    Java的优先级
  • 原文地址:https://www.cnblogs.com/Yixin-ran/p/6069542.html
Copyright © 2011-2022 走看看