コード例 #1
0
    public Vector3 LineIntersectsWindowframeAt(Vector3 begin, Vector3 finish)
    {
        Strand segment = new Strand(
            new Node(begin.x, begin.y),
            new Node(finish.x, finish.y)
            );

        Vector3 direction = (finish - begin).normalized;
        float   m = segment.getLineM(), c = segment.getLineC();

        Vector3 topIntersect, leftIntersect, bottomIntersect, rightIntersect, result = Vector3.zero;

        topIntersect    = new Vector3((float)(8 - c) / m, 8, 0);
        leftIntersect   = new Vector3(-8, (float)(m * -8) + c, 0);
        bottomIntersect = new Vector3((float)(-8 - c) / m, -8, 0);
        rightIntersect  = new Vector3(8, (float)(m * 8) + c, 0);

        Vector3[] intersects = new Vector3[] { topIntersect, leftIntersect, bottomIntersect, rightIntersect };

        float angle;

        foreach (Vector3 pt in intersects)
        {
            Vector3 otherDirection = (pt - finish).normalized;
            angle = Vector3.Angle(otherDirection, direction);

            if (angle == 0 && (pt.x <= 8 && pt.x >= -8) && (pt.y <= 8 && pt.y >= -8))
            {
                result = pt;
            }
        }

        return(result);
    }
コード例 #2
0
    public bool Intersects(Strand other, out Vector3 intersectPt)
    {
        Vector3 pt = Vector3.zero;

        intersectPt = pt;

        float m1, m2, c1, c2, x = 0, y = 0;

        m1 = this.m;
        c1 = this.c;
        m2 = other.getLineM();
        c2 = other.getLineC();

        if (double.IsInfinity(m1))
        {
            //vertical line x = c
            x = c1;
            y = (m2 * x) + c2;
        }
        else if (double.IsInfinity(m2))
        {
            x = c2;
            y = (m1 * x) + c1;
        }
        else if (m1 != m2)
        {
            x = (float)System.Math.Round((c1 - c2) / (m2 - m1), 3);
            y = (float)System.Math.Round(((m2 * c1) - (m1 * c2)) / (m2 - m1), 3);
        }

        if (this.ContainsPoint(x, y) && other.ContainsPoint(x, y))
        {
            // Debug.Log(string.Format("{0} contains ({1},{2})", other.PositionString(), x, y));
            intersectPt = new Vector3(x, y, 0);
            return(true);
        }
        return(false);
    }