/********************************************************** * To check the Pt is in the Triangle or not. * If the Pt is in the line or is a vertex, then return true. * If the Pt is out of the Triangle, then return false. * * This method is used for triangle only. ***********************************************************/ private bool TriangleContainsPoint(Coordinate[] trianglePts, Coordinate pt) { if (trianglePts.Length != 3) { return(false); } for (int i = trianglePts.GetLowerBound(0); i < trianglePts.GetUpperBound(0); i++) { if (pt.Equals(trianglePts[i])) { return(true); } } bool bIn = false; LineSegmentEquation line0 = new LineSegmentEquation( trianglePts[0], trianglePts[1]); LineSegmentEquation line1 = new LineSegmentEquation( trianglePts[1], trianglePts[2]); LineSegmentEquation line2 = new LineSegmentEquation( trianglePts[2], trianglePts[0]); if (line0.OnLine(pt) || line1.OnLine(pt) || line2.OnLine(pt)) { bIn = true; } else //point is not in the lines { double dblArea0 = PolygonHelper.PolygonArea( new Coordinate[] { trianglePts[0], trianglePts[1], pt }); double dblArea1 = PolygonHelper.PolygonArea( new Coordinate[] { trianglePts[1], trianglePts[2], pt }); double dblArea2 = PolygonHelper.PolygonArea( new Coordinate[] { trianglePts[2], trianglePts[0], pt }); if (dblArea0 > 0) { if ((dblArea1 > 0) && (dblArea2 > 0)) { bIn = true; } } else if (dblArea0 < 0) { if ((dblArea1 < 0) && (dblArea2 < 0)) { bIn = true; } } } return(bIn); }
/***Check whether this line is in a longer line***/ public bool InLine(LineSegmentEquation longerLineSegment) { if (longerLineSegment == null) { throw new ArgumentNullException("longerLineSegment"); } bool bInLine = false; if ((longerLineSegment.OnLine(m_startPoint)) && (longerLineSegment.OnLine(m_endPoint))) { bInLine = true; } return(bInLine); }
/******************************************************** * To check whether 2 lines segments have an intersection *********************************************************/ public bool IntersectedWith(LineSegmentEquation line) { if (line == null) { throw new ArgumentNullException("line"); } double x1 = this.m_startPoint.X; double y1 = this.m_startPoint.Y; double x2 = this.m_endPoint.X; double y2 = this.m_endPoint.Y; double x3 = line.m_startPoint.X; double y3 = line.m_startPoint.Y; double x4 = line.m_endPoint.X; double y4 = line.m_endPoint.Y; double de = (y4 - y3) * (x2 - x1) - (x4 - x3) * (y2 - y1); //if de<>0 then //lines are not parallel if (Math.Abs(de - 0) < ConstantValue.SmallValue) //not parallel { double ua = ((x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3)) / de; double ub = ((x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3)) / de; if ((ua > 0) && (ub < 1)) { return(true); } else { return(false); } } else //lines are parallel { return(false); } }
/************************************************ * Offset the line segment to generate a new line segment * If the offset direction is along the x-axis or y-axis, * Parameter is true, other wise it is false * ***********************************************/ public LineSegmentEquation OffsetLine(double distance, bool rightOrDown) { //offset a line with a given distance, generate a new line //rightOrDown=true means offset to x incress direction, // if the line is horizontal, offset to y incress direction LineSegmentEquation line; Coordinate newStartPoint = new Coordinate(); Coordinate newEndPoint = new Coordinate(); double alphaInRad = this.LineAngle; // 0-PI if (rightOrDown) { if (this.IsHorizontalLine) //offset to y+ direction { newStartPoint.X = this.m_startPoint.X; newStartPoint.Y = this.m_startPoint.Y + distance; newEndPoint.X = this.m_endPoint.X; newEndPoint.Y = this.m_endPoint.Y + distance; line = new LineSegmentEquation(newStartPoint, newEndPoint); } else //offset to x+ direction { if (Math.Sin(alphaInRad) > 0) { newStartPoint.X = m_startPoint.X + Math.Abs(distance * Math.Sin(alphaInRad)); newStartPoint.Y = m_startPoint.Y - Math.Abs(distance * Math.Cos(alphaInRad)); newEndPoint.X = m_endPoint.X + Math.Abs(distance * Math.Sin(alphaInRad)); newEndPoint.Y = m_endPoint.Y - Math.Abs(distance * Math.Cos(alphaInRad)); line = new LineSegmentEquation( newStartPoint, newEndPoint); } else //sin(FalphaInRad)<0 { newStartPoint.X = m_startPoint.X + Math.Abs(distance * Math.Sin(alphaInRad)); newStartPoint.Y = m_startPoint.Y + Math.Abs(distance * Math.Cos(alphaInRad)); newEndPoint.X = m_endPoint.X + Math.Abs(distance * Math.Sin(alphaInRad)); newEndPoint.Y = m_endPoint.Y + Math.Abs(distance * Math.Cos(alphaInRad)); line = new LineSegmentEquation( newStartPoint, newEndPoint); } } } //{rightOrDown} else //leftOrUp { if (this.IsHorizontalLine) //offset to y directin { newStartPoint.X = m_startPoint.X; newStartPoint.Y = m_startPoint.Y - distance; newEndPoint.X = m_endPoint.X; newEndPoint.Y = m_endPoint.Y - distance; line = new LineSegmentEquation( newStartPoint, newEndPoint); } else //offset to x directin { if (Math.Sin(alphaInRad) >= 0) { newStartPoint.X = m_startPoint.X - Math.Abs(distance * Math.Sin(alphaInRad)); newStartPoint.Y = m_startPoint.Y + Math.Abs(distance * Math.Cos(alphaInRad)); newEndPoint.X = m_endPoint.X - Math.Abs(distance * Math.Sin(alphaInRad)); newEndPoint.Y = m_endPoint.Y + Math.Abs(distance * Math.Cos(alphaInRad)); line = new LineSegmentEquation( newStartPoint, newEndPoint); } else //sin(FalphaInRad)<0 { newStartPoint.X = m_startPoint.X - Math.Abs(distance * Math.Sin(alphaInRad)); newStartPoint.Y = m_startPoint.Y - Math.Abs(distance * Math.Cos(alphaInRad)); newEndPoint.X = m_endPoint.X - Math.Abs(distance * Math.Sin(alphaInRad)); newEndPoint.Y = m_endPoint.Y - Math.Abs(distance * Math.Cos(alphaInRad)); line = new LineSegmentEquation( newStartPoint, newEndPoint); } } } return(line); }