public static ICollection <IPoint> ImportCrossesFromFile(string path)
        {
            ICollection <IPoint> crosses = new List <IPoint>();

            using (StreamReader reader = new StreamReader(path, Encoding.Default))
            {
                while (reader.EndOfStream == false)
                {
                    string[] line = reader.ReadLine().Trim().Split(' ');

                    if (line.Length != 2)
                    {
                        continue;
                    }

                    double x = double.Parse(line[0]);
                    double y = double.Parse(line[1]);

                    IPoint cross = new BoundaryPoint(x, y);

                    crosses.Add(cross);
                }
            }

            return(crosses);
        }
Ejemplo n.º 2
0
    EdgeInfo FindEdge(BoundaryPoint bp1, BoundaryPoint bp2)
    {
        var min = bp1;
        var max = bp2;

        for (var i = 0; i < EdgeResolveIterations; i++)
        {
            var angle = min.angle + (max.angle - min.angle) / 2;
            var bp    = FindBoundaryPoint(angle);

            if (bp.hit == min.hit)
            {
                min = bp;
            }
            else
            {
                max = bp;
            }
        }

        min.debug = Color.white;
        max.debug = Color.white;

        return(new EdgeInfo
        {
            pointA = min,
            pointB = max
        });
    }
Ejemplo n.º 3
0
        public void IsLeftSide_ShouldReturnCorrectResult(int x, int y, bool result)
        {
            IPoint lineStartPoint = new BoundaryPoint(14.3429, 17.5163);
            IPoint lineEndPoint   = new BoundaryPoint(29.9575, 32.0665);
            ICross point          = new Cross(x, y);

            Assert.AreEqual(result, GeometryHelper.IsLeftSide(lineStartPoint, lineEndPoint, point));
        }
Ejemplo n.º 4
0
        public static IPoint[] CreatePolygonFromPolyline(Polyline polyline)
        {
            IPoint[] polygon = new IPoint[polyline.NumberOfVertices];

            for (int vertexID = 0; vertexID < polyline.NumberOfVertices; vertexID++)
            {
                Point2d vertex = polyline.GetPoint2dAt(vertexID);

                polygon[vertexID] = new BoundaryPoint(vertex.X, vertex.Y);
            }

            return(polygon);
        }
Ejemplo n.º 5
0
        private static List <MyInkLine> GetLinesFromPoints(Point start, Point end,
                                                           List <BoundaryPoint> boundaryPoints, ColoringBookColoring coloring)
        {
            var lines = new List <MyInkLine>();

            if (boundaryPoints == null || boundaryPoints.Count == 0)
            {
                return(null);
            }

            var didStartReachable  = coloring.IsWithinUserCurrentCell(start);
            var didFinishReachable = coloring.IsWithinUserCurrentCell(end);

            // Set start to be the previous point. If the point is within the cell, equate to being "after" a boundary.
            // We create a line if consecutive after -> before && (after || before) is within users cell (both should be).
            var startBPoint   = new BoundaryPoint(didStartReachable, start);
            var previousPoint = startBPoint;

            // Set end point as reverse and we would want a line *to* there to *from* there as with start.
            var endBPoint = new BoundaryPoint(!didFinishReachable, end);

            boundaryPoints.Add(endBPoint);

            foreach (var currentPoint in boundaryPoints)
            {
                // if after -> before
                if (previousPoint.IsAfterBoundary && !currentPoint.IsAfterBoundary &&
                    coloring.IsWithinUserCurrentCell(currentPoint.Point))
                {
                    if (previousPoint.Equals(startBPoint))
                    {
                        lines.Add(new MyInkLine(previousPoint.Point, currentPoint.Point, false, true));
                    }
                    else if (currentPoint.Equals(endBPoint))
                    {
                        // Boundary to end.
                        lines.Add(new MyInkLine(previousPoint.Point, currentPoint.Point, true, false));
                    }
                    else
                    {
                        // It must be boundary to boundary.
                        lines.Add(new MyInkLine(previousPoint.Point, currentPoint.Point, true, true));
                    }
                }

                previousPoint = currentPoint;
            }

            return(lines);
        }
Ejemplo n.º 6
0
        public static bool InsidePolygon(Polyline polygon, ICross point)
        {
            int numberOfVertices = polygon.GetPoint2dAt(0) == polygon.GetPoint2dAt(polygon.NumberOfVertices - 1) ? polygon.NumberOfVertices - 1 : polygon.NumberOfVertices;

            for (int vertexID = 0; vertexID < numberOfVertices; vertexID++)
            {
                int nextVertexID = vertexID + 1 == numberOfVertices ? 0 : vertexID + 1;

                Point2d currentVertex = polygon.GetPoint2dAt(vertexID);
                Point2d nextVertex    = polygon.GetPoint2dAt(nextVertexID);

                BoundaryPoint lineStartPoint = new BoundaryPoint(currentVertex.X, currentVertex.Y);
                BoundaryPoint lineEndPoint   = new BoundaryPoint(nextVertex.X, nextVertex.Y);

                if (GeometryHelper.IsLeftSide(lineStartPoint, lineEndPoint, point))
                {
                    return(false);
                }
            }

            return(true);
        }
Ejemplo n.º 7
0
    BoundaryPoint FindBoundaryPoint(float angle)
    {
        var bp = new BoundaryPoint {
            angle = angle, hit = false
        };
        var angleVector = (Vector3)GetVectorAtAngle(angle);
        var hit         = Physics2D.Raycast(transform.position, angleVector, Radius, LayerMask);

        if (hit.collider != null)
        {
            bp.hit     = true;
            bp.point   = hit.point;
            bp.point.z = transform.position.z;
            bp.normal  = hit.normal;
        }
        else
        {
            bp.point  = transform.position + angleVector * Radius;
            bp.normal = angleVector;
        }

        return(bp);
    }
Ejemplo n.º 8
0
 public BoundaryLine(BoundaryPoint p1, BoundaryPoint p2)
 {
     A = p1;
     B = p2;
 }
Ejemplo n.º 9
0
        /// <summary>
        /// 将一个Model转换为一个集合列表
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public static Dictionary <string, string> ConvertOneModelToDictionary(T model, bool oracle = true)
        {
            Dictionary <string, string> dic    = new Dictionary <string, string>();
            Dictionary <string, object> dicobj = ConvertOneModelToDic(model);

            foreach (KeyValuePair <string, object> pair in dicobj)
            {
                object value    = pair.Value;
                string memName  = pair.Key;
                string valuestr = null;
                if (value != null)
                {
                    if (value is DateTime)
                    {
                        DateTime dt = (DateTime)value;
                        if (dt.IsValid())
                        {
                            if (oracle)
                            {
                                valuestr = string.Format(",to_date('{0}','yyyy-mm-dd hh24:mi:ss')", dt.ToFormatDateTimeStr());
                            }
                            else
                            {
                                valuestr = dt.ToFormatDateTimeStr();
                            }
                        }
                    }
                    else if (value is bool)
                    {
                        bool b = (bool)value;
                        valuestr = b ? "1" : "0";
                    }
                    else if (value is BinDateTime)
                    {
                        BinDateTime binDateTime = value as BinDateTime;
                        if (binDateTime != null && binDateTime.DateTime.IsValid())
                        {
                            if (oracle)
                            {
                                valuestr = string.Format(",to_date('{0}','yyyy-mm-dd hh24:mi:ss')",
                                                         binDateTime.DateTime.ToFormatDateTimeStr());
                            }
                            else
                            {
                                valuestr = binDateTime.ToString();
                            }
                        }
                    }
                    else if (value is BoundaryPoint)
                    {
                        BoundaryPoint bp = value as BoundaryPoint;
                        dic.Add(memName.Replace("BP", "X"), bp.X);
                        dic.Add(memName.Replace("BP", "Y"), bp.Y);
                        memName  = memName.Replace("BP", "Z");
                        valuestr = bp.Z;
                    }
                    else
                    {
                        valuestr = value.ToString();
                        if (valuestr == "undefined" || valuestr == "null")
                        {
                            valuestr = null;
                        }
                    }
                }
                dic.Add(memName, valuestr);
            }

            return(dic);
        }
Ejemplo n.º 10
0
        /// <summary>
        /// 从一行数据中为对象赋值
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
        private static T CreateTFromRow(DataRow row)
        {
            if (row == null)
            {
                return(default(T));
            }

            T t = new T();

            // 获得此模型的公共属性

            MemberInfo[] mems = GetMemberInfos(t);
            foreach (MemberInfo pi in mems)
            {
                Type         memType;
                string       memName = pi.Name;
                PropertyInfo p       = pi as PropertyInfo;
                FieldInfo    f       = pi as FieldInfo;
                if (p != null)
                {
                    memType = p.PropertyType;

                    #region 设置属性值
                    // 检查DataTable是否包含此列
                    if (row.Table.Columns.Contains(memName))
                    {
                        // 判断此属性是否有Setter
                        if (!p.CanWrite)
                        {
                            continue;
                        }

                        object value = row[memName];
                        if (value != DBNull.Value)
                        {
                            value = Common.Convert2Type(memType, value);
                            p.SetValue(t, value, null);
                        }
                    }
                    else
                    {
                        if (memName == "FIRSTBP")
                        {
                            BoundaryPoint value = new BoundaryPoint();
                            value.X = row["FIRSTX"].ToString();
                            value.Y = row["FIRSTY"].ToString();
                            value.Z = row["FIRSTZ"].ToString();
                            p.SetValue(t, value, null);
                        }
                        else if (memName == "SECONDBP")
                        {
                            BoundaryPoint value = new BoundaryPoint();
                            value.X = row["SECONDX"].ToString();
                            value.Y = row["SECONDY"].ToString();
                            value.Z = row["SECONDZ"].ToString();
                            p.SetValue(t, value, null);
                        }
                        else if (memName == "THIRDBP")
                        {
                            BoundaryPoint value = new BoundaryPoint();
                            value.X = row["THIRDX"].ToString();
                            value.Y = row["THIRDY"].ToString();
                            value.Z = row["THIRDZ"].ToString();
                            p.SetValue(t, value, null);
                        }
                        else if (memName == "FOURBP")
                        {
                            BoundaryPoint value = new BoundaryPoint();
                            value.X = row["FOURX"].ToString();
                            value.Y = row["FOURY"].ToString();
                            value.Z = row["FOURZ"].ToString();
                            p.SetValue(t, value, null);
                        }
                    }
                    #endregion
                }
                else if (f != null)
                {
                    memType = f.FieldType;

                    #region 设置字段值
                    // 检查DataTable是否包含此列
                    if (row.Table.Columns.Contains(memName))
                    {
                        object value = row[memName];
                        if (value != DBNull.Value)
                        {
                            value = Common.Convert2Type(memType, value);
                            f.SetValue(t, value);
                        }
                    }
                    else
                    {
                        if (memName == "FIRSTBP")
                        {
                            BoundaryPoint value = new BoundaryPoint();
                            value.X = row["FIRSTX"].ToString();
                            value.Y = row["FIRSTY"].ToString();
                            value.Z = row["FIRSTZ"].ToString();
                            f.SetValue(t, value);
                        }
                        else if (memName == "SECONDBP")
                        {
                            BoundaryPoint value = new BoundaryPoint();
                            value.X = row["SECONDX"].ToString();
                            value.Y = row["SECONDY"].ToString();
                            value.Z = row["SECONDZ"].ToString();
                            f.SetValue(t, value);
                        }
                        else if (memName == "THIRDBP")
                        {
                            BoundaryPoint value = new BoundaryPoint();
                            value.X = row["THIRDX"].ToString();
                            value.Y = row["THIRDY"].ToString();
                            value.Z = row["THIRDZ"].ToString();
                            f.SetValue(t, value);
                        }
                        else if (memName == "FOURBP")
                        {
                            BoundaryPoint value = new BoundaryPoint();
                            value.X = row["FOURX"].ToString();
                            value.Y = row["FOURY"].ToString();
                            value.Z = row["FOURZ"].ToString();
                            f.SetValue(t, value);
                        }
                    }
                    #endregion
                }
            }

            return(t);
        }
Ejemplo n.º 11
0
        private static T CreateTFromDictionary(Dictionary <string, string> dicData)
        {
            T t = new T();

            if (dicData == null)
            {
                return(t);
            }
            // 获得此模型的公共属性

            MemberInfo[] mems = GetMemberInfos(t);
            foreach (MemberInfo pi in mems)
            {
                Type         memType;
                string       memName = pi.Name;
                PropertyInfo p       = pi as PropertyInfo;
                FieldInfo    f       = pi as FieldInfo;
                if (p != null)
                {
                    memType = p.PropertyType;

                    #region 设置属性值
                    // 检查DicData是否包含此列
                    if (dicData.ContainsKey(memName))
                    {
                        // 判断此属性是否有Setter
                        if (!p.CanWrite)
                        {
                            continue;
                        }

                        object value = dicData[memName];
                        if (value != DBNull.Value)
                        {
                            value = Common.Convert2Type(memType, value);
                            p.SetValue(t, value, null);
                        }
                    }
                    else
                    {
                        if (memName == "FIRSTBP")
                        {
                            BoundaryPoint value = new BoundaryPoint();
                            value.X = dicData["FIRSTX"].ToString();
                            value.Y = dicData["FIRSTY"].ToString();
                            value.Z = dicData["FIRSTZ"].ToString();
                            p.SetValue(t, value, null);
                        }
                        else if (memName == "SECONDBP")
                        {
                            BoundaryPoint value = new BoundaryPoint();
                            value.X = dicData["SECONDX"].ToString();
                            value.Y = dicData["SECONDY"].ToString();
                            value.Z = dicData["SECONDZ"].ToString();
                            p.SetValue(t, value, null);
                        }
                        else if (memName == "THIRDBP")
                        {
                            BoundaryPoint value = new BoundaryPoint();
                            value.X = dicData["THIRDX"].ToString();
                            value.Y = dicData["THIRDY"].ToString();
                            value.Z = dicData["THIRDZ"].ToString();
                            p.SetValue(t, value, null);
                        }
                        else if (memName == "FOURBP")
                        {
                            BoundaryPoint value = new BoundaryPoint();
                            value.X = dicData["FOURX"].ToString();
                            value.Y = dicData["FOURY"].ToString();
                            value.Z = dicData["FOURZ"].ToString();
                            p.SetValue(t, value, null);
                        }
                    }
                    #endregion
                }
                else if (f != null)
                {
                    memType = f.FieldType;

                    #region 设置字段值
                    // 检查DicData是否包含此列
                    if (dicData.ContainsKey(memName))
                    {
                        object value = dicData[memName];
                        if (value != DBNull.Value)
                        {
                            value = Common.Convert2Type(memType, value);
                            f.SetValue(t, value);
                        }
                    }
                    else
                    {
                        if (memName == "FIRSTBP")
                        {
                            BoundaryPoint value = new BoundaryPoint();
                            value.X = dicData["FIRSTX"].ToString();
                            value.Y = dicData["FIRSTY"].ToString();
                            value.Z = dicData["FIRSTZ"].ToString();
                            f.SetValue(t, value);
                        }
                        else if (memName == "SECONDBP")
                        {
                            BoundaryPoint value = new BoundaryPoint();
                            value.X = dicData["SECONDX"].ToString();
                            value.Y = dicData["SECONDY"].ToString();
                            value.Z = dicData["SECONDZ"].ToString();
                            f.SetValue(t, value);
                        }
                        else if (memName == "THIRDBP")
                        {
                            BoundaryPoint value = new BoundaryPoint();
                            value.X = dicData["THIRDX"].ToString();
                            value.Y = dicData["THIRDY"].ToString();
                            value.Z = dicData["THIRDZ"].ToString();
                            f.SetValue(t, value);
                        }
                        else if (memName == "FOURBP")
                        {
                            BoundaryPoint value = new BoundaryPoint();
                            value.X = dicData["FOURX"].ToString();
                            value.Y = dicData["FOURY"].ToString();
                            value.Z = dicData["FOURZ"].ToString();
                            f.SetValue(t, value);
                        }
                    }
                    #endregion
                }
            }

            return(t);
        }
Ejemplo n.º 12
0
    // MAP DRAWING PROCESS
    // List of BoundaryLines
    // Pick any BoundaryLine and ask it if it still needs to be included
    // BoundaryLine knows if needs inclusion by the number of factions claiming each of its points
    // If neither point claimed, skip it and tell it not to be included
    // Pick faction claimed and draw the whole border
    public void Draw()
    {
        foreach (BoundaryPoint resetPoint in _boundaryPoints.Values)
        {
            resetPoint.ResetDraw();
        }

        bool stillDrawing = true;

        while (stillDrawing) // Run passes through the points till they've been drawn for all factions
        {
            stillDrawing = false;
            foreach (BoundaryPoint drawPoint in _boundaryPoints.Values)
            {
                // Keep going till we find a point where factions still need to be drawn
                if (drawPoint.FactionsToDraw.Count == 0)
                {
                    continue;
                }

                // Find any cross-faction border lines for the point
                List <BoundaryLine> linesWithPoint = _lineReference[drawPoint];
                Faction             currentFaction = Faction.None;
                BoundaryLine        firstNextLine  = null;

                foreach (BoundaryLine l in linesWithPoint)
                {
                    if (firstNextLine != null)
                    {
                        break;
                    }

                    List <Faction> commonFactions = l.CommonFactions();
                    if (commonFactions.Count < 2)
                    {
                        continue;                             // Needs to be a border line
                    }
                    // Make sure that faction that still needs to be drawn is part of the border
                    foreach (Faction f in drawPoint.FactionsToDraw)
                    {
                        if (commonFactions.Contains(f))
                        {
                            currentFaction = f;
                            firstNextLine  = l;
                            break;
                        }
                    }
                }

                stillDrawing = true;
                // We got a live one! Time to start a new blob.
                List <BoundaryPoint> blobPoints = new List <BoundaryPoint>();

                BoundaryPoint currentPoint = drawPoint;
                BoundaryPoint nextPoint    = null;
                if (drawPoint == firstNextLine.A)
                {
                    nextPoint = firstNextLine.B;
                }
                else
                {
                    nextPoint = firstNextLine.A;
                }
                BoundaryLine lastLine = firstNextLine;

                // Add till we get back to original point
                while (!blobPoints.Contains(currentPoint))
                {
                    blobPoints.Add(currentPoint);
                    currentPoint = nextPoint;
                    nextPoint    = null;

                    // Find the next point
                    linesWithPoint = _lineReference[currentPoint];
                    linesWithPoint.Remove(lastLine); // Don't go backward!

                    BoundaryLine nextLine = null;
                    foreach (BoundaryLine l in linesWithPoint)
                    {
                        if (nextLine != null)
                        {
                            break;
                        }

                        List <Faction> commonFactions = l.CommonFactions();
                        if (commonFactions.Count > 1 && commonFactions.Contains(currentFaction))
                        {
                            nextLine = l;
                        }
                    }

                    if (currentPoint == nextLine.A)
                    {
                        nextPoint = nextLine.B;
                    }
                    else
                    {
                        nextPoint = nextLine.A;
                    }
                    lastLine = nextLine;
                }

                foreach (BoundaryPoint p in blobPoints)
                {
                    p.MarkDrawn(currentFaction);
                }

                _blobs.Add(blobPoints);

                for (int i = 0; i < blobPoints.Count; i++)
                {
                    int nextI = i + 1;
                    if (nextI == blobPoints.Count)
                    {
                        nextI = 0;
                    }
                    GameObject go         = new GameObject("Line");
                    GizmoLine  lineToDraw = go.AddComponent <GizmoLine>();
                    lineToDraw.A      = blobPoints[i].Point;
                    lineToDraw.B      = blobPoints[nextI].Point;
                    lineToDraw.drawMe = true;
                }
                Debug.Log("BLOB DONE: " + blobPoints.Count);
                return;
            }
        }
    }
Ejemplo n.º 13
0
    void UpdatePoints()
    {
        if (_hits == null)
        {
            _hits = new List <BoundaryPoint>();
        }
        else
        {
            _hits.Clear();
        }

        var angleIncrement = (360f / Segments);
        var lastPoint      = new BoundaryPoint();

        for (var i = 0; i <= Segments; i++)
        {
            var angle = Angle + i * angleIncrement;
            var bp    = FindBoundaryPoint(angle);
            bp.debug = Color.red;

            if (i > 0)
            {
                if ((bp.hit || lastPoint.hit) && (bp.normal != lastPoint.normal || Vector2.Distance(bp.point, lastPoint.point) > EdgeDistanceThreshold))
                {
                    var prevAngle = angle - angleIncrement;
                    for (var k = 1; k < EdgeResolveIterations; k++)
                    {
                        var nbp = FindBoundaryPoint(prevAngle + k * (angleIncrement / EdgeResolveIterations));
                        nbp.debug = Color.cyan;
                        _hits.Add(nbp);
                    }
                }
            }

            _hits.Add(bp);
            lastPoint = bp;
        }


        var hits        = _hits.OrderBy(x => x.angle).ToArray();
        var vertexCount = _hits.Count + 1;
        var vertices    = new Vector3[vertexCount];
        var triangles   = new int[(vertexCount - 2) * 3];

        vertices[0] = Vector2.zero;
        for (var i = 0; i < vertexCount - 1; i++)
        {
            vertices[i + 1] = transform.InverseTransformPoint(hits[i].point);

            if (i < vertexCount - 2)
            {
                triangles[i * 3]     = 0;
                triangles[i * 3 + 1] = i + 1;
                triangles[i * 3 + 2] = i + 2;
            }
        }

        _mesh.Clear();
        _mesh.vertices  = vertices;
        _mesh.triangles = triangles;
        _mesh.RecalculateNormals();
    }