Exemplo n.º 1
0
        /// <summary>
        /// calc and return info on how two lines horizontally intersect each other.
        /// </summary>
        /// <param name="Line1"></param>
        /// <param name="Line2"></param>
        /// <returns></returns>
        public static HorizontalIntersect HorizontalIntersect(
            LineCoordinates Line1, LineCoordinates Line2)
        {
            HorizontalIntersect hi = null;
            double iw;
            double line1Ofs;
            double line2Ofs;

            if ((Line1.LeftMost <= Line2.LeftMost) && (Line1.RightMost >= Line2.LeftMost))
            {
                line1Ofs = Line2.LeftMost - Line1.LeftMost;
                line2Ofs = 0;
                if (Line1.RightMost < Line2.RightMost)
                {
                    iw = Line1.RightMost - Line2.LeftMost + 1.00;
                }
                else
                {
                    iw = Line2.Width;
                }
            }
            else if ((Line2.LeftMost <= Line1.LeftMost) && (Line2.RightMost >= Line1.LeftMost))
            {
                line2Ofs = Line1.LeftMost - Line2.LeftMost;
                line1Ofs = 0;
                if (Line2.RightMost < Line1.RightMost)
                {
                    iw = Line2.RightMost - Line1.LeftMost + 1.00;
                }
                else
                {
                    iw = Line1.Width;
                }
            }
            else
            {
                iw       = 0;
                line1Ofs = 0;
                line2Ofs = 0;
            }

            hi = new HorizontalIntersect()
            {
                Length   = iw,
                Line1    = Line1,
                Line1Ofs = line1Ofs,
                Line2    = Line2,
                Line2Ofs = line2Ofs
            };

            return(hi);
        }
Exemplo n.º 2
0
        /// <summary>
        /// compute the angle between two lines.
        /// </summary>
        /// <param name="Line1"></param>
        /// <param name="Line2"></param>
        /// <returns></returns>
        public static double AngleBetween(LineCoordinates Line1, LineCoordinates Line2)
        {
            // the start point of each line is the common point between the two lines.
            var commonPoint = LineCoordinates.CommonEndPoint(Line1, Line2);

            // the end points of each line.
            var end1 = Line1.OtherPoint(commonPoint);
            var end2 = Line2.OtherPoint(commonPoint);

            // the 360 degree angles of each line.
            var angle1 = LineExt.GetAngle(commonPoint, end1);
            var angle2 = LineExt.GetAngle(commonPoint, end2);

            // the angle between is the diff between the 360 degree angles of the 2 lines.
            var angleBet = Angle360.Subtract(angle1, angle2);

            if (angleBet.Value > 180)
            {
                angleBet = Angle360.Subtract(angle2, angle1);
            }

            return(angleBet.Value);
        }
Exemplo n.º 3
0
        /// <summary>
        /// return the start/end points the two lines have in common. The two lines form a
        /// vertex. The common end point is the point location of that vertex.
        /// </summary>
        /// <param name="Line1"></param>
        /// <param name="Line2"></param>
        /// <returns></returns>
        public static Point CommonEndPoint(LineCoordinates Line1, LineCoordinates Line2)
        {
            if (Line1.Start.Equals(Line2.Start))
            {
                return(Line1.Start);
            }
            else if (Line1.End.Equals(Line2.Start))
            {
                return(Line1.End);
            }
            else if (Line1.Start.Equals(Line2.End))
            {
                return(Line1.Start);
            }
            else if (Line1.End.Equals(Line2.End))
            {
                return(Line1.End);
            }

            else
            {
                throw new ApplicationException("lines do not have end points in common");
            }
        }
Exemplo n.º 4
0
 public static LineVertexJunction TryMatchLines(
     LineCoordinates Line1, LineCoordinates Line2)
 {
     return(null);
 }
Exemplo n.º 5
0
        /// <summary>
        /// Calc the point at which this line intersects with another line.
        /// </summary>
        /// <param name="Other"></param>
        /// <returns></returns>
        public static Point?IntersectPos(LineCoordinates Line1, LineCoordinates Line2)
        {
            // the x-axis range of this line does not intersect at all with the x-axis range
            // of the other line.
            if ((Line1.Start.X > Line2.End.X) || (Line1.End.X < Line2.Start.X))
            {
                return(null);
            }

            // the y-axis range of this line does not intersect at all with the y-axis range
            // of the other line.
            else if ((Line1.TopMost > Line2.BottomMost) || (Line1.BottomMost < Line2.TopMost))
            {
                return(null);
            }

            // lines are perpendicular. They intersect at the x-axis of the vertical line and
            // the y-axis of the horizontal line.
            else if (Line1.IsVertical && Line2.IsHorizontal)
            {
                return(new Point(Line1.Start.X, Line2.Start.Y));
            }
            else if (Line1.IsHorizontal && Line2.IsVertical)
            {
                return(new Point(Line1.Start.X, Line2.Start.Y));
            }

            // start pos of this line matches the start or end pos of the other line.
            else if ((Line1.Start.Equals(Line2.Start)) || (Line1.Start.Equals(Line2.End)))
            {
                return(Line1.Start);
            }

            // end pos of this line matches the start or end pos of the other line.
            else if ((Line1.End.Equals(Line2.Start)) || (Line1.End.Equals(Line2.End)))
            {
                return(Line2.End);
            }

            else
            {
                double i_x = 0.00;
                double i_y = 0.00;
//        char collisionDetected;

                double s1_x, s1_y, s2_x, s2_y;
                s1_x = Line1.End.X - Line1.Start.X;
                s1_y = Line1.End.Y - Line1.Start.Y;
                s2_x = Line2.End.X - Line2.Start.X;
                s2_y = Line2.End.Y - Line2.Start.Y;

                double s, t;
                s = (-s1_y * (Line1.Start.X - Line2.Start.X) + s1_x * (Line1.Start.Y - Line2.Start.Y))
                    / (-s2_x * s1_y + s1_x * s2_y);
                t = (s2_x * (Line1.Start.Y - Line2.Start.Y) - s2_y * (Line1.Start.X - Line2.Start.X))
                    / (-s2_x * s1_y + s1_x * s2_y);

                if (s >= 0 && s <= 1 && t >= 0 && t <= 1)
                {
                    // Collision detected
                    i_x = Line1.Start.X + (t * s1_x);
                    i_y = Line1.Start.Y + (t * s1_y);
                    return(new Point(i_x, i_y));
                }
                else
                {
                    return(null);
                }
            }

#if skip
            var hi = LineCoordinates.HorizontalIntersect(this, Other);

            double         leftMostX = hi.Line1.Start.X + hi.Line1Ofs;
            TriangleVertex vertex1;
            TriangleVertex vertex2;

            // calc the location and angle of the vertex of the intersect triangle formed
            // by line1.
            {
                Point? pos;
                double angle;
                if (hi.Line1Ofs > 0)
                {
                    var adjLine    = new HorizontalLineCoordinates(hi.Line1.Start, hi.Line1Ofs);
                    var oppLine    = this.CalcOppositeSideLine(hi.Line1Ofs);
                    var rtTriangle = new RightTriangle()
                    {
                        AdjSide = adjLine,
                        OppSide = oppLine
                    };
                    pos = rtTriangle.OppVertex.Location;
                }
                else
                {
                    pos = hi.Line1.Start;
                }
                angle   = IntersectPos_CalcVertexAngle(hi.Line1.Angle);
                vertex1 = new TriangleVertex()
                {
                    Angle    = angle,
                    Location = pos.Value
                };
            }

            // calc the left side vertex of the intersect triangle formed by line2.
            {
                Point? pos;
                double angle;
                if (hi.Line2Ofs > 0)
                {
                    var adjLine    = new HorizontalLineCoordinates(hi.Line2.Start, hi.Line2Ofs);
                    var oppLine    = this.CalcOppositeSideLine(hi.Line2Ofs);
                    var rtTriangle = new RightTriangle()
                    {
                        AdjSide = adjLine,
                        OppSide = oppLine
                    };
                    pos = rtTriangle.OppVertex.Location;
                }
                else
                {
                    pos = hi.Line2.Start;
                }
                angle   = IntersectPos_CalcVertexAngle(hi.Line2.Angle);
                vertex2 = new TriangleVertex()
                {
                    Angle    = angle,
                    Location = pos.Value
                };
            }

            // build the oblique triangle that is formed by the intersection of the
            // two lines.
            var obTriangle = new ObliqueTriangle()
            {
                Vertex1 = vertex1,
                Vertex2 = vertex2
            };


            return(null);
        }