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或翻书

  • 相关阅读:
    扫描线与悬线
    随机搜索与模拟退火
    树的直径相关
    分数规划及斜率优化
    数学-剩余系
    后缀数据结构
    AC自动机和KMP
    生命游戏和随机数之间某种不可言说的秘密
    转移了
    BZOJ 1710: [Usaco2007 Open]Cheappal 廉价回文
  • 原文地址:https://www.cnblogs.com/Yixin-ran/p/6069542.html
Copyright © 2011-2022 走看看