我们参照 http://www.u3d8.com/?p=601 这个h5教程中的例子去做,这次需求是检测是否在屏幕上画出了圆形。上代码:
首先 图形算法脚本:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
using UnityEngine; using System.Collections; using System.Collections.Generic; public class FigureGesture { public List<Vector2> swipeDatas; public bool isCircle = false; public void Init() { swipeDatas = new List<Vector2>(); } /// <summary> /// 数据降噪操作 /// </summary> public void Motion() { List<Vector2> _arr = new List<Vector2>(); int currentIndex = 0; int len = swipeDatas.Count; _arr.Add(this.swipeDatas[currentIndex]); for(int i = 0; i<len; i++) { if (Vector2.Distance(this.swipeDatas[currentIndex], this.swipeDatas[i]) > 10) { currentIndex = i; _arr.Add(this.swipeDatas[currentIndex]); } } this.swipeDatas = _arr; this.ParseDirection(); } /// <summary> /// 将采样数据转换为方向序列 /// </summary> private List<float> dirsArr; private void ParseDirection() { dirsArr = new List<float>(); int len = this.swipeDatas.Count; for(int i = 0; i<len - 1; i++) { Vector2 p1 = this.swipeDatas[i]; Vector2 p2 = this.swipeDatas[i + 1]; float a = p1.y - p2.y; float b = Vector2.Distance(p1, p2); float rad = Mathf.Asin(a / b); float ang = rad * 57.2957800f; // rad * 180/Math.PI 直接求常量,优化 int quad = Quadrant(p1, p2); int dir = GetDirByAngQuad(ang, quad); dirsArr.Add(dir); } string dirstr = RepDiff(); JudgeIfCircle(dirstr); } /// <summary> /// 计算两点关系所形成的象限 /// 以P1 作为坐标原点,P2为设定点,判断P2相对于P1时所在象限 /// </summary> /// <param name="p1"></param> /// <param name="p2"></param> /// <returns></returns> private int Quadrant(Vector2 p1, Vector2 p2) { if(p2.x>=p1.x) { if( p2.y <= p1.y ) { return 1; } else { return 4; } } else { if( p2.y <= p1.y ) { return 2; } else { return 3; } } } /// <summary> /// 根据所在象限与角度计算出方向编号。 /// 方向编号,以第一象限0度为基础,按照顺时针方向,将圆等分为8份 /// </summary> private int GetDirByAngQuad(float ang, int quad) { switch(quad) { case 1: if( ang<=22.5 && ang>= 0 ) { return 1; } else if( ang<= 67.5 && ang> 22.5 ) { return 8; } else { return 7; } break; case 2: if( ang<=22.5 && ang>=0 ) { return 5; } else if( ang<= 67.5 && ang> 22.5 ) { return 6; } else { return 7; } break; case 3: if( ang<= -67.5 && ang>= -90 ) { return 3; } else if( ang<=-22.5 && ang> -67.5 ) { return 4; } else{ return 5; } break; case 4: if( ang<=-67.5 && ang>= -90 ) { return 3; } else if( ang<=-22.5 && ang>-67.5) { return 2; } else{ return 1; } break; } return 0; } /// <summary> /// 对比去重 /// </summary> /// <returns></returns> private string RepDiff() { string str = ""; int len = dirsArr.Count; int currentType = 0; for(int i=0; i<len; i++) { if( currentType != dirsArr[i]) { currentType = (int)dirsArr[i]; str += dirsArr[i]; } } return str; } /// <summary> /// 判断是否为圆形 /// </summary> /// <param name="str"></param> private void JudgeIfCircle(string str) { List<int> ints = new List<int>(); for (var i = 0; i < str.Length; i++) { ints.Add(int.Parse(str[i].ToString())); } int wrongNum1 = 0; // 记录每三个连续数字是否其中任意两个都不挨着 则+1 如:153 758 int wrongNum11 = 0; // 记录每两个连续数字 不挨着则 +1 如: 63 72 for (var j = 0; j < ints.Count - 1; j++) { if (j > 0 && ints[j - 1] - ints[j] != 1 && ints[j] - ints[j + 1] != 1) { wrongNum1++; } if (ints[j] - ints[j + 1] != 1) { wrongNum11++; } } int wrongNum2 = 0; int wrongNum22 = 0; for (var j = 0; j < ints.Count - 1; j++) { if (j > 0 && ints[j + 1] - ints[j] != 1 && ints[j] - ints[j - 1] != 1) { wrongNum2++; } if (ints[j + 1] - ints[j] != 1) { wrongNum22++; } } //Debug.Log(str + " w1: " + wrongNum1 + " w11: " + wrongNum11 + " w2: " + wrongNum2 + " w22: " + wrongNum22); if (ints.Count >= 8 && ((wrongNum1 == 0 && wrongNum11 <= 2) || (wrongNum1 == 1 && wrongNum11 <= 1) || (wrongNum2 == 0 && wrongNum22 <= 2) || (wrongNum2 == 1 && wrongNum22 <= 1))) { isCircle = true; } else { isCircle = false; } //GevekInputTest.test1Log.text = (isCircle + " 序号: " + str + '\n' + "w1: " + wrongNum1 + " w11: " + wrongNum11 + '\n' + "w2: " + wrongNum2 + " w22: " + wrongNum22); } } |
下面为手指按下时事件响应:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
using UnityEngine; using System.Collections; public class Test : MonoBehaviour { FigureGesture fg; void Start() { fg = new FigureGesture(); } void Update() { if (Input.GetMouseButtonDown(0)) { fg.Init(); fg.swipeDatas.Add(Input.mousePosition); } else if (Input.GetMouseButton(0)) { fg.swipeDatas.Add(Input.mousePosition); } else if (Input.GetMouseButtonUp(0)) { fg.swipeDatas.Add(Input.mousePosition); fg.Motion(); if (fg.isCircle) { Debug.Log("画出了圆形"); } } } } |
随意创建个空的工程,将Test类脚本放在摄像机上,就可以测试了。
- 本文固定链接: http://www.u3d8.com/?p=609
- 转载请注明: 网虫虫 在 u3d8.com 发表过