Ejemplo n.º 1
0
    static public Vector2f point_line_intersect(Vector2f pt, LineSegf line)
    {
        Vector2f linedir = new Vector2f(line.vertex1.x - line.vertex0.x, line.vertex1.y - line.vertex0.y);
        float    length  = linedir.length();

        if (length <= 0)
        {
            return(line.vertex0);
        }

        linedir.x /= length;
        linedir.y /= length;

        float nu = linedir.x * (pt.x - line.vertex0.x) + linedir.y * (pt.y - line.vertex0.y);

        float n = nu / 1;

        if (n < 0)
        {
            return(line.vertex0);
        }
        if (n > length)
        {
            return(line.vertex1);
        }

        return(new Vector2f(line.vertex0.x + linedir.x * n, line.vertex0.y + linedir.y * n));
    }
Ejemplo n.º 2
0
    static public float point_lineseg_side(Vector2f pt, LineSegf line)
    {
        Vector2f vec1 = new Vector2f(line.vertex1.x - line.vertex0.x, line.vertex1.y - line.vertex0.y);
        Vector2f vec2 = new Vector2f(pt.x - line.vertex1.x, pt.y - line.vertex1.y);

        return(vector_ccw_vector(vec1, vec2));
    }
Ejemplo n.º 3
0
    static public bool lineseg_intersect_rectangle(LineSegf lineseg, Rectanglef rect)
    {
        if (point_in_rectangle(lineseg.vertex0, rect))
        {
            return(true);
        }

        if (point_in_rectangle(lineseg.vertex1, rect))
        {
            return(true);
        }

        if (lineseg_intersect_lineseg(lineseg, rect.side(0)))
        {
            return(true);
        }

        if (lineseg_intersect_lineseg(lineseg, rect.side(1)))
        {
            return(true);
        }

        if (lineseg_intersect_lineseg(lineseg, rect.side(2)))
        {
            return(true);
        }

        if (lineseg_intersect_lineseg(lineseg, rect.side(3)))
        {
            return(true);
        }

        return(false);
    }
Ejemplo n.º 4
0
    static public bool overlap(SceneShapeLine shape1, SceneShapeRound shape2)
    {
        LineSegf line = new LineSegf(new Vector2f(shape1.mVertex1.x, shape1.mVertex1.y), new Vector2f(shape1.mVertex2.x, shape1.mVertex2.y));

        Vector2f tarPos = Geometryalgorithm2d.point_line_intersect(new Vector2f(shape2.mCenter.x, shape2.mCenter.y), line);

        return((tarPos.Subtract(new Vector2f(shape2.mCenter.x, shape2.mCenter.y))).length() <= shape2.mRadius);
    }
Ejemplo n.º 5
0
    static public bool point_in_triangle(Vector2f pt, Trianglef triangle)
    {
        float minX = triangle.v0.x;
        float minY = triangle.v0.y;
        float maxX = triangle.v0.x;
        float maxY = triangle.v0.y;

        minX = minX < triangle.v1.x ? minX : triangle.v1.x;
        minX = minX < triangle.v2.x ? minX : triangle.v2.x;

        minY = minY < triangle.v1.y ? minY : triangle.v1.y;
        minY = minY < triangle.v2.y ? minY : triangle.v2.y;

        maxX = maxX > triangle.v1.x ? maxX : triangle.v1.x;
        maxX = maxX > triangle.v2.x ? maxX : triangle.v2.x;

        maxY = maxY > triangle.v1.y ? maxY : triangle.v1.y;
        maxY = maxY > triangle.v2.y ? maxY : triangle.v2.y;

        if (pt.x < minX || pt.x > maxX || pt.y < minY || pt.y > maxY)
        {
            return(false);
        }

        int count = 0;

        for (uint i = 0; i < 3; i++)
        {
            LineSegf line = triangle.side(i);

            if (point_in_lineseg(pt, line))
            {
                return(true);
            }

            if (point_lineseg_side(pt, line) > 0)
            {
                if (count != 0 && count != 1)
                {
                    return(false);
                }

                count = 1;
            }
            else
            {
                if (count != 0 || count != -1)
                {
                    return(false);
                }

                count = -1;
            }
        }

        return(true);
    }
Ejemplo n.º 6
0
    static public Rectanglef lineseg_box(LineSegf l1)
    {
        float minx = l1.vertex0.x < l1.vertex1.x ? l1.vertex0.x : l1.vertex1.x;
        float maxx = l1.vertex0.x > l1.vertex1.x ? l1.vertex0.x : l1.vertex1.x;
        float miny = l1.vertex0.y < l1.vertex1.y ? l1.vertex0.y : l1.vertex1.y;
        float maxy = l1.vertex0.y > l1.vertex1.y ? l1.vertex0.y : l1.vertex1.y;

        return(new Rectanglef(new Vector2f(minx, miny), new Vector2f(maxx, maxy)));
    }
Ejemplo n.º 7
0
    static public bool point_in_lineseg(Vector2f pt, LineSegf line)
    {
        float f1 = (new Vector2f(line.vertex0.x - pt.x, line.vertex0.y - pt.y)).length();
        float f2 = (new Vector2f(line.vertex1.x - pt.x, line.vertex1.y - pt.y)).length();

        float v = Math.Abs(f1 + f2 - line.length());

        return(v < 1E-5f);
    }
Ejemplo n.º 8
0
    static public bool overlap(SceneShapeLine shape1, SceneShapePolygon shape2)
    {
        LineSegf line = new LineSegf(new Vector2f(shape1.mVertex1.x, shape1.mVertex1.y), new Vector2f(shape1.mVertex2.x, shape1.mVertex2.y));

        for (int i = 1; i < shape2.mPts.Count; i++)
        {
            Vector2f p1 = new Vector2f(shape2.mPts[i - 1].x, shape2.mPts[i - 1].y);
            Vector2f p2 = new Vector2f(shape2.mPts[i].x, shape2.mPts[i].y);

            if (Geometryalgorithm2d.lineseg_intersect_lineseg(line, new LineSegf(p1, p2)))
            {
                return(true);
            }
        }

        return(false);
    }
Ejemplo n.º 9
0
    static public bool circle_intersect_triangle(Vector2f center, float radius, Trianglef triangle)
    {
        if (Geometryalgorithm2d.point_in_triangle(center, triangle))
        {
            return(true);
        }

        for (uint i = 0; i < 3; i++)
        {
            LineSegf line = triangle.side(i);

            Vector2f pt = Geometryalgorithm2d.point_line_intersect(center, line);

            float distance = pt.Subtract(center).length();
            if (distance < radius)
            {
                return(true);
            }
        }

        return(false);
    }
Ejemplo n.º 10
0
    static public bool lineseg_intersect_triangle(LineSegf lineseg, Trianglef triangle)
    {
        if (point_in_triangle(lineseg.vertex0, triangle))
        {
            return(true);
        }

        if (point_in_triangle(lineseg.vertex1, triangle))
        {
            return(true);
        }

        for (uint j = 0; j < 3; j++)
        {
            if (lineseg_intersect_lineseg(lineseg, triangle.side(j)))
            {
                return(true);
            }
        }

        return(false);
    }
Ejemplo n.º 11
0
    static public bool lineseg_intersect_lineseg(LineSegf l1, LineSegf l2)
    {
        Rectanglef r1 = lineseg_box(l1);
        Rectanglef r2 = lineseg_box(l2);

        if (r1.minv.x > r2.maxv.x || r1.maxv.x < r2.minv.x ||
            r1.minv.y > r2.maxv.y || r1.maxv.y < r2.minv.y)
        {
            return(false);
        }

        float d1 = point_lineseg_side(l1.vertex0, l2);
        float d2 = point_lineseg_side(l1.vertex1, l2);
        float d3 = point_lineseg_side(l2.vertex0, l1);
        float d4 = point_lineseg_side(l2.vertex1, l1);

        if (d1 * d2 < 0 && d3 * d4 < 0)
        {
            return(true);
        }
        else if (d1 == 0 && point_in_lineseg(l1.vertex0, l2))
        {
            return(true);
        }
        else if (d2 == 0 && point_in_lineseg(l1.vertex1, l2))
        {
            return(true);
        }
        else if (d3 == 0 && point_in_lineseg(l2.vertex0, l1))
        {
            return(true);
        }
        else if (d4 == 0 && point_in_lineseg(l2.vertex1, l1))
        {
            return(true);
        }

        return(false);
    }
Ejemplo n.º 12
0
    static public bool IsPointInPolygon(List <Vector2> polygon, Vector2 pt, bool side = true)
    {
        int wn = 0;

        for (int i = 0; i < polygon.Count; i++)
        {
            Vector2 pt1 = polygon[i];
            Vector2 pt2 = polygon[(i + 1) % (int)polygon.Count];

            LineSegf line = new LineSegf(new Vector2f(pt1.x, pt1.y), new Vector2f(pt2.x, pt2.y));

            if (point_in_lineseg(new Vector2f(pt.x, pt.y), line))
            {
                return(side);
            }

            if (point_lineseg_side(new Vector2f(pt.x, pt.y), line) > 0)
            {
                if (wn != 0 && wn != 1)
                {
                    return(false);
                }

                wn = 1;
            }
            else
            {
                if (wn != 0 && wn != -1)
                {
                    return(false);
                }

                wn = -1;
            }
        }

        return(true);
    }
Ejemplo n.º 13
0
    private UpdateRetCode UpdateMovePos(uint elapsed)
    {
        if (mPathNodes == null)
        {
            return(UpdateRetCode.Aborted);
        }

        float movDist = elapsed * mOwner.GetSpeed() / 1000f;

        if (movDist < 0.001f)
        {
            return(UpdateRetCode.Continue);
        }

        Vector3f toPos  = mOwner.GetPosition3f();
        Vector3f curPos = mOwner.GetPosition3f();

        while (movDist > 0 && mCurrentIndex < mPathNodes.Count)
        {
            Vector3f nodePos = mPathNodes[mCurrentIndex];
            Vector3f len3d   = nodePos.Subtract(curPos);
            len3d.y = 0;

            float distToNode = len3d.Length();
            if (movDist >= distToNode)
            {
                movDist -= distToNode;
                toPos    = nodePos;

                ++mCurrentIndex;

                if (mCurrentIndex < mPathNodes.Count)
                {
                    Vector3f nodePosNext = mPathNodes[mCurrentIndex];
                    Vector3f posOwner    = mOwner.GetPosition3f();
                    if (Math.Abs(posOwner.x - nodePosNext.x) > 0.01f ||
                        Math.Abs(posOwner.z - nodePosNext.z) > 0.01f)
                    {
                        float dir = Utility.Angle2D(new UnityEngine.Vector3(nodePosNext.x, nodePosNext.y, nodePosNext.z), new UnityEngine.Vector3(posOwner.x, posOwner.y, posOwner.z)) * UnityEngine.Mathf.Deg2Rad;
                        mOwner.SetDirection(dir);
                        mOwner.SetMoveDirection(dir);
                    }
                }
                else
                {
                    movDist = 0;
                }
            }
            else
            {
                if (mCurrentIndex > 0)
                {
                    Vector3f dir = mPathNodes[mCurrentIndex].Subtract(mPathNodes[mCurrentIndex - 1]);
                    dir.y = 0;

                    dir.Normalize();
                    dir.MultiplyBy(movDist);
                    toPos = curPos.Add(dir);

                    LineSegf line = new LineSegf(new Vector2f(mPathNodes[mCurrentIndex - 1].x, mPathNodes[mCurrentIndex - 1].z),
                                                 new Vector2f(mPathNodes[mCurrentIndex].x, mPathNodes[mCurrentIndex].z));
                    Vector2f tarPos = Geometryalgorithm2d.point_line_intersect(new Vector2f(toPos.x, toPos.z), line);
                    toPos.x = tarPos.x;
                    toPos.z = tarPos.y;
                    movDist = 0;
                }
                else
                {
                    len3d.y = 0;
                    len3d.Normalize();
                    toPos   = curPos.Add(len3d.MultiplyBy(movDist));
                    movDist = 0;
                }
            }
        }

        if (!mOwner.Scene.IsInWalkableRegion(toPos.x, toPos.z))
        {
            return(UpdateRetCode.Finished);
        }

        //if (mOwner.Scene.IsBarrierRegion(mOwner, toPos.x, toPos.z))
        //{
        //    return UpdateRetCode.Finished;
        //}

        toPos.y = mOwner.Scene.GetHeight(toPos.x, toPos.z);

        mOwner.SetPosition3f(toPos);

        return(mCurrentIndex >= mPathNodes.Count ? UpdateRetCode.Finished : UpdateRetCode.Continue);
    }
Ejemplo n.º 14
0
 static public bool lineseg_in_rectangle(LineSegf lineseg, Rectanglef rect)
 {
     return(point_in_rectangle(lineseg.vertex0, rect) && point_in_rectangle(lineseg.vertex1, rect));
 }
Ejemplo n.º 15
0
    static public bool lineseg_intersect_rectangle_by_triangle(Vector2f p1_l1, Vector2f p2_l1,
                                                               Vector2f p1_r2, Vector2f p2_r2,
                                                               Vector2f p3_r2, Vector2f p4_r2)
    {
        Trianglef triangle = new Trianglef();

        triangle.v0 = p1_r2;
        triangle.v1 = p2_r2;
        triangle.v2 = p3_r2;

        if (point_in_triangle(p1_l1, triangle))
        {
            return(true);
        }

        triangle.v0 = p1_r2;
        triangle.v1 = p4_r2;
        triangle.v2 = p3_r2;

        if (point_in_triangle(p1_l1, triangle))
        {
            return(true);
        }

        triangle.v0 = p1_r2;
        triangle.v1 = p2_r2;
        triangle.v2 = p3_r2;

        if (point_in_triangle(p2_l1, triangle))
        {
            return(true);
        }

        triangle.v0 = p1_r2;
        triangle.v1 = p4_r2;
        triangle.v2 = p3_r2;

        if (point_in_triangle(p2_l1, triangle))
        {
            return(true);
        }

        LineSegf l1 = new LineSegf();
        LineSegf l2 = new LineSegf();

        l1.vertex0 = p1_l1;
        l1.vertex1 = p2_l1;

        l2.vertex0 = p1_r2;
        l2.vertex1 = p2_r2;

        if (lineseg_intersect_lineseg(l1, l2))
        {
            return(true);
        }

        l2.vertex0 = p2_r2;
        l2.vertex1 = p3_r2;

        if (lineseg_intersect_lineseg(l1, l2))
        {
            return(true);
        }

        l2.vertex0 = p3_r2;
        l2.vertex1 = p4_r2;

        if (lineseg_intersect_lineseg(l1, l2))
        {
            return(true);
        }

        l2.vertex0 = p4_r2;
        l2.vertex1 = p1_r2;

        if (lineseg_intersect_lineseg(l1, l2))
        {
            return(true);
        }

        return(false);
    }
Ejemplo n.º 16
0
 public bool Equal(LineSegf rhs)
 {
     return((this.vertex0.Equal(rhs.vertex0) && this.vertex1.Equal(rhs.vertex1)) || (this.vertex0.Equal(rhs.vertex1) && this.vertex1.Equal(rhs.vertex0)));
 }