public LineCoordinates xCalcOppositeSideLine(double AdjacentSideLength) { var adjSide = new HorizontalLineCoordinates(this.Start, AdjacentSideLength); var angle = Math.Abs(this.Angle); var radians = angle * (Math.PI / 180); var tan = Math.Tan(radians); // length of the opposite side line. var oppLgth = AdjacentSideLength * tan; // the end point of the opposite side line. Where the end point is either // above the adjacent line or below it. Depending on if the line ( the // hypotenuse ) goes up or down. if (this.SlopesUp == true) { var oppTop = new Point(adjSide.End.X, adjSide.End.Y - oppLgth + 1.00); return(new VerticalLineCoordinates(oppTop, oppLgth)); } else { return(new VerticalLineCoordinates(adjSide.End, oppLgth)); } }
public LineCoordinates CalcAdjacentSideLine(double OppLgth) { // calc the adj side line lgth from the angle of this line and the given // opposite side lgth. var angle = Math.Abs(this.Angle); var radians = angle * (Math.PI / 180); var tan = Math.Tan(radians); var adjLgth = OppLgth / tan; var adjLine = new HorizontalLineCoordinates(this.Start, adjLgth); return(adjLine); }
/// <summary> /// Calc the point at which this line intersects with another line. /// </summary> /// <param name="Other"></param> /// <returns></returns> public static Point?IntersectPos(LineCoordinates Line1, LineCoordinates Line2) { // the x-axis range of this line does not intersect at all with the x-axis range // of the other line. if ((Line1.Start.X > Line2.End.X) || (Line1.End.X < Line2.Start.X)) { return(null); } // the y-axis range of this line does not intersect at all with the y-axis range // of the other line. else if ((Line1.TopMost > Line2.BottomMost) || (Line1.BottomMost < Line2.TopMost)) { return(null); } // lines are perpendicular. They intersect at the x-axis of the vertical line and // the y-axis of the horizontal line. else if (Line1.IsVertical && Line2.IsHorizontal) { return(new Point(Line1.Start.X, Line2.Start.Y)); } else if (Line1.IsHorizontal && Line2.IsVertical) { return(new Point(Line1.Start.X, Line2.Start.Y)); } // start pos of this line matches the start or end pos of the other line. else if ((Line1.Start.Equals(Line2.Start)) || (Line1.Start.Equals(Line2.End))) { return(Line1.Start); } // end pos of this line matches the start or end pos of the other line. else if ((Line1.End.Equals(Line2.Start)) || (Line1.End.Equals(Line2.End))) { return(Line2.End); } else { double i_x = 0.00; double i_y = 0.00; // char collisionDetected; double s1_x, s1_y, s2_x, s2_y; s1_x = Line1.End.X - Line1.Start.X; s1_y = Line1.End.Y - Line1.Start.Y; s2_x = Line2.End.X - Line2.Start.X; s2_y = Line2.End.Y - Line2.Start.Y; double s, t; s = (-s1_y * (Line1.Start.X - Line2.Start.X) + s1_x * (Line1.Start.Y - Line2.Start.Y)) / (-s2_x * s1_y + s1_x * s2_y); t = (s2_x * (Line1.Start.Y - Line2.Start.Y) - s2_y * (Line1.Start.X - Line2.Start.X)) / (-s2_x * s1_y + s1_x * s2_y); if (s >= 0 && s <= 1 && t >= 0 && t <= 1) { // Collision detected i_x = Line1.Start.X + (t * s1_x); i_y = Line1.Start.Y + (t * s1_y); return(new Point(i_x, i_y)); } else { return(null); } } #if skip var hi = LineCoordinates.HorizontalIntersect(this, Other); double leftMostX = hi.Line1.Start.X + hi.Line1Ofs; TriangleVertex vertex1; TriangleVertex vertex2; // calc the location and angle of the vertex of the intersect triangle formed // by line1. { Point? pos; double angle; if (hi.Line1Ofs > 0) { var adjLine = new HorizontalLineCoordinates(hi.Line1.Start, hi.Line1Ofs); var oppLine = this.CalcOppositeSideLine(hi.Line1Ofs); var rtTriangle = new RightTriangle() { AdjSide = adjLine, OppSide = oppLine }; pos = rtTriangle.OppVertex.Location; } else { pos = hi.Line1.Start; } angle = IntersectPos_CalcVertexAngle(hi.Line1.Angle); vertex1 = new TriangleVertex() { Angle = angle, Location = pos.Value }; } // calc the left side vertex of the intersect triangle formed by line2. { Point? pos; double angle; if (hi.Line2Ofs > 0) { var adjLine = new HorizontalLineCoordinates(hi.Line2.Start, hi.Line2Ofs); var oppLine = this.CalcOppositeSideLine(hi.Line2Ofs); var rtTriangle = new RightTriangle() { AdjSide = adjLine, OppSide = oppLine }; pos = rtTriangle.OppVertex.Location; } else { pos = hi.Line2.Start; } angle = IntersectPos_CalcVertexAngle(hi.Line2.Angle); vertex2 = new TriangleVertex() { Angle = angle, Location = pos.Value }; } // build the oblique triangle that is formed by the intersection of the // two lines. var obTriangle = new ObliqueTriangle() { Vertex1 = vertex1, Vertex2 = vertex2 }; return(null); }