/******************************************************** * To check whether 2 lines segments have an intersection *********************************************************/ public bool IntersectedWith(CLineSegment 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) < CPolygon.thresh) //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 ((ub > 0) && (ub < 1)) { return(true); } else { return(false); } } else //lines are parallel { return(false); } }
/***Check whether this line is in a longer line***/ public bool InLine(CLineSegment longerLineSegment) { bool bInLine = false; if (longerLineSegment.PointInLine(m_startPoint) && longerLineSegment.PointInLine(m_endPoint)) { bInLine = true; } return(bInLine); }
/********************************************************** * 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(Vector2d[] trianglePts, Vector2d pt) { if (trianglePts.Length != 3) { return(false); } for (int i = trianglePts.GetLowerBound(0); i < trianglePts.GetUpperBound(0); i++) { if (pt.EqualsPoint(trianglePts[i])) { return(true); } } bool bIn = false; CLineSegment line0 = new CLineSegment(trianglePts[0], trianglePts[1]); CLineSegment line1 = new CLineSegment(trianglePts[1], trianglePts[2]); CLineSegment line2 = new CLineSegment(trianglePts[2], trianglePts[0]); if (line0.PointInLine(pt) || line1.PointInLine(pt) || line2.PointInLine(pt)) { bIn = true; } else //point is not in the lines { double dblArea0 = CPolygon.PolygonArea(new Vector2d[] { trianglePts[0], trianglePts[1], pt }); double dblArea1 = CPolygon.PolygonArea(new Vector2d[] { trianglePts[1], trianglePts[2], pt }); double dblArea2 = CPolygon.PolygonArea(new Vector2d[] { 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); }
/************************************************ * 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 CLineSegment 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 CLineSegment line; MyVector2 newStartPoint = new MyVector2(); MyVector2 newEndPoint = new MyVector2(); double alphaInRad = this.GetLineAngle(); // 0-PI if (rightOrDown) { if (this.HorizontalLine()) //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 CLineSegment(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 CLineSegment( 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 CLineSegment( newStartPoint, newEndPoint); } } } //{rightOrDown} else //leftOrUp { if (this.HorizontalLine()) //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 CLineSegment( 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 CLineSegment( 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 CLineSegment( newStartPoint, newEndPoint); } } } return(line); }