/******************************************************** * To check whether 2 lines segments have an intersection *********************************************************/ public bool IntersectedWith(CLineSegment line) { float x1 = this.m_startPoint.X; float y1 = this.m_startPoint.Y; float x2 = this.m_endPoint.X; float y2 = this.m_endPoint.Y; float x3 = line.m_startPoint.X; float y3 = line.m_startPoint.Y; float x4 = line.m_endPoint.X; float y4 = line.m_endPoint.Y; float 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 { float ua = ((x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3)) / de; float ub = ((x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3)) / de; if ((ua > 0) && (ua < 1) && (ub > 0) && (ub < 1)) { return(true); } else { return(false); } } else //lines are parallel { return(false); } }
/***To check whether the point is in a line segment***/ public bool InLine(CLineSegment lineSegment) { bool bInline = false; float Ax, Ay, Bx, By, Cx, Cy; Bx = lineSegment.EndPoint.X; By = lineSegment.EndPoint.Y; Ax = lineSegment.StartPoint.X; Ay = lineSegment.StartPoint.Y; Cx = this.m_dCoordinate_X; Cy = this.m_dCoordinate_Y; float L = lineSegment.GetLineSegmentLength(); float s = Math.Abs(((Ay - Cy) * (Bx - Ax) - (Ax - Cx) * (By - Ay)) / (L * L)); if (Math.Abs(s - 0) < ConstantValue.SmallValue) { if ((SamePoints(this, lineSegment.StartPoint)) || (SamePoints(this, lineSegment.EndPoint))) { bInline = true; } else if ((Cx < lineSegment.GetXmax()) && (Cx > lineSegment.GetXmin()) && (Cy < lineSegment.GetYmax()) && (Cy > lineSegment.GetYmin())) { bInline = true; } } return(bInline); }
/***Check whether this line is in a longer line***/ public bool InLine(CLineSegment longerLineSegment) { bool bInLine = false; if ((m_startPoint.InLine(longerLineSegment)) && (m_endPoint.InLine(longerLineSegment))) { bInLine = true; } return(bInLine); }
/******************************************************** To check whether 2 lines segments have an intersection *********************************************************/ public bool IntersectedWith(CLineSegment line) { float x1 = this.m_startPoint.X; float y1 = this.m_startPoint.Y; float x2 = this.m_endPoint.X; float y2 = this.m_endPoint.Y; float x3 = line.m_startPoint.X; float y3 = line.m_startPoint.Y; float x4 = line.m_endPoint.X; float y4 = line.m_endPoint.Y; float 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 { float ua = ((x4 - x3) * (y1 - y3) - (y4 - y3) * (x1 - x3)) / de; float ub = ((x2 - x1) * (y1 - y3) - (y2 - y1) * (x1 - x3)) / de; if ((ua > 0) && (ua < 1) && (ub > 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 CLineSegment OffsetLine(float 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; CPoint2D newStartPoint = new CPoint2D(); CPoint2D newEndPoint = new CPoint2D(); float 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 = (float)(m_startPoint.X + Math.Abs(distance * Math.Sin(alphaInRad))); newStartPoint.Y = (float)(m_startPoint.Y - Math.Abs(distance * Math.Cos(alphaInRad))); newEndPoint.X = (float)(m_endPoint.X + Math.Abs(distance * Math.Sin(alphaInRad))); newEndPoint.Y = (float)(m_endPoint.Y - Math.Abs(distance * Math.Cos(alphaInRad))); line = new CLineSegment(newStartPoint, newEndPoint); } else //sin(FalphaInRad)<0 { newStartPoint.X = (float)(m_startPoint.X + Math.Abs(distance * Math.Sin(alphaInRad))); newStartPoint.Y = (float)(m_startPoint.Y + Math.Abs(distance * Math.Cos(alphaInRad))); newEndPoint.X = (float)(m_endPoint.X + Math.Abs(distance * Math.Sin(alphaInRad))); newEndPoint.Y = (float)(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 = (float)(m_startPoint.X - Math.Abs(distance * Math.Sin(alphaInRad))); newStartPoint.Y = (float)(m_startPoint.Y + Math.Abs(distance * Math.Cos(alphaInRad))); newEndPoint.X = (float)(m_endPoint.X - Math.Abs(distance * Math.Sin(alphaInRad))); newEndPoint.Y = (float)(m_endPoint.Y + Math.Abs(distance * Math.Cos(alphaInRad))); line = new CLineSegment( newStartPoint, newEndPoint); } else //sin(FalphaInRad)<0 { newStartPoint.X = (float)(m_startPoint.X - Math.Abs(distance * Math.Sin(alphaInRad))); newStartPoint.Y = (float)(m_startPoint.Y - Math.Abs(distance * Math.Cos(alphaInRad))); newEndPoint.X = (float)(m_endPoint.X - Math.Abs(distance * Math.Sin(alphaInRad))); newEndPoint.Y = (float)(m_endPoint.Y - Math.Abs(distance * Math.Cos(alphaInRad))); line = new CLineSegment(newStartPoint, newEndPoint); } } } return line; }
/***Check whether this line is in a longer line***/ public bool InLine(CLineSegment longerLineSegment) { bool bInLine = false; if ((m_startPoint.InLine(longerLineSegment)) && (m_endPoint.InLine(longerLineSegment))) bInLine = true; return bInLine; }
/***To check whether the point is in a line segment***/ public bool InLine(CLineSegment lineSegment) { bool bInline = false; float Ax, Ay, Bx, By, Cx, Cy; Bx = lineSegment.EndPoint.X; By = lineSegment.EndPoint.Y; Ax = lineSegment.StartPoint.X; Ay = lineSegment.StartPoint.Y; Cx = this.m_dCoordinate_X; Cy = this.m_dCoordinate_Y; float L = lineSegment.GetLineSegmentLength(); float s = Math.Abs(((Ay - Cy) * (Bx - Ax) - (Ax - Cx) * (By - Ay)) / (L * L)); if (Math.Abs(s - 0) < ConstantValue.SmallValue) { if ((SamePoints(this, lineSegment.StartPoint)) || (SamePoints(this, lineSegment.EndPoint))) bInline = true; else if ((Cx < lineSegment.GetXmax()) && (Cx > lineSegment.GetXmin()) && (Cy < lineSegment.GetYmax()) && (Cy > lineSegment.GetYmin())) bInline = true; } return bInline; }
/************************************************ * 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(float 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; CPoint2D newStartPoint = new CPoint2D(); CPoint2D newEndPoint = new CPoint2D(); float 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 = (float)(m_startPoint.X + Math.Abs(distance * Math.Sin(alphaInRad))); newStartPoint.Y = (float)(m_startPoint.Y - Math.Abs(distance * Math.Cos(alphaInRad))); newEndPoint.X = (float)(m_endPoint.X + Math.Abs(distance * Math.Sin(alphaInRad))); newEndPoint.Y = (float)(m_endPoint.Y - Math.Abs(distance * Math.Cos(alphaInRad))); line = new CLineSegment(newStartPoint, newEndPoint); } else //sin(FalphaInRad)<0 { newStartPoint.X = (float)(m_startPoint.X + Math.Abs(distance * Math.Sin(alphaInRad))); newStartPoint.Y = (float)(m_startPoint.Y + Math.Abs(distance * Math.Cos(alphaInRad))); newEndPoint.X = (float)(m_endPoint.X + Math.Abs(distance * Math.Sin(alphaInRad))); newEndPoint.Y = (float)(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 = (float)(m_startPoint.X - Math.Abs(distance * Math.Sin(alphaInRad))); newStartPoint.Y = (float)(m_startPoint.Y + Math.Abs(distance * Math.Cos(alphaInRad))); newEndPoint.X = (float)(m_endPoint.X - Math.Abs(distance * Math.Sin(alphaInRad))); newEndPoint.Y = (float)(m_endPoint.Y + Math.Abs(distance * Math.Cos(alphaInRad))); line = new CLineSegment( newStartPoint, newEndPoint); } else //sin(FalphaInRad)<0 { newStartPoint.X = (float)(m_startPoint.X - Math.Abs(distance * Math.Sin(alphaInRad))); newStartPoint.Y = (float)(m_startPoint.Y - Math.Abs(distance * Math.Cos(alphaInRad))); newEndPoint.X = (float)(m_endPoint.X - Math.Abs(distance * Math.Sin(alphaInRad))); newEndPoint.Y = (float)(m_endPoint.Y - Math.Abs(distance * Math.Cos(alphaInRad))); line = new CLineSegment(newStartPoint, newEndPoint); } } } return(line); }