Example #1
0
            public Node getMinimalAgleNode(Vector2 direction)
            {
                Vector2 fromDir  = direction;
                float   minAngle = float.MaxValue;
                Node    minNode  = null;

                foreach (Node n in nodes)
                {
                    float ca = Vector2Utillites.AngleFromTo(fromDir, (position - n.position).normalized);
                    if (ca < minAngle)
                    {
                        minAngle = ca;
                        minNode  = n;
                    }
                }
                return(minNode);
            }
Example #2
0
        private void getTris()
        {
            foreach (Connection c in connections)
            {
                Node left  = c.node1.position.x < c.node2.position.x ? c.node1 : c.node2;
                Node right = c.node1 == left ? c.node2 : c.node1;



                Node up = right.getMinimalAgleNode(left);
                if (up == null)
                {
                    continue;
                }
                //if (down.position.y < left.position.y || down.position.y < right.position.y) continue;
                if (Vector2Utillites.AngleFromTo(right.position - left.position, right.position - up.position) > 180)
                {
                    continue;
                }
                //if (Vector2Utillites.AngleFromTo(right.position - left.position, right.position - up.position) > 180) continue;
                //if (left.position.x > right.position.x) continue;
                //if(up.position.y < right.position.y) continue;
                //if (left.position.y < up.position.y && right.position.y < up.position.y) continue;
                //if (!correctTris(left.index, right.index, up.index)) continue;
                //if (up.position.y  > Math.Max(left.position.y, right.position.y)) continue;
                //if (left.position.y < right.position.y) continue;

                //Node vup = left.position.y > right.position.y ? left : right;
                //if (up.position.y > vup.position.y) vup = up;
                //Node vl =


                if (up.nodes.Contains(left))
                {
                    //Debug.Log(left.index.ToString() + " " + right.index.ToString() + " " + up.index.ToString());
                    Triangle t = new Triangle(left, up, right);
                    if (!trises.Contains(t))
                    {
                        //Debug.Log(t.index1.ToString() + " " + t.index2.ToString() +" " +t.index3.ToString());
                        trises.Add(t);
                    }
                }
            }
            // Debug.Log(trises.Count);
        }
Example #3
0
            public Node getMinimalAgleNode(Node fromNode)
            {
                Vector2 fromDir  = (position - fromNode.position).normalized;
                float   minAngle = float.MaxValue;
                Node    minNode  = null;

                foreach (Node n in nodes)
                {
                    if (n == fromNode)
                    {
                        continue;
                    }
                    float ca = Vector2Utillites.AngleFromTo(fromDir, (position - n.position).normalized);
                    if (ca < minAngle)
                    {
                        minAngle = ca;
                        minNode  = n;
                    }
                }
                return(minNode);
            }
Example #4
0
            public Triangle(Node n1, Node n2, Node n3)
            {
                Node left;
                Node o1;
                Node o2;

                if (n1.position.x <= n2.position.x && n1.position.x < n3.position.x)
                {
                    left = n1;
                    o1   = n2;
                    o2   = n3;
                }
                else
                if (n2.position.x <= n1.position.x && n2.position.x < n3.position.x)
                {
                    left = n2;
                    o1   = n1;
                    o2   = n3;
                }
                else
                {
                    left = n3;
                    o1   = n1;
                    o2   = n2;
                }


                if (Vector2Utillites.AngleFromTo(left.position - o1.position, left.position - o2.position) >= 180f)
                {
                    index1 = o1.index;
                    index2 = left.index;
                    index3 = o2.index;
                }
                else
                {
                    index1 = o2.index;
                    index2 = left.index;
                    index3 = o1.index;
                }
            }
Example #5
0
            public void triangulate()
            {
                int countC = 0;

                foreach (Node n in allNodes)
                {
                    foreach (Node n2 in allNodes)
                    {
                        if (n2.isBigLoop && n.isBigLoop)
                        {
                            int   ind  = this.edgeNodes.IndexOf(n);
                            Node  back = edgeNodes[ind > 0 ? ind - 1 : this.edgeNodes.Count - 1];
                            Node  next = edgeNodes[ind < this.edgeNodes.Count - 1 ? ind + 1 : 0];
                            float a    = Vector2Utillites.AngleFromTo(n.position - back.position, n.position - next.position);
                            float ca   = Vector2Utillites.AngleFromTo(n.position - back.position, n.position - n2.position);
                            //if (a == 0 && ca == 180f) continue;
                            //if (a == 180 && ca == 0f) continue;
                            if (Mathf.DeltaAngle(a, ca) == 180)
                            {
                                continue;
                            }
                            if (ca <= a)
                            {
                                continue;
                            }
                        }
                        if (n2 != n && !n.nodes.Contains(n2))
                        {
                            n.needCheck.Add(n2);
                            countC++;
                        }
                    }
                    sortn = n;
                    n.needCheck.Sort(sortDistance);
                }
                // Debug.Log(countC);
                int errorC = 0;

                while (countC > 0)
                {
                    if (errorC > 100000)
                    {
                        Debug.Log("Error count check connection");
                        break;
                    }
                    errorC++;
                    foreach (Node n in allNodes)
                    {
                        countC--;
                        if (n.needCheck.Count == 0)
                        {
                            continue;
                        }
                        Node nc = n.needCheck[0];

                        if (!triangulator.intractConnections(n, nc))
                        {
                            n.nodes.Add(nc);
                            nc.nodes.Add(n);
                            triangulator.connections.Add(new Connection(n, nc));
                            countC--;
                            nc.needCheck.Remove(n);
                        }
                        n.needCheck.Remove(nc);


                        // Debug.Log(countC);
                    }
                }
            }