public LineSegment(Point point1, Point point2) { this.point1 = point1.ToVector2(); this.point2 = point2.ToVector2(); if (point1.X <= point2.X) { minX = point1.X; maxX = point2.X; } else { minX = point2.X; maxX = point1.X; } if (point1.Y <= point2.Y) { minY = point1.Y; maxY = point2.Y; } else { minY = point2.Y; maxY = point1.Y; } equation = new LineEquation(this); }
public LineSegment(float x1, float y1, float x2, float y2) { point1 = new Vector2((float)Math.Round(x1, 5), (float)Math.Round(y1, 5)); point2 = new Vector2((float)Math.Round(x2, 5), (float)Math.Round(y2, 5)); if (x1 <= x2) { minX = point1.X; maxX = point2.X; } else { minX = point2.X; maxX = point1.X; } if (y1 <= y2) { minY = point1.Y; maxY = point2.Y; } else { minY = point2.Y; maxY = point1.Y; } equation = new LineEquation(this); }
public List <Vector2> getIntercepts(LineEquation equation) { List <Vector2> intercepts = new List <Vector2>(); if ((vertical && equation.vertical) || (slope == equation.slope)) { //either exact same line or no intersections return(intercepts); } if (vertical) { //this is vertical. other eq is not. get y from other eq where x = this eq's x intercepts.Add(new Vector2(x, equation.slope * x + equation.yIntercept)); } else if (equation.vertical) { //other eq is vertical. this is not. get y from this eq where x = other eq's x intercepts.Add(new Vector2(equation.x, slope * equation.x + yIntercept)); } else { // lines are oblique // y1 = m1 * x1 + b1 // y2 = m2 * x2 + b2 // // set ys equal to each other and solve for x // m1 * x + b1 = m2 * x + b2 // (m1 * x) - (m2 * x) = b2 - b1 // x * (m1 - m2) = b2 - b1 // x = (b2 - b1) / (m1 - m2) // // shove x back into original eq to get y float interceptX = (equation.yIntercept - yIntercept) / (slope - equation.slope); float interceptY = slope * interceptX + yIntercept; intercepts.Add(new Vector2(interceptX, interceptY)); } return(intercepts); }