示例#1
0
    public Edge(Vertex v1, Vertex v2)
    {
        Debug.LogFormat("Creating new edge between {0} and {1}", v1.number, v2.number);

        this.v1 = v1;
        this.v2 = v2;

        v1.AddEdge(this);
        v2.AddEdge(this);

        linepiece               = CalculateLinepieceValues();
        lineObject              = new GameObject();
        lineObject.name         = "EdgeDebugObject";
        lineRenderer            = lineObject.AddComponent <LineRenderer>();
        lineRenderer.material   = Paper.Instance.LineMaterial;
        lineRenderer.startWidth = 0.05f;
        lineRenderer.endWidth   = 0.05f;
        UpdateLineRenderer(null);

        debugText               = lineObject.AddComponent <TextMesh>();
        debugText.color         = Color.black;
        debugText.characterSize = 0.2f;

        lineObject.transform.position = (v1.transform.position + v2.transform.position) / 2f;

        Paper.Instance.NewEdge(this, out number);
    }
示例#2
0
    public static Vector2 MirrorPointInLinepiece(Linepiece linepiece, Vector2 point)
    {
        Vector2 closestPointOnLine = ClosestPointOnLinepiece(linepiece, point);
        Vector2 mirroredPoint      = point + 2f * (closestPointOnLine - point);

        return(mirroredPoint);
    }
示例#3
0
    public static Vector2 ClosestPointOnLinepiece(Linepiece linepiece, Vector2 point)
    {
        Vector2 ab        = linepiece.End - linepiece.Start;
        Vector2 ap        = point - linepiece.Start;
        float   abSquared = ab.x * ab.x + ab.y * ab.y;
        float   dotABAP   = ab.x * ap.x + ab.y * ap.y;

        // Make sure that we don't divide by zero here
        // If so, just return the original point
        if (Mathf.Approximately(abSquared, 0f))
        {
            return(linepiece.Start);
        }
        float ratio = dotABAP / abSquared;

        if (ratio < 0f)
        {
            return(linepiece.Start);
        }
        else if (ratio > 1f)
        {
            return(linepiece.End);
        }
        Vector2 closestPoint = linepiece.Start + ab * ratio;

        return(closestPoint);
    }
示例#4
0
 public static bool LinepieceIntersectWithLine(Linepiece l1, Line l2, out Vector2 intersection)
 {
     intersection = Vector2.zero;
     if (LinesIntersect(l1, l2, out intersection))
     {
         return(l1.ContainsPoint(intersection));
     }
     return(false);
 }
示例#5
0
 public static bool LinepiecesIntersect(Linepiece l1, Linepiece l2, out Vector2 intersection)
 {
     intersection = Vector2.zero;
     // There is one (unlikely) edge case: two equidirectional linepieces that overlap
     // LinesIntersect will return a random point on either line as the intersection point
     // This point might not lie on both linepieces
     // This could result in a false evaluation, while the linepieces do overlap
     if (LinesIntersect(l1, l2, out intersection))
     {
         return(l1.ContainsPoint(intersection) && l2.ContainsPoint(intersection));
     }
     return(false);
 }
示例#6
0
 public void UpdateLinepiece()
 {
     linepiece = CalculateLinepieceValues();
 }
示例#7
0
    public static float DistancePointToLinepiece(Linepiece linepiece, Vector2 point)
    {
        Vector2 closestPoint = ClosestPointOnLinepiece(linepiece, point);

        return(Vector2.Distance(closestPoint, point));
    }