Example #1
0
        public bool Intersects(Line l, ref Point intercept)
        {
            float this_m = slope;
            float l_m = l.slope;

            float this_b = yint;
            float l_b = l.yint;

            // case 1: two vertical lines
            if (a.X == b.X && l.a.X == l.b.X)
                return a.X == l.a.X;

            // case 2: 'l' is vertical.  'this' is not.
            if (l.a.X == l.b.X) {
                intercept.X = l.a.X;
                intercept.Y = (int)atX(l.a.X);
                return true;
            }

            // case 3: 'this' is vertical.  'l' is not.
            if (a.X == b.X) {
                return l.Intersects(this, ref intercept);
            }

            // case 4: the way always works unless you try to divide by zero

            // Given:
            // y           == m1*x+b1
            // y           == m2*x+b2
            //
            // m1*x+b1     == m2*x+b2
            // m1*x - m2*x == b2-b1
            // x*(m1-m2)   == b2-b1

            // Solution:
            // x           == (b2-b1) / (m1-m2)
            // y           == m1*x+b1

            if (l_m == this_m && a.X != l.a.X) {
                // if they're parallel, and not the same line
                // then they don't intersect anywhere at all.
                return false;
            }

            intercept.X = (int)((l_b - this_b) / (this_m - l_m));
            intercept.Y = (int)(this_m * intercept.X + this_b);

            return true;
        }
Example #2
0
        public void DrawLine(Line l, Color c)
        {
            vertices[0] = new VertexPositionColor(new Vector3(l.A.X, l.A.Y, 0), c);
            vertices[1] = new VertexPositionColor(new Vector3(l.B.X, l.B.Y, 0), c);

            effect.VertexColorEnabled = true;
            device.DepthStencilState = DepthStencilState.None;
            foreach (EffectPass pass in effect.CurrentTechnique.Passes) {
                pass.Apply();
                device.DrawUserPrimitives<VertexPositionColor>(PrimitiveType.LineStrip, vertices, 0, 1);
            }
        }
Example #3
0
 public void DrawLine(Line l)
 {
     DrawLine(l, Color.White);
 }
Example #4
0
        // UGLY
        bool Touches(Line l, ref Point intercept)
        {
            // If the bounding boxes that the lines live in don't touch, then we don't need to do any more work.
            if (a.X < l.a.X && a.X < l.b.X && b.X < l.a.X && b.X < l.b.X)
                return false;
            if (a.X > l.a.X && a.X > l.b.X && b.X > l.a.X && b.X > l.b.X)
                return false;
            if (a.Y < l.a.Y && a.Y < l.b.Y && b.Y < l.a.Y && b.Y < l.b.Y)
                return false;
            if (a.Y > l.a.Y && a.Y > l.b.Y && b.Y > l.a.Y && b.Y > l.b.Y)
                return false;

            if (!Intersects(l, ref intercept))
                return false;

            // now we know that the lines, if extended infinitely, touch.  And we know where they touch.
            // All that remains is to verify that this point is on both line segments.

            if ((intercept.X < a.X && intercept.X < b.X) || (intercept.X > a.X && intercept.X > b.X))
                return false;

            if ((intercept.Y < a.Y && intercept.Y < b.Y) || (intercept.Y > a.Y && intercept.Y > b.Y))
                return false;

            if ((intercept.X < l.a.X && intercept.X < l.b.X) || (intercept.X > l.a.X && intercept.X > l.b.X))
                return false;

            if ((intercept.Y < l.a.Y && intercept.Y < l.b.Y) || (intercept.Y > l.a.Y && intercept.Y > l.b.Y))
                return false;

            return true;
        }