Exemplo n.º 1
0
    // Update is called once per frame
    void Update()
    {
        #region Identical to EX_6_2
        // Computes V1 and V2
        Vector3 v1 = P1.transform.localPosition - P0.transform.localPosition;
        Vector3 v2 = P2.transform.localPosition - P0.transform.localPosition;
        if ((v1.magnitude < float.Epsilon) || (v2.magnitude < float.Epsilon))
        {
            return;
        }

        // Plane equation parameters
        Vector3 vn = Vector3.Cross(v1, v2);
        vn.Normalize();  // keep this vector normalized
        float D = Vector3.Dot(vn, P0.transform.localPosition);

        // Showing the plane equation is consistent
        Pn.transform.localPosition = D * vn;
        Ds.transform.localScale    = new Vector3(D * 2f, D * 2f, D * 2f); // sphere expects diameter

        // Set up for displaying Pt and Pon
        Pt.SetActive(ShowPointOnPlane);
        Pon.SetActive(ShowPointOnPlane);
        float t = 0;
        bool  almostParallel = false;
        if (ShowPointOnPlane)
        {
            float d = Vector3.Dot(vn, Pt.transform.localPosition);  // distance
            almostParallel = (Mathf.Abs(d) < float.Epsilon);
            Pon.SetActive(!almostParallel);
            if (!almostParallel)
            {
                t = D / d;
                Pon.transform.localPosition = t * Pt.transform.localPosition;
            }
        }
        #endregion

        float   l1  = v1.magnitude;
        float   l2  = v2.magnitude;
        Vector3 v2p = l2 * Vector3.Cross(vn, v1).normalized;
        P2p.transform.localPosition = P0.transform.localPosition + v2p;

        bool inside = false;
        if (!almostParallel)
        {
            Vector3 von = Pon.transform.localPosition - P0.transform.localPosition;
            float   d1  = Vector3.Dot(von, v1.normalized);
            float   d2  = Vector3.Dot(von, v2p.normalized);

            inside = ((d1 >= 0) && (d1 <= l1)) && ((d2 >= 0) && (d2 <= l2));
            if (inside)
            {
                Debug.Log("Inside: Pon is inside of the region defined by V1 and V2");
            }
            else
            {
                Debug.Log("Outside: Pon is outside of the region defined by V1 and V2");
            }
        }
        #region  For visualizing the vectors

        ShowV1.VectorFromTo(P0.transform.localPosition, P1.transform.localPosition);
        ShowV2.VectorFromTo(P0.transform.localPosition, P2.transform.localPosition);
        ShowV3.VectorFromTo(P0.transform.localPosition, P2p.transform.localPosition);

        ShowVn.VectorAt  = P0.transform.localPosition;
        ShowVn.Direction = vn;
        ShowVn.Magnitude = 2f;

        ShowNormal.VectorAt  = Vector3.zero;
        ShowNormal.Magnitude = Mathf.Abs(D) + 2f;
        ShowNormal.Direction = vn;

        ShowPlane.PlaneNormal = -vn;
        Vector3 at = P0.transform.localPosition + P1.transform.localPosition + P2.transform.localPosition + Pn.transform.localPosition;
        int     c  = 4;

        float scale = 1.0f;
        ShowPtLine.DrawVector = ShowPointOnPlane;
        float da = v1.magnitude * scale;
        float db = v2.magnitude * scale;
        float du = Mathf.Max(da, db);

        if (ShowPointOnPlane && (!almostParallel))
        {
            Pon.GetComponent <Renderer>().material.color = Color.white;
            float don = (Pon.transform.localPosition - P0.transform.localPosition).magnitude * scale;
            at += Pon.transform.localPosition;
            c++;
            du = Mathf.Max(du, don);

            // Now the line
            ShowPtLine.VectorColor = Color.black;
            if (Vector3.Dot(Pon.transform.localPosition, Pt.transform.localPosition) < 0)
            {
                ShowPtLine.VectorColor = Color.red;
                ShowPtLine.VectorFromTo(Pt.transform.localPosition, Pon.transform.localPosition);
            }
            else
            {
                if (Pon.transform.localPosition.magnitude > Pt.transform.localPosition.magnitude)
                {
                    ShowPtLine.VectorFromTo(Vector3.zero, Pon.transform.localPosition);
                }
                else
                {
                    ShowPtLine.VectorFromTo(Vector3.zero, Pt.transform.localPosition);
                }
            }

            if (!inside)
            {
                Pon.GetComponent <Renderer>().material.color = Color.red;
            }
        }

        if (du < 0.5f)
        {
            du = 0.5f;
        }

        ShowPlane.XSize  = ShowPlane.ZSize = du;
        ShowPlane.Center = at / c;
        #endregion
    }
Exemplo n.º 2
0
    // Update is called once per frame
    void Update()
    {
        // Computes V1 and V2
        Vector3 v1 = P1.transform.localPosition - P0.transform.localPosition;
        Vector3 v2 = P2.transform.localPosition - P0.transform.localPosition;

        if ((v1.magnitude < float.Epsilon) || (v2.magnitude < float.Epsilon))
        {
            return;
        }

        // Plane equation parameters
        Vector3 vn = Vector3.Cross(v1, v2);

        vn.Normalize();  // keep this vector normalized
        float D = Vector3.Dot(vn, P0.transform.localPosition);

        // Showing the plane equation is consistent
        Pn.transform.localPosition = D * vn;
        Ds.transform.localScale    = new Vector3(D * 2f, D * 2f, D * 2f); // sphere expects diameter

        // Set up for displaying Pt and Pon
        Pt.SetActive(ShowPointOnPlane);
        Pon.SetActive(ShowPointOnPlane);
        float t = 0;
        bool  almostParallel = false;

        if (ShowPointOnPlane)
        {
            float d = Vector3.Dot(vn, Pt.transform.localPosition);  // distance
            almostParallel = (Mathf.Abs(d) < float.Epsilon);
            Pon.SetActive(!almostParallel);
            if (!almostParallel)
            {
                t = D / d;
                Pon.transform.localPosition = t * Pt.transform.localPosition;
            }
        }

        #region  For visualizing the vectors

        ShowV1.VectorFromTo(P0.transform.localPosition, P1.transform.localPosition);
        ShowV2.VectorFromTo(P0.transform.localPosition, P2.transform.localPosition);

        ShowVn.VectorAt  = P0.transform.localPosition;
        ShowVn.Direction = vn;
        ShowVn.Magnitude = 2f;

        ShowNormal.VectorAt  = Vector3.zero;
        ShowNormal.Magnitude = Mathf.Abs(D) + 2f;
        ShowNormal.Direction = vn;

        ShowPlane.PlaneNormal = -vn;
        Vector3 at = P0.transform.localPosition + P1.transform.localPosition + P2.transform.localPosition + Pn.transform.localPosition;
        int     c  = 4;

        float scale = 1.0f;
        ShowPtLine.DrawVector = ShowPointOnPlane;
        float d1 = v1.magnitude * scale;
        float d2 = v2.magnitude * scale;
        float du = Mathf.Max(d1, d2);

        if (ShowPointOnPlane && (!almostParallel))
        {
            float don = (Pon.transform.localPosition - P0.transform.localPosition).magnitude * scale;
            at += Pon.transform.localPosition;
            c++;
            du = Mathf.Max(du, don);

            // Now the line
            ShowPtLine.VectorColor = Color.black;
            if (Vector3.Dot(Pon.transform.localPosition, Pt.transform.localPosition) < 0)
            {
                ShowPtLine.VectorColor = Color.red;
                ShowPtLine.VectorFromTo(Pt.transform.localPosition, Pon.transform.localPosition);
            }
            else
            {
                if (Pon.transform.localPosition.magnitude > Pt.transform.localPosition.magnitude)
                {
                    ShowPtLine.VectorFromTo(Vector3.zero, Pon.transform.localPosition);
                }
                else
                {
                    ShowPtLine.VectorFromTo(Vector3.zero, Pt.transform.localPosition);
                }
            }
        }

        if (du < 0.5f)
        {
            du = 0.5f;
        }

        ShowPlane.XSize  = ShowPlane.ZSize = du;
        ShowPlane.Center = at / c;
        #endregion
    }