private void Initialize(float angleInRad, CPoint2D point) { //angleInRad should be between 0-Pi try { //if ((angleInRad<0) ||(angleInRad>Math.PI)) if (angleInRad > 2 * Math.PI) { string errMsg = string.Format("The input line angle" + " {0} is wrong. It should be between 0-2*PI.", angleInRad); InvalidInputGeometryDataException ex = new InvalidInputGeometryDataException(errMsg); throw ex; } if (Math.Abs(angleInRad - Math.PI / 2) < ConstantValue.SmallValue) //vertical line { a = 1; b = 0; c = -point.X; } else //not vertical line { a = (float)-Math.Tan(angleInRad); b = 1; c = -a * point.X - b * point.Y; } } catch (Exception e) { ServiceRegistration.Get<ILogger>().Error("Error initializing CLine", e); } }
public bool SamePoint(CPoint2D other) { float dDeff_X = Math.Abs(m_dCoordinate_X - other.X); float dDeff_Y = Math.Abs(m_dCoordinate_Y - other.Y); return dDeff_X < ConstantValue.SmallValue && dDeff_Y < ConstantValue.SmallValue; }
public static bool SamePoints(CPoint2D Point1, CPoint2D Point2) { float dDiff_X = Math.Abs(Point1.X - Point2.X); float dDiff_Y = Math.Abs(Point1.Y - Point2.Y); return dDiff_X < ConstantValue.SmallValue && dDiff_Y < ConstantValue.SmallValue; }
public bool Equals(CPoint2D other) { return m_dCoordinate_X == other.m_dCoordinate_X && m_dCoordinate_Y == other.m_dCoordinate_Y; }
/************************************************ * 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; }
/********************************************************** Get point location, using windows coordinate system: y-axes points down. Return Value: -1:point at the left of the line (or above the line if the line is horizontal) 0: point in the line segment or in the line segment 's extension 1: point at right of the line (or below the line if the line is horizontal) ***********************************************************/ public int GetPointLocation(CPoint2D point) { float Ax, Ay, Bx, By, Cx, Cy; Bx = m_endPoint.X; By = m_endPoint.Y; Ax = m_startPoint.X; Ay = m_startPoint.Y; Cx = point.X; Cy = point.Y; if (this.HorizontalLine()) { if (Math.Abs(Ay - Cy) < ConstantValue.SmallValue) //equal return 0; else if (Ay > Cy) return -1; //Y Axis points down, point is above the line else //Ay<Cy return 1; //Y Axis points down, point is below the line } else //Not a horizontal line { //make the line direction bottom->up if (m_endPoint.Y > m_startPoint.Y) this.ChangeLineDirection(); float L = this.GetLineSegmentLength(); float s = ((Ay - Cy) * (Bx - Ax) - (Ax - Cx) * (By - Ay)) / (L * L); //Note: the Y axis is pointing down: if (Math.Abs(s - 0) < ConstantValue.SmallValue) //s=0 return 0; //point is in the line or line extension else if (s > 0) return -1; //point is left of line or above the horizontal line else //s<0 return 1; } }
/*** chagne the line's direction ***/ public void ChangeLineDirection() { CPoint2D tempPt; tempPt = this.m_startPoint; this.m_startPoint = this.m_endPoint; this.m_endPoint = tempPt; }
public CLineSegment(CPoint2D startPoint, CPoint2D endPoint) : base(startPoint, endPoint) { this.m_startPoint = startPoint; this.m_endPoint = endPoint; }
/************************************** Calculate intersection point of two lines if two lines are parallel, return null * ************************************/ public CPoint2D IntersecctionWith(CLine line) { CPoint2D point = new CPoint2D(); float a1 = this.a; float b1 = this.b; float c1 = this.c; float a2 = line.a; float b2 = line.b; float c2 = line.c; if (!(this.Parallel(line))) //not parallen { point.X = (c2 * b1 - c1 * b2) / (a1 * b2 - a2 * b1); point.Y = (a1 * c2 - c1 * a2) / (a2 * b2 - a1 * b2); } return point; }
/*** calculate the distance from a given point to the line ***/ public float GetDistance(CPoint2D point) { float x0 = point.X; float y0 = point.Y; float d = (float)Math.Abs(a * x0 + b * y0 + c); d = d / ((float)(Math.Sqrt(a * a + b * b))); return d; }
public bool Equals(CPoint2D other) { return(m_dCoordinate_X == other.m_dCoordinate_X && m_dCoordinate_Y == other.m_dCoordinate_Y); }
public CLine(float angleInRad, CPoint2D point) { Initialize(angleInRad, point); }
/*********** Sort points from Ymin->Ymax ******/ public static void SortPointsByY(CPoint2D[] points) { if (points.Length > 1) { for (int i = 0; i < points.Length - 2; i++) { for (int j = i + 1; j < points.Length - 1; j++) { if (points[i].Y > points[j].Y) { CPoint2D tempPt = points[j]; points[j] = points[i]; points[i] = tempPt; } } } } }
public bool PointInsidePolygon(CPoint2D[] polygonVertices) { if (polygonVertices.Length < 3) //not a valid polygon return false; int nCounter = 0; int nPoints = polygonVertices.Length; CPoint2D s1, p1, p2; s1 = this; p1 = polygonVertices[0]; for (int i = 1; i < nPoints; i++) { p2 = polygonVertices[i % nPoints]; if (s1.Y > Math.Min(p1.Y, p2.Y)) { if (s1.Y <= Math.Max(p1.Y, p2.Y)) { if (s1.X <= Math.Max(p1.X, p2.X)) { if (p1.Y != p2.Y) { float xInters = (s1.Y - p1.Y) * (p2.X - p1.X) / (p2.Y - p1.Y) + p1.X; if ((p1.X == p2.X) || (s1.X <= xInters)) { nCounter++; } } //p1.y != p2.y } } } p1 = p2; } //for loop if ((nCounter % 2) == 0) return false; else return true; }
/*** Distance between two points***/ public float DistanceTo(CPoint2D point) { return (float)Math.Sqrt((point.X - this.X) * (point.X - this.X) + (point.Y - this.Y) * (point.Y - this.Y)); }
/************************************************ * 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); }
public CLine(CPoint2D point1, CPoint2D point2) { try { if (CPoint2D.SamePoints(point1, point2)) { string errMsg = "The input points are the same"; InvalidInputGeometryDataException ex = new InvalidInputGeometryDataException(errMsg); throw ex; } //Point1 and Point2 are different points: if (Math.Abs(point1.X - point2.X) < ConstantValue.SmallValue) //vertical line { Initialize((float)Math.PI / 2, point1); } else if (Math.Abs(point1.Y - point2.Y) < ConstantValue.SmallValue) //Horizontal line { Initialize(0, point1); } else //normal line { float m = (point2.Y - point1.Y) / (point2.X - point1.X); float alphaInRad = (float)Math.Atan(m); Initialize(alphaInRad, point1); } } catch (Exception e) { ServiceRegistration.Get<ILogger>().Error("Error creating CLine", e); } }
/*** Distance between two points***/ public float DistanceTo(CPoint2D point) { return((float)Math.Sqrt((point.X - this.X) * (point.X - this.X) + (point.Y - this.Y) * (point.Y - this.Y))); }