TransWikia.com

Аналоги в Unity QLine,QPointF,QPolygonF

Stack Overflow на русском Asked on February 10, 2021

на С++ есть классы:

#include <QLineF>
#include <QPointF>
#include <QPolygonF>
#include <QVector2D>
const QLineF &line

Есть аналоги в Unity?

я пока заменил:

QLineF    - UnityEngine.Vector2[] _line
QPointF   - Vector2 -pt
QPolygonF - List<Vector2> _poly

One Answer

Я создал классы (QLineF,QPolygonF), для решения этой проблемы. Может кто поможет ещё дописать методы или как то улучшить:

public class QLineF : IComparable<QLineF>
{
    public int idLine { get; set; }
    public int CompareTo(QLineF other)
    {
        return idLine.CompareTo(other.idLine);
    }
    public enum LinesRelationship
    {
        Intersect = 0,
        Parallel = 1,
        Superposition = 2,
        Equal = 3
    }
    public UnityEngine.Vector2?[] m_lineVec2 = null;
    public UnityEngine.Vector3?[] m_lineVec3 = null;
    public QLineF(Vector2 pt0)
    {
        m_lineVec2 = new UnityEngine.Vector2?[2];
        m_lineVec2[0] = pt0;
        m_lineVec2[1] = pt0 * pt0;
    }
    public QLineF(Vector2 pt0, Vector2 pt1)
    {
        m_lineVec2 = new UnityEngine.Vector2?[2];
        m_lineVec2[0] = pt0;
        m_lineVec2[1] = pt1;
    }
    public QLineF(Vector3 pt0)
    {
        m_lineVec3 = new UnityEngine.Vector3?[2];
        m_lineVec3[0] = pt0;
        Vector3.Cross(pt0, pt0);
    }
    public QLineF(Vector3 pt0, Vector3 pt1)
    {
        m_lineVec3 = new UnityEngine.Vector3?[2];
        m_lineVec3[0] = pt0;
        m_lineVec3[1] = pt1;
    }
    public bool isVector2()
    {
        return (m_lineVec2 != null) ? true : false;
    }
    public LinesRelationship intersect(Vector3 b0, Vector3 b1, out Vector3 intersectionPoint, bool a0IsEnd = true, bool a1IsEnd = true, bool b0IsEnd = true, bool b1IsEnd = true, bool x = true, bool y = true, bool z = true)
    {
        float uxy, uxz, uyz, u, tx, ty, tz, t;
        LinesRelationship relationships = GetLinesRelationship(
            (isVector2() == true) ? new Vector3(m_lineVec2[0].Value.x, 0, m_lineVec2[0].Value.y) : (Vector3)m_lineVec3[0],
            (isVector2() == true) ? new Vector3(m_lineVec2[1].Value.x, 0, m_lineVec2[1].Value.y) : (Vector3)m_lineVec3[1],
            b0, b1, out intersectionPoint, out uxy, out uxz, out uyz, out u, out tx, out ty, out tz, out t, x, y, z);
        // if (relationships == LinesRelationship.Intersect)
        //     if (!(a0IsEnd ? t >= 0 : true) || !(a1IsEnd ? t <= 1 : true) || !(b0IsEnd ? u >= 0 : true) || !(b1IsEnd ? u <= 1 : true))
        //         relationships.Remove(LinesRelationship.Intersect);
        return relationships;
    }
    public LinesRelationship intersect(QLineF b_line, out Vector3 intersectionPoint, bool a0IsEnd = true, bool a1IsEnd = true, bool b0IsEnd = true, bool b1IsEnd = true, bool x = true, bool y = true, bool z = true)
    {
        float uxy, uxz, uyz, u, tx, ty, tz, t;
        LinesRelationship relationships = GetLinesRelationship(
            (isVector2() == true) ? new Vector3(m_lineVec2[0].Value.x, 0, m_lineVec2[0].Value.y) : (Vector3)m_lineVec3[0],
            (isVector2() == true) ? new Vector3(m_lineVec2[1].Value.x, 0, m_lineVec2[1].Value.y) : (Vector3)m_lineVec3[1],
            (b_line.isVector2() == true) ? new Vector3(b_line.m_lineVec2[0].Value.x, 0, b_line.m_lineVec2[0].Value.y) : (Vector3)b_line.m_lineVec3[0],
            (b_line.isVector2() == true) ? new Vector3(b_line.m_lineVec2[1].Value.x, 0, b_line.m_lineVec2[1].Value.y) : (Vector3)b_line.m_lineVec3[1],
            out intersectionPoint, out uxy, out uxz, out uyz, out u, out tx, out ty, out tz, out t, x, y, z);
        return relationships;
    }
    internal LinesRelationship GetLinesRelationship(Vector3 a0, Vector3 a1, Vector3 b0, Vector3 b1, out Vector3 intersectionPoint, out float uxy, out float uxz, out float uyz, out float u, out float tx, out float ty, out float tz, out float t, bool x = true, bool y = true, bool z = true)
    {
        if (!x)
            a0.x = a1.x = b0.x = b1.x = 0;
        if (!y)
            a0.y = a1.y = b0.y = b1.y = 0;
        if (!z)
            a0.z = a1.z = b0.z = b1.z = 0;

        uxy = (b0.x - a0.x - (b0.y - a0.y) / (a1.y - a0.y) * (a1.x - a0.x)) / ((b1.y - b0.y) / (a1.y - a0.y) * (a1.x - a0.x) - (b1.x - b0.x));
        uxz = (b0.x - a0.x - (b0.z - a0.z) / (a1.z - a0.z) * (a1.x - a0.x)) / ((b1.z - b0.z) / (a1.z - a0.z) * (a1.x - a0.x) - (b1.x - b0.x));
        uyz = (b0.y - a0.y - (b0.z - a0.z) / (a1.z - a0.z) * (a1.y - a0.y)) / ((b1.z - b0.z) / (a1.z - a0.z) * (a1.y - a0.y) - (b1.y - b0.y));

        u = !float.IsNaN(uxy) ? uxy : (!float.IsNaN(uxz) ? uxz : uyz);


        tx = (b0.x + u * (b1.x - b0.x) - a0.x) / (a1.x - a0.x);
        ty = (b0.y + u * (b1.y - b0.y) - a0.y) / (a1.y - a0.y);
        tz = (b0.z + u * (b1.z - b0.z) - a0.z) / (a1.z - a0.z);

        t = !float.IsNaN(tx) ? tx : (!float.IsNaN(ty) ? ty : tz);

        intersectionPoint = (a1 - a0) * t + a0;

        LinesRelationship LinesRel = new LinesRelationship();

        if ((a0 - a1).normalized == (b0 - b1).normalized || (a0 - a1).normalized == (b1 - b0).normalized)
            LinesRel = LinesRelationship.Parallel;
        else if (a0 == b0 && a1 == b1)
            LinesRel = LinesRelationship.Equal;
        else if (a0 == b1 && a1 == b0 || a0 == b0 && a1 == b1)
            LinesRel = LinesRelationship.Superposition;
        else if (!(float.IsNaN(intersectionPoint.x) && a0.x - a1.x != 0 && b0.x - b1.x != 0 || float.IsNaN(intersectionPoint.y) && a0.y - a1.y != 0 && b0.y - b1.y != 0 || float.IsNaN(intersectionPoint.z) && a0.z - a1.z != 0 && b0.z - b1.z != 0))
            LinesRel = LinesRelationship.Intersect;

        return LinesRel;
    }
}

public class QPolygonF : IComparable<QPolygonF>
{
    public int idPolygon { get; set; }
    public int CompareTo(QPolygonF other)
    {
        return idPolygon.CompareTo(other.idPolygon);
    }

    public int Count { get { return (pointVec2 != null) ? pointVec2.Count : pointVec3.Count; } }

    public List<Vector2?> pointVec2 = null;
    public List<Vector3?> pointVec3 = null;
    public QPolygonF()
    {
        pointVec2 = null;
        pointVec3 = null;
    }
    public QPolygonF(List<Vector2?> _ptList)
    {
        pointVec2 = new List<Vector2?>();
        pointVec3 = null;
        pointVec2 = _ptList;
    }
    public QPolygonF(List<Vector3?> _ptList)
    {
        pointVec3 = new List<Vector3?>();
        pointVec2 = null;
        pointVec3 = _ptList;
    }
    public QPolygonF(List<QLineF> _lineList)
    {
        if (_lineList[0].isVector2() == true)
        {
            pointVec2 = new List<Vector2?>();
            pointVec3 = null;
            foreach (var line in _lineList)
            {
                foreach (var point in line.m_lineVec2)
                {
                    pointVec2.Add((Vector2)point);
                }
            }
        }
        else
        {
            pointVec2 = null;
            pointVec3 = new List<Vector3?>();
            foreach (var line in _lineList)
            {
                foreach (var point in line.m_lineVec3)
                {
                    pointVec3.Add((Vector3)point);
                }
            }
        }
    }
    public bool isVector2()
    {
        return (pointVec2 != null) ? true : false;
    }
    public bool isTriangle()
    {
        return (pointVec2 != null) ? ((pointVec2.Count == 3) ? true : false) : ((pointVec3.Count == 3) ? true : false);
    }
    public bool isSquare()
    {
        if (pointVec2 != null && pointVec2.Count == 4)
        {
            if (Vector2.Distance((Vector2)pointVec2[0], (Vector2)pointVec2[1]) == Vector2.Distance((Vector2)pointVec2[1], (Vector2)pointVec2[2])
                && Vector2.Distance((Vector2)pointVec2[2], (Vector2)pointVec2[3]) == Vector2.Distance((Vector2)pointVec2[3], (Vector2)pointVec2[0])
                && Vector2.Distance((Vector2)pointVec2[0], (Vector2)pointVec2[1]) == Vector2.Distance((Vector2)pointVec2[2], (Vector2)pointVec2[3])
                && Vector2.Distance((Vector2)pointVec2[1], (Vector2)pointVec2[2]) == Vector2.Distance((Vector2)pointVec2[0], (Vector2)pointVec2[3]))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        else if (pointVec3 != null && pointVec3.Count == 4)
        {
            if (Vector3.Distance((Vector3)pointVec3[0], (Vector3)pointVec3[1]) == Vector3.Distance((Vector3)pointVec3[1], (Vector3)pointVec3[2])
                && Vector3.Distance((Vector3)pointVec3[2], (Vector3)pointVec3[3]) == Vector3.Distance((Vector3)pointVec3[3], (Vector3)pointVec3[0])
                && Vector3.Distance((Vector3)pointVec3[0], (Vector3)pointVec3[1]) == Vector3.Distance((Vector3)pointVec3[2], (Vector3)pointVec3[3])
                && Vector3.Distance((Vector3)pointVec3[1], (Vector3)pointVec3[2]) == Vector3.Distance((Vector3)pointVec3[0], (Vector3)pointVec3[3]))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        else
        {
            return false;
        }
    }

    public void Add(Vector2 point)
    {
        if (pointVec2 == null) { pointVec2 = new List<Vector2?>(); pointVec3 = null; }
        pointVec2.Add(point);
    }
    public void Add(Vector3 point)
    {
        if (pointVec3 == null) { pointVec3 = new List<Vector3?>(); pointVec2 = null; }
        pointVec3.Add(point);
    }

}

Answered by Ivan Triumphov on February 10, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP