Subversion Repositories AndroidProjects

Rev

Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.gebauz.bauzoid.math;

/** Line segment class. */
public class Line2
{
        // Constants========================================================================================

        // Embedded Types===================================================================================

        // Fields===========================================================================================
       
        private float ax = 0;
        private float ay = 0;
       
        private float bx = 0;
        private float by = 0;
       
        // parametric form
        private float A = 0.0f;
        private float B = 0.0f;
        private float C = 0.0f;

        // Methods==========================================================================================
       
        public Line2()
        {
                calcParametric();
        }
       
        public Line2(float _ax, float _ay, float _bx, float _by)
        {
                ax = _ax; ay = _ay;
                bx = _bx; by = _by;
                calcParametric();
        }
       
        public Line2(Vector2 a, Vector2 b)
        {
                ax = a.x; ay = a.y;
                bx = b.x; by = b.y;
                calcParametric();
        }
       
        public void calcParametric()
        {
                A = getA();
                B = getB();
                C = getC();
        }
       
        public float getLengthSqr()
        {
                float diffX = ax - bx;
                float diffY = ay - by;
                return (diffX*diffX + diffY*diffY);
        }
       
        public float getLength()
        {
                float length = getLengthSqr();
                return (float)(Math.sqrt(length));
        }
       
        /** Check if x, y (which must be collinear to the line segment) lies within the line segment. */
        public boolean isCollinearPointOnSegment(float x, float y)
        {
                if ((x <= Math.max(ax, bx)) && (x >= Math.min(ax, bx)) &&
                        (y <= Math.max(ay, by)) && (y >= Math.min(ay, by)))
                {
                        return true;
                }
               
                return false;
        }
       
        /** Get the A coefficient in the Ax + By = C form. */
        public final float getA()
        {
                return by - ay;
        }
       
        /** Get the B coefficient in the Ax + By = C form. */
        public final float getB()
        {
                return ax - bx;
        }
       
        /** Get the C coefficient in the Ax + By = C form. */
        public final float getC()
        {
                return getA() * ax + getB() * ay;
        }
       
        public Vector2 getLineIntersection(Line2 other)
        {
                // get Ax + By = C form coefficients           
                float A1 = A;
                float B1 = B;
                float C1 = C;
               
                float A2 = other.A;
                float B2 = other.B;
                float C2 = other.C;
               
                float delta = A1*B2 - A2*B1;
                if (delta == 0)
                        return null;
               
                float x = (B2*C1 - B1*C2)/delta;
                float y = (A1*C2 - A2*C1)/delta;
               
                return new Vector2(x, y);
        }
       
        public Vector2 getSegmentIntersection(Line2 other)
        {
                Vector2 p = getLineIntersection(other);
               
                if (p == null)
                        return null;
               
                if (this.isCollinearPointOnSegment(p.x, p.y) && other.isCollinearPointOnSegment(p.x, p.y))
                        return p;
               
                return null;
        }
       
        public boolean intersectsSegment(Line2 other)
        {
                Vector2 p = getSegmentIntersection(other);
               
                if (p == null)
                        return false;
               
                return true;
        }

        // Getters/Setters==================================================================================

}