private static GrindNode GetNode(Vector3 p, Rect _bound, bool _is3D) { float ratioX = 0f; float ratioY = 0f; if (_is3D) { ratioX = (p.x - _bound.x) / (_bound.width); ratioY = (p.z - _bound.y) / (_bound.height); } else { ratioX = (p.x - _bound.x) / (_bound.width); ratioY = (p.y - _bound.y) / (_bound.height); } // 逆转Y值 ratioY = 1f - ratioY; ratioX = Mathf.Clamp01(ratioX); ratioY = Mathf.Clamp01(ratioY); GrindNode n = new GrindNode(); n.x = Mathf.RoundToInt(ratioX * (ColumnCount - 1)); n.y = Mathf.RoundToInt(ratioY * (RowCount - 1)); return(n); }
// public override bool OpenPort() // { // if (base.OpenPort()) // { // int udp = controller.gameConfig.GetValue("UDP", -1); // //如果监测udp开启了,则使用udp做为3D传感器的端口 // if (udp >= 0) // { // m7bPort = new LMInput_UDP(); // (m7bPort as LMInput_UDP).Init(controller, KeyportData, udp); // } // //否则使用serial port做为3D传感器端口 // else // { // m7bPort = new LMInput_Port(); // (m7bPort as LMInput_Port).Init(controller, // KeyportData, // controller.gameConfig.GetValue("端口2", -1)); // } // return true; // } // return false; // } public static GrindNode[] PathToNodes(Vector3[] _path, Rect _bound, bool _is3D) { // 用来保存最后一次的编号,以避免重复发送同样的编号 string lastCode = string.Empty; // 记录最新的编号 string newCode = string.Empty; GrindNode lastNode = new GrindNode(); List <GrindNode> nodes = new List <GrindNode>(); for (int i = 0; i < _path.Length; i++) { var tmpNode = GetNode(_path[i], _bound, _is3D); Debug.Log("Get Node: " + tmpNode.x + ", " + tmpNode.y); if (tmpNode == lastNode) { continue; } nodes.Add(tmpNode); lastNode = tmpNode; // // 测试时候发送到模拟器 // if( TGData.IsTesting ) { // GrindTableEmu.SetBtnEnable( tmpNode.x, tmpNode.y ); // } } return(nodes.ToArray()); }
public void DrawLine(int x0, int y0, int x1, int y1) { var startNode = new GrindNode() { x = x0, y = y0 }; var endNode = new GrindNode() { x = x1, y = y1 }; Write(GetLines(startNode, endNode)); }
public static Vector3 NodeToVector(GrindNode node, Rect bound, bool is3D) { float ratioX = ( float )node.x / (ColumnCount - 1); float ratioY = 1f - ( float )node.y / (RowCount - 1); float rx = ratioX * bound.width + bound.x; float ry = ratioY * bound.height + bound.y; Debug.Log(string.Format("Rx: {0}. Ry: {1}", ratioX, ratioY)); if (is3D) { return(new Vector3(rx, 0f, ry)); } return(new Vector3(rx, ry)); }
private void DrawCirc(int cx, int cy, int tx, int ty, int fromDeg, int toDeg, ref List <GrindNode> nodes, bool counterClockwise) { // 判断要画的点是否在有效区域,并且角度是否在起始角度与终点角度之间 if (Within(cx + tx, cy + ty) && WithinAngle(cx, cy, cx + tx, cy + ty, fromDeg, toDeg, counterClockwise)) { var n = new GrindNode() { x = cx + tx, y = cy + ty }; if (!nodes.Contains(n)) { // Debug.Log( "Add Node: " + n ); nodes.Add(n); } } }
private string NodeToCode(GrindNode node) { int colIndex = 65 + node.x; int rowIndex = 65 + node.y; // 直接跳过从Y开始到a之前的字符 if (colIndex >= 89) { colIndex += SKIP_LENGTH; } if (rowIndex >= 89) { rowIndex += SKIP_LENGTH; } char colChar = ( char )colIndex; char rowChar = ( char )rowIndex; string retval = colChar.ToString() + rowChar.ToString(); Debug.Log(string.Format("Writing code x={0} y={1}, return {2}", colIndex, rowIndex, retval)); return(retval); }
private GrindNode[] GetLines(GrindNode a, GrindNode b) { List <GrindNode> nodes = new List <GrindNode>(); int x0 = a.x; int y0 = a.y; int x1 = b.x; int y1 = b.y; // 检测是否大斜率 bool steep = Mathf.Abs(y1 - y0) > Mathf.Abs(x1 - x0); // 如果大斜率,则把xy掉转,小斜率比较好计算,在取值时候调过来就好了 if (steep) { Swap(ref x0, ref y0); Swap(ref x1, ref y1); } // 只从左往右画线 // 如果发现起点在右手边,则把两个点调换 // 最后再把列表倒转就好了 bool mirror = x0 > x1; if (mirror) { Swap(ref x0, ref x1); Swap(ref y0, ref y1); } int dy = y1 - y0; int dx = x1 - x0; // 检测往上画还是往下画 int yStep = (y1 > y0) ? 1 : -1; // 表示是一条竖线 if (dx == 0) { int yLength = Mathf.Abs(dy); for (int y = 0; y < yLength; y++) { int fy = y0 + y * yStep; nodes.Add(new GrindNode() { x = x0, y = fy }); } } else { int de = 2 * Mathf.Abs(dy); int e = 0; int ty = y0; for (int tx = x0; tx <= x1; tx++) { if (steep) { // 如果是大斜率的,记得把xy调转回来 nodes.Add(new GrindNode() { x = ty, y = tx }); } else { Debug.LogWarning(x0); nodes.Add(new GrindNode() { x = tx, y = ty }); } e += de; if (e > dx) { ty += yStep; e -= 2 * dx; } } } if (mirror || steep) { nodes.Reverse(); } return(nodes.ToArray()); }
public void DrawLine(GrindNode from, GrindNode to) { Write(GetLines(from, to)); }