Exemple #1
0
 public float GetLengthSq()
 {
     return(Line2D.GetLengthSq(v2.x - v1.x, v2.y - v1.y));
 }
 public float GetNearestOnLine(Vector2D p)
 {
     return(Line2D.GetNearestOnLine(v1, v2, p));
 }
 public Vector2D GetCoordinatesAt(float u)
 {
     return(Line2D.GetCoordinatesAt(v1, v2, u));
 }
 public float GetSideOfLine(Vector2D p)
 {
     return(Line2D.GetSideOfLine(v1, v2, p));
 }
 public float GetDistanceToLineSq(Vector2D p, bool bounded)
 {
     return(Line2D.GetDistanceToLineSq(v1, v2, p, bounded));
 }
 public bool GetIntersection(Line2D ray)
 {
     return(Line2D.GetIntersection(v1, v2, ray.v1.x, ray.v1.y, ray.v2.x, ray.v2.y));
 }
 public bool GetIntersection(Line2D ray, out float u_ray, out float u_line)
 {
     return(Line2D.GetIntersection(v1, v2, ray.v1.x, ray.v1.y, ray.v2.x, ray.v2.y, out u_ray, out u_line));
 }
 public bool GetIntersection(float x3, float y3, float x4, float y4, out float u_ray, out float u_line)
 {
     return(Line2D.GetIntersection(v1, v2, x3, y3, x4, y4, out u_ray, out u_line));
 }
 public bool GetIntersection(float x3, float y3, float x4, float y4)
 {
     return(Line2D.GetIntersection(v1, v2, x3, y3, x4, y4));
 }
Exemple #10
0
        // Cohen-Sutherland algorithm
        public static Line2D ClipToRectangle(Line2D line, RectangleF rect, out bool intersects)
        {
            int flags1 = MapSet.GetCSFieldBits(line.v1, rect);
            int flags2 = MapSet.GetCSFieldBits(line.v2, rect);

            intersects = false;

            Line2D result = line;

            while (true)
            {
                if (flags1 == 0 && flags2 == 0)
                {
                    // Line is fully inside the box
                    intersects = true;
                    return(result);
                }

                if ((flags1 & flags2) != 0)
                {
                    // Both points are in the same outer area
                    intersects = false;
                    return(new Line2D());
                }

                float x, y;
                int   outFlags = flags1 != 0 ? flags1 : flags2;
                if ((outFlags & 0x1) > 0)                 // Top
                {
                    x = result.v1.x + (result.v2.x - result.v1.x) * (rect.Top - result.v1.y) / (result.v2.y - result.v1.y);
                    y = rect.Top;
                }
                else if ((outFlags & 0x2) > 0)                 // Bottom
                {
                    x = result.v1.x + (result.v2.x - result.v1.x) * (rect.Bottom - result.v1.y) / (result.v2.y - result.v1.y);
                    y = rect.Bottom;
                }
                else if ((outFlags & 0x4) > 0)                 // Left
                {
                    y = result.v1.y + (result.v2.y - result.v1.y) * (rect.Left - result.v1.x) / (result.v2.x - result.v1.x);
                    x = rect.Left;
                }
                else if ((outFlags & 0x8) > 0)                 // Right
                {
                    y = result.v1.y + (result.v2.y - result.v1.y) * (rect.Right - result.v1.x) / (result.v2.x - result.v1.x);
                    x = rect.Right;
                }
                else
                {
                    intersects = true;
                    return(result);
                }

                if (outFlags == flags1)
                {
                    result.v1 = new Vector2D(x, y);
                    flags1    = MapSet.GetCSFieldBits(result.v1, rect);
                }
                else
                {
                    result.v2 = new Vector2D(x, y);
                    flags2    = MapSet.GetCSFieldBits(result.v2, rect);
                }
            }
        }
Exemple #11
0
 //mxd. This tests if given lines intersects
 public static bool GetIntersection(Line2D line1, Line2D line2)
 {
     return(GetIntersection(line1.v1, line1.v2, line2.v1.x, line2.v1.y, line2.v2.x, line2.v2.y));
 }
Exemple #12
0
        // Constructor
        public ProjectedFrustum2D(Vector2D pos, float xyangle, float zangle, float near, float far, float fov)
        {
            Vector2D[] forwards  = new Vector2D[4];
            Vector2D[] downwards = new Vector2D[4];
            Vector2D[] corners   = new Vector2D[4];

            // Initialize
            this.pos     = pos;
            this.xyangle = xyangle;
            this.zangle  = zangle;
            this.near    = near;
            this.far     = far;
            this.fov     = fov;

            // Make the corners for a forward frustum
            // The corners are in this order: Left-Far, Right-Far, Left-Near, Right-Near
            float fovhalf        = fov * 0.5f;
            float fovhalfcos     = (float)Math.Cos(fovhalf);
            float farsidelength  = far / fovhalfcos;
            float nearsidelength = near / fovhalfcos;

            forwards[0] = pos + Vector2D.FromAngle(xyangle - fovhalf, farsidelength);
            forwards[1] = pos + Vector2D.FromAngle(xyangle + fovhalf, farsidelength);
            forwards[2] = pos + Vector2D.FromAngle(xyangle - fovhalf, nearsidelength);
            forwards[3] = pos + Vector2D.FromAngle(xyangle + fovhalf, nearsidelength);

            // Make the corners for a downward frustum
            // The corners are in the same order as above
            //float farradius = far * (float)Math.Tan(fovhalf) * Angle2D.SQRT2;
            float farradius = (float)(far * 0.5f * Angle2D.SQRT2);

            downwards[0] = pos + Vector2D.FromAngle(xyangle - Angle2D.PI * 0.25f, farradius);
            downwards[1] = pos + Vector2D.FromAngle(xyangle + Angle2D.PI * 0.25f, farradius);
            downwards[2] = pos + Vector2D.FromAngle(xyangle - Angle2D.PI * 0.75f, farradius);
            downwards[3] = pos + Vector2D.FromAngle(xyangle + Angle2D.PI * 0.75f, farradius);

            // Interpolate between the two to make the final corners depending on the z angle
            float d = Math.Abs((float)Math.Sin(zangle));

            corners[0] = forwards[0] * (1.0f - d) + downwards[0] * d;
            corners[1] = forwards[1] * (1.0f - d) + downwards[1] * d;
            corners[2] = forwards[2] * (1.0f - d) + downwards[2] * d;
            corners[3] = forwards[3] * (1.0f - d) + downwards[3] * d;

            // Make the frustum lines
            // Note that the lines all have their right side inside the frustum!
            lines    = new Line2D[4];
            lines[0] = new Line2D(corners[2], corners[0]);
            lines[1] = new Line2D(corners[1], corners[3]);
            lines[2] = new Line2D(corners[3], corners[2]);
            lines[3] = new Line2D(corners[0], corners[1]);

            // Calculate the circle center
            center = (corners[0] + corners[1] + corners[2] + corners[3]) * 0.25f;

            // Calculate the radius from the center to the farthest corner
            float radius2 = 0.0f;

            for (int i = 0; i < corners.Length; i++)
            {
                float distance2 = (float)Vector2D.DistanceSq(center, corners[i]);
                if (distance2 > radius2)
                {
                    radius2 = distance2;
                }
            }
            radius = (float)Math.Sqrt(radius2);
        }