private Point GetCp4(Rect rect, Point p1, Point p2) { var rightLineEquation = new LineEquation(p1, p2); var dock = Dock; var isBottom = (dock & TabItemDock.Bottom) == TabItemDock.Bottom; if (isBottom) { return(rightLineEquation.IntersectWithHorizontalLine(p2.X, rect.Bottom)); } var isTop = (dock & TabItemDock.Top) == TabItemDock.Top; if (isTop) { return(rightLineEquation.IntersectWithHorizontalLine(p2.X, rect.Top)); } var isLeft = (dock & TabItemDock.Left) == TabItemDock.Left; if (isLeft) { return(rightLineEquation.IntersectWithVerticalLine(p2.Y, rect.Left)); } return(rightLineEquation.IntersectWithVerticalLine(p2.Y, rect.Right)); }
/// <summary> /// if line equation is: [y = kx + b]. if lines matches or parallel retrns (NaN,NaN) /// </summary> public static float2 GetLinesCrossPoint(LineEquation line1Equation, LineEquation line2Equation) { //если одна вертикальная а вторая нет, то считаем на прямую if (float.IsNaN(line1Equation.k) && !float.IsNaN(line2Equation.k)) { return(new float2( line1Equation.b, line2Equation.k * line1Equation.b + line2Equation.b )); } else if (!float.IsNaN(line1Equation.k) && float.IsNaN(line2Equation.k)) { return(new float2( line2Equation.b, line1Equation.k * line2Equation.b + line1Equation.b )); } else if (float.IsNaN(line1Equation.k) && float.IsNaN(line2Equation.k)) { return(new float2(float.NaN, float.NaN)); } else { var cross = new float2(); cross.x = (line2Equation.b - line1Equation.b) / (line1Equation.k - line2Equation.k); cross.y = line1Equation.k * cross.x + line1Equation.b; return(cross); } }
private Point GetCp2(Rect rect, Point p1, Point p2) { var leftLineEquation = new LineEquation(p1, p2); var dock = Dock; var isBottom = (dock & TabItemDock.Bottom) == TabItemDock.Bottom; if (isBottom) { // the 1st control point is the intersection between the slope line and the base line. return(leftLineEquation.IntersectWithHorizontalLine(p2.X, rect.Top)); } var isTop = (dock & TabItemDock.Top) == TabItemDock.Top; if (isTop) { return(leftLineEquation.IntersectWithHorizontalLine(p2.X, rect.Bottom)); } var isLeft = (dock & TabItemDock.Left) == TabItemDock.Left; if (isLeft) { return(leftLineEquation.IntersectWithVerticalLine(p2.Y, rect.Right)); } return(leftLineEquation.IntersectWithVerticalLine(p2.Y, rect.Left)); }
void fillTriangle(Triangle T, int ApexIndex, Color C) { LineEquation L1, L2; Dimension.data.Point Apex = T.wireframeSegment[ApexIndex]; L1 = new LineEquation(Apex, T.wireframeSegment[(ApexIndex + 1) % 3]); L2 = new LineEquation(Apex, T.wireframeSegment[(ApexIndex + 2) % 3]); Dimension.data.Point raster1, raster2, rasterPoint; raster1 = L1.calculateNextPoint(Apex); raster2 = L2.calculateNextPoint(Apex); for (int i = 0; i < L2.length; i++) { LineEquation RasterLine = new LineEquation(raster1, raster2); rasterPoint = RasterLine.calculateNextPoint(raster1); for (int j = 0; j < RasterLine.length; j++) { if (rasterPoint.x >= stageWpx || rasterPoint.y >= stageHpx || rasterPoint.x < 0 || rasterPoint.y < 0) { rasterPoint = RasterLine.calculateNextPoint(rasterPoint); continue; } Outbound.SetPixel((int)rasterPoint.x, (int)rasterPoint.y, C); rasterPoint = RasterLine.calculateNextPoint(rasterPoint); } raster1 = L1.calculateNextPoint(raster1); raster2 = L2.calculateNextPoint(raster2); } }
public static BoundarySegment GetBoundarySegment(Vector p1, Vector p2) { return(new BoundarySegment() { Line = LineEquation.GetLineEquation(p1, p2), Min = new Vector(Math.Min(p1.X, p2.X), Math.Min(p1.Y, p2.Y)), Max = new Vector(Math.Max(p1.X, p2.X), Math.Max(p1.Y, p2.Y)) }); }
public float Distance(PointF p) { if (LineEquation.IsBetween(line.GetNormalAt(A), p, line.GetNormalAt(B))) { return(line.Distance(p)); } return(Math.Min(Distance(A, p), Distance(B, p))); }
public LineSegment(PointF a, PointF b) { A = a; B = b; line = new LineEquation(a, b); bindingRectangle = new RectangleF( Math.Min(a.X, b.X), Math.Min(a.Y, b.Y), Math.Abs(a.X - b.X), Math.Abs(a.Y - b.Y)); }
public bool IntersectWithSegementOfLine(LineEquation otherLine, out Point intersectionPoint) { bool hasIntersection = IntersectsWithLine(otherLine, out intersectionPoint); if (hasIntersection) { return(intersectionPoint.IsBetweenTwoPoints(otherLine.Start, otherLine.End)); } return(false); }
public Form1() { InitializeComponent(); qe = new LineEquation(); qe2 = new LineEquation(); sjeciste = new Sjeciste(); functionPanel1.Function = new Function(qe.Y); functionPanel1.Function2 = new Function2(qe2.Y); functionPanel1.Invalidate(); log4net.Config.XmlConfigurator.Configure(); }
private static Point GetIntersectionPointIfOneIsVertical(LineEquation line1, LineEquation line2) { LineEquation verticalLine = line2.IsVertical ? line2 : line1; LineEquation nonVerticalLine = line2.IsVertical ? line1 : line2; double y = (verticalLine.Start.X - nonVerticalLine.Start.X) * (nonVerticalLine.End.Y - nonVerticalLine.Start.Y) / ((nonVerticalLine.End.X - nonVerticalLine.Start.X)) + nonVerticalLine.Start.Y; double x = line1.IsVertical ? line1.Start.X : line2.Start.X; return(new Point(x, y)); }
/// <summary> /// under or left /// </summary> /// <param name="point"></param> /// <param name="line"></param> /// <param name="trueResultIfLineIncludesPoint"></param> /// <returns></returns> public static bool PointUnderOrLeftLine(float2 point, LineEquation line, bool trueResultIfLineIncludesPoint = true) { if (float.IsNaN(line.k)) { return(trueResultIfLineIncludesPoint ? point.x <= line.b : point.x < line.b); } else { var lineY = line.k * point.x + line.b; return(trueResultIfLineIncludesPoint ? point.y <= lineY : point.y < lineY); } }
public bool GetIntersectionLineForRay(Rect rectangle, out LineEquation intersectionLine) { if (Start == End) { intersectionLine = null; return(false); } IEnumerable <LineEquation> lines = rectangle.GetLinesForRectangle(); intersectionLine = new LineEquation(new Point(0, 0), new Point(0, 0)); var intersections = new Dictionary <LineEquation, Point>(); foreach (LineEquation equation in lines) { Point point; if (IntersectWithSegementOfLine(equation, out point)) { intersections[equation] = point; } } if (!intersections.Any()) { return(false); } var intersectionPoints = new SortedDictionary <double, Point>(); foreach (var intersection in intersections) { if (End.IsBetweenTwoPoints(Start, intersection.Value) || intersection.Value.IsBetweenTwoPoints(Start, End)) { double distanceToPoint = Start.DistanceToPoint(intersection.Value); intersectionPoints[distanceToPoint] = intersection.Value; } } if (intersectionPoints.Count == 1) { Point endPoint = intersectionPoints.First().Value; intersectionLine = new LineEquation(Start, endPoint); return(true); } if (intersectionPoints.Count == 2) { Point start = intersectionPoints.First().Value; Point end = intersectionPoints.Last().Value; intersectionLine = new LineEquation(start, end); return(true); } return(false); }
static void Main(string[] args) { Console.WriteLine($"Площадь треугольника: {Equation.TriangleArea(5, 8)}"); Console.WriteLine($"Площадь квадрата: {Equation.SquareArea(15)}"); Console.WriteLine($"Площадь прямоугольника: {Equation.RecktangleArea(6, 4)}"); string a = "Меня зовут даукен "; string palindrom = "Довод"; Console.WriteLine($"Переворот: {LineEquation.Reverse(a)}"); Console.WriteLine($"Строка {palindrom}: {LineEquation.IsPalindrom(palindrom)}"); Console.WriteLine($"Количество предложений в \"{a}\": {LineEquation.HowManyWords(a)}"); Console.ReadKey(); }
/// <summary> /// if line equation is: [y = kx + b]. if line is vertical returns (0, y), if horisontal returns (NaN,x) /// </summary> public static LineEquation GetPerpendicularLineEquation(float2 linePoint, LineEquation lineEquation) { if (float.IsNaN(lineEquation.k)) { return(new LineEquation(0, linePoint.y)); } if (lineEquation.k == 0) { return(new LineEquation(float.NaN, linePoint.x)); } var k = -1f / lineEquation.k; return(new LineEquation(k, linePoint.y - k * linePoint.x)); }
public Vector2?GetIntersectionWithLine(LineEquation otherLine) { float determinant = A * otherLine.B - otherLine.A * B; //lines are parallel if (Mathf.Approximately(0, determinant)) { return(default(Vector2?)); } //Cramer's Rule float x = (otherLine.B * C - B * otherLine.C) / determinant; float y = (A * otherLine.C - otherLine.A * C) / determinant; return(new Vector2(x, y)); }
private Node GenerateNeighbor(List <LineEquation> validLocations, Node centerNode) { Node node = null; int option = Random.Range(0, validLocations.Count); LineEquation line = validLocations[option]; Tuple <int, int> nodeCoord = line.End; foreach (Node oldNode in nodes.Values) { if (nodeCoord.Equals(oldNode.GetCoords())) { node = oldNode; } } if (node == null) { node = new Node(++id); node.SetCoords(nodeCoord); nodes.Add(node.GetID(), node); } int xRange = nodeCoord.Item1; int yRange = nodeCoord.Item2; if (Mathf.Abs(xRange) == 2 && Mathf.Abs(yRange) == 2) { int oneUnitY = xRange > 0 ? 1 : -1; int oneUnitX = yRange > 0 ? 1 : -1; int x = oneUnitX + centerNode.GetXCoord(); int y = oneUnitY + centerNode.GetYCoord(); nodeCoord = new Tuple <int, int>(x, y); int index = -1; for (int i = 0; i < validLocations.Count; i++) { if (nodeCoord == validLocations[i].End) { index = i; } } if (index > -1) { validLocations.RemoveAt(index); } } return(node); }
public Tuple <double, double> GetIntersectionWithLine(LineEquation otherLine) { double determinant = A * otherLine.B - otherLine.A * B; //lines are parallel if (determinant == 0) { return(null); } //Cramer's Rule double x = (otherLine.B * C - B * otherLine.C) / determinant; double y = (A * otherLine.C - otherLine.A * C) / determinant; return(new Tuple <double, double>(x, y)); }
public void AddInvalidCoords(List <LineEquation> lineList) { foreach (Node node in linkedNodes.Values) { LineEquation neighborLine = new LineEquation(coord, node.GetCoords()); int index = 0; while (index != lineList.Count) { LineEquation centerLine = lineList[index]; if (centerLine.IntersectsAtEdge(neighborLine)) { lineList.RemoveAt(index); continue; } index++; } } }
/// <summary> /// Returns the position that the given ray /// (starting from the center of the given rectangle) /// intersects the given rectangle /// </summary> /// <param name="ray">The ray starting from the rectangle's center</param> /// <param name="rect">The rectangle</param> /// <returns></returns> public static Vector2 rayIntersectRectangle(Vector2 ray, Rect rect) { Vector2 center = rect.center; float rectDiagonalLength = rect.size.magnitude; LineEquation line = new LineEquation( center, center + (ray.normalized * rectDiagonalLength * 10) ); try { return(line.GetIntersectionWithRectangle(rect).Value); } catch (System.InvalidOperationException) { Debug.LogError($"rayIntersectRectangle failed! ray: {ray}, rect: {rect}"); } return(center + ray); }
private Vector Look(double eye) { var dir = direction.RotateDegrees(eye); var lookp = position + dir; var lookLine = LineEquation.GetLineEquation(position, lookp); var closest = new Vector(5.0, 5.0); foreach (var seg in raceTrack.BoundarySegments) { var res = seg.FindIntersect(lookLine); if (res.Intersects && res.Location.WithinBox(position, lookp) && position.DistanceSquare(res.Location) < position.DistanceSquare(closest)) { closest = res.Location; } } return(closest); }
///// <summary> ///// 正交(默认偏差值0.01) ///// </summary> ///// <param name="l1"></param> ///// <param name="l2"></param> ///// <param name="parallel">指定水平</param> ///// <returns>是否正交</returns> //internal static bool GetOrthogonal(this LineSegment2d l1, LineSegment2d l2, object parallel = null) //{ // return l1.All(l2).GetOrthogonal(parallel); //} ///// <summary> ///// 正交(默认偏差值0.01) ///// </summary> ///// <param name="l1"></param> ///// <param name="le"></param> ///// <param name="parallel">指定水平</param> ///// <returns>是否正交</returns> //internal static bool GetOrthogonal(this LineSegment2d l1, LineEquation le, object parallel = null) //{ // l1.Lel(out LineEquation le1); // return le1.All(le).GetOrthogonal(parallel); //} //#endregion ////计算方法 //#region 二、数学基础 ///// <summary> ///// 11.向量点积 ///// </summary> ///// <param name="v1"></param> ///// <param name="v2"></param> ///// <returns></returns> //internal static double DotProduct(Vector2d v1, Vector2d v2) //{ // return v1.X * v2.X + v1.Y * v2.Y; //} ///// <summary> ///// 11.点线点积 ///// </summary> ///// <param name="p"></param> ///// <param name="l"></param> ///// <returns></returns> //internal static double DotProduct(this Point2d p, LineSegment2d l) //{ // return DotProduct(p - l.StartPoint, l.EndPoint - l.StartPoint); //} ///// <summary> ///// 12.向量叉积 ///// </summary> ///// <param name="v1"></param> ///// <param name="v2"></param> ///// <returns>result》0,v1在v2逆时针;result = 0,v1在v2共线;result《0,v1在v2顺时针</returns> //internal static double CrossProduct(Vector2d v1, Vector2d v2) //{ // return v1.X * v2.Y - v2.X * v1.Y; //} ///// <summary> ///// 12.向量叉积 ///// </summary> ///// <param name="p"></param> ///// <param name="l"></param> ///// <returns>result》0,v1在v2逆时针;result = 0,v1在v2共线;result《0,v1在v2顺时针</returns> //internal static double CrossProduct(this Point2d p, LineSegment2d l) //{ // return CrossProduct(p, l.StartPoint, l.EndPoint); //} ///// <summary> ///// 12.向量叉积 ///// </summary> ///// <param name="p"></param> ///// <param name="p1"></param> ///// <param name="p2"></param> ///// <returns>result》0,v1在v2逆时针;result = 0,v1在v2共线;result《0,v1在v2顺时针</returns> //internal static double CrossProduct(this Point2d p, Point2d p1, Point2d p2) //{ // return CrossProduct(p - p1, p2 - p1); //} ///// <summary> ///// 14。向量旋向 ///// </summary> ///// <param name="v1"></param> ///// <param name="v2"></param> ///// <returns></returns> //internal static VTV Ivv(this Vector2d v1, Vector2d v2) //{ // return Isign(CrossProduct(v1, v2)); //} ///// <summary> ///// 15.拐点判断 ///// </summary> ///// <param name="p1"></param> ///// <param name="p2"></param> ///// <param name="p3"></param> ///// <returns>Right,p2凸点;Collinear,三点共线;Left,p2凹点</returns> //internal static VTV Ippp(this Point2d p3, Point2d p1, Point2d p2) //{ // return (p3 - p2).Ivv(p2 - p1); //} ///// <summary> ///// 15.拐点判断 ///// </summary> ///// <param name="p"></param> ///// <param name="l"></param> ///// <returns>Right,p凸点;Collinear,三点共线;Left,p凹点</returns> //internal static VTV Ipl(this Point2d p, LineSegment2d l) //{ // return p.Ippp(l.StartPoint, l.EndPoint); //} ///// <summary> ///// 19.符号传递(d2符号传递d1) ///// </summary> ///// <param name="d1"></param> ///// <param name="d2"></param> ///// <returns></returns> //internal static double Sign(this double d1, double d2) //{ // return d2 >= 0 // ? Math.Abs(d1) // : -Math.Abs(d1); //} ///// <summary> ///// 20.符号函数 ///// </summary> ///// <param name="d"></param> ///// <returns></returns> //internal static VTV Isign(this double d) //{ // return Math.Abs(d) < Eps // ? VTV.Collinear // : d > 0 // ? VTV.Right // : VTV.Left; //} ///// <summary> ///// 11-1.角度类型 ///// </summary> ///// <param name="v1"></param> ///// <param name="v2"></param> ///// <returns></returns> //internal static AT GetAngleType(Vector2d v1, Vector2d v2) //{ // double d = DotProduct(v1, v2); // return d == 0 // ? AT.RightAngle // : d > 0 // ? AT.AcuteAngle // : AT.ObtuseAngle; //} ///// <summary> ///// 11-1.角度类型 ///// </summary> ///// <param name="p"></param> ///// <param name="l"></param> ///// <returns></returns> //internal static AT GetAngleType(this Point2d p, LineSegment2d l) //{ // return GetAngleType(p - l.StartPoint, l.EndPoint - l.StartPoint); //} ///// <summary> ///// 11-1.角度类型 ///// </summary> ///// <param name="l1"></param> ///// <param name="l2"></param> ///// <returns></returns> //internal static AT GetAngleType(this LineSegment2d l1, LineSegment2d l2) //{ // return GetAngleType(l1.EndPoint - l1.StartPoint, l2.EndPoint - l2.StartPoint); //} ///// <summary> ///// 判断多边形方向(顺/逆时针) ///// </summary> ///// <param name="pts"></param> ///// <returns></returns> //internal static bool Direction(ref List<Point2d> pts) //{ // int minus = 0; // if (pts.Count > 2) // { // for (int i = 0; i < pts.Count; i++) // { // Vector2d v1 = pts[(i + 1) % pts.Count] - pts[i]; // Vector2d v2 = pts[(i + 2) % pts.Count] - pts[(i + 1) % pts.Count]; // minus += (int)v2.Ivv(v1); // } // } // if (minus > 0) // { // Point2d pt = pts.FirstOrDefault(); // pts.RemoveAt(0); // pts.Reverse(); // pts.Insert(0, pt); // } // return minus < 0; //} //#endregion //#region 三、几何基础 ///// <summary> ///// 30.向量的参数解及交点的几何数 ///// </summary> ///// <param name="lp"></param> ///// <param name="lq"></param> ///// <param name="sp"></param> ///// <param name="sq"></param> ///// <param name="kp"></param> ///// <param name="kq"></param> ///// <returns></returns> //internal static bool Pvv(LineSegment2d lp, LineSegment2d lq, out double sp, out double sq, out LTL kp, out LTL kq) //{ // sp = 0; kp = 0; kq = 0; // Vector2d vp = lp.StartPoint - lp.EndPoint; // Vector2d vq = lq.StartPoint - lq.EndPoint; // sq = CrossProduct(vp, vq); // if (Math.Abs(sq) > Eps) // { // if (sq >= 0) // { // kp = LTL.Out; // kq = LTL.In; // } // else // { // kp = LTL.In; // kq = LTL.Out; // } // double ux = lq.StartPoint.X - lp.StartPoint.X; // double vy = lq.StartPoint.Y - lp.StartPoint.Y; // sp = (vq.Y * ux - vq.X * vy) / sq; // sq = (vp.Y * ux - vp.X * vy) / sq; // if ((sp < 1 + Eps && sp > -Eps) && (sq < 1 + Eps && sq > -Eps)) // return true; // } // return false; //} //#endregion //#region 四、几何变换 ///// <summary> ///// 84-1.两点中点 ///// </summary> ///// <param name="p"></param> ///// <param name="c"></param> ///// <param name="angle"></param> ///// <returns></returns> //internal static Point2d Rot2d(this Point2d p, Point2d c, double angle) //{ // return new Point2d(c.X + (p.X - c.X) * Math.Cos(angle) - (p.Y - c.Y) * Math.Sin(angle), // c.Y + (p.X - c.X) * Math.Sin(angle) + (p.Y - c.Y) * Math.Cos(angle)); //} //#endregion //#region 五、二维几何 ///// <summary> ///// 84.求分比点 ///// </summary> ///// <param name="p1"></param> ///// <param name="p2"></param> ///// <param name="d"></param> ///// <param name="p"></param> ///// <returns>d》0 内分点,d《0 外分点;|d|《1 靠近p1,|d|》1 靠近p2;d = 1 p1-p2中点</returns> //internal static bool Ppp(Point2d p1, Point2d p2, double d, out Point2d p) //{ // p = Point2d.Origin; // if (Math.Abs(d + 1) >= Eps) // { // p = new Point2d((p1.X + d * p2.X) / (1 + d), (p1.Y + d * p2.Y) / (1 + d)); // return true; // } // return false; //} ///// <summary> ///// 84.求分比点 ///// </summary> ///// <param name="l"></param> ///// <param name="d"></param> ///// <param name="p"></param> ///// <returns>d》0 内分点,d《0 外分点;|d|《1 靠近p1,|d|》1 靠近p2;d = 1 p1-p2中点</returns> //internal static bool Pl(this LineSegment2d l, double d, out Point2d p) //{ // return Ppp(l.StartPoint, l.EndPoint, d, out p); //} ///// <summary> ///// 84-1.两点中点 ///// </summary> ///// <param name="p1"></param> ///// <param name="p2"></param> ///// <returns></returns> //internal static Point2d Ppm(Point2d p1, Point2d p2) //{ // Ppp(p1, p2, 1, out Point2d p); // return p; //} ///// <summary> ///// 84-1.直线段中点 ///// </summary> ///// <param name="l"></param> ///// <returns></returns> //internal static Point2d Plm(this LineSegment2d l) //{ // return Ppm(l.StartPoint, l.EndPoint); //} ///// <summary> ///// 85.求垂足 ///// </summary> ///// <param name="p"></param> ///// <param name="le"></param> ///// <returns>p点到p1-p2线段垂足</returns> //internal static Point2d Ppln(this Point2d p, LineEquation le) //{ // double d = le.A * p.X + le.B * p.Y + le.C; // return new Point2d(p.X - d * le.A, p.Y - d * le.B); //} ///// <summary> ///// 85.求垂足 ///// </summary> ///// <param name="p"></param> ///// <param name="p1"></param> ///// <param name="p2"></param> ///// <returns>p点到p1-p2线段垂足</returns> //internal static Point2d Pppn(this Point2d p, Point2d p1, Point2d p2) //{ // Lpp(p1, p2, out LineEquation le); // double d = le.A * p.X + le.B * p.Y + le.C; // return new Point2d(p.X - d * le.A, p.Y - d * le.B); //} ///// <summary> ///// 85.求垂足 ///// </summary> ///// <param name="p"></param> ///// <param name="l"></param> ///// <returns>p点到l垂足</returns> //internal static Point2d Ppln(this Point2d p, LineSegment2d l) //{ // return p.Pppn(l.StartPoint, l.EndPoint); //} ///// <summary> ///// 85-1.求投影向量 ///// </summary> ///// <param name="v1">要投影</param> ///// <param name="v2">被投影</param> ///// <returns>投影向量</returns> //internal static Vector2d Vvv(this Vector2d v1, Vector2d v2) //{ // return v2 * DotProduct(v1, v2) / DotProduct(v2, v2); //} ///// <summary> ///// 86.求直线外定距离点 ///// </summary> ///// <param name="p"></param> ///// <param name="l"></param> ///// <param name="d">d》0 前进方向右侧去点,d《0 前进方向左侧取点</param> ///// <returns>p点指定距离点</returns> //internal static Point2d Ppldn(this Point2d p, LineSegment2d l, double d) //{ // l.Lel(out LineEquation le); // return p.Ppldn(le, d); //} ///// <summary> ///// 86.求直线外定距离点 ///// </summary> ///// <param name="p"></param> ///// <param name="le"></param> ///// <param name="d">d》0 前进方向右侧去点,d《0 前进方向左侧取点</param> ///// <returns>p点指定距离点</returns> //internal static Point2d Ppldn(this Point2d p, LineEquation le, double d) //{ // return new Point2d(p.X + d * le.A, p.Y + d * le.B); //} ///// <summary> ///// 88.求对称点 ///// </summary> ///// <param name="p"></param> ///// <param name="l"></param> ///// <returns></returns> //internal static Point2d Plp(this Point2d p, LineSegment2d l) //{ // l.Lel(out LineEquation le); // return p.Plp(le); //} ///// <summary> ///// 88.求对称点 ///// </summary> ///// <param name="p"></param> ///// <param name="le"></param> ///// <returns></returns> //internal static Point2d Plp(this Point2d p, LineEquation le) //{ // double d = 2 * (le.A * p.X + le.B * p.Y + le.C); // return new Point2d(p.X - d * le.A, p.Y - d * le.B); //} ///// <summary> ///// 89.直线上定距离点 ///// </summary> ///// <param name="p"></param> ///// <param name="l"></param> ///// <param name="d">d》0 前进方向,d《0 前进方向</param> ///// <returns></returns> //internal static Point2d Plpd(this Point2d p, LineSegment2d l, double d) //{ // l.Lel(out LineEquation le); // return p.Plpd(le, d); //} ///// <summary> ///// 89.直线上定距离点 ///// </summary> ///// <param name="p"></param> ///// <param name="le"></param> ///// <param name="d">d》0 前进方向,d《0 前进方向</param> ///// <returns></returns> //internal static Point2d Plpd(this Point2d p, LineEquation le, double d) //{ // return new Point2d(p.X - d * le.B, p.Y + d * le.A); //} ///// <summary> ///// 90.两直线交点 ///// </summary> ///// <param name="le1"></param> ///// <param name="le2"></param> ///// <param name="p"></param> ///// <returns>false:平行或重合</returns> //internal static bool Pll(this LineEquation le1, LineEquation le2, out Point2d p) //{ // p = Point2d.Origin; // double d = le1.A * le2.B - le1.B * le2.A; // if (Math.Abs(d) > Eps) // { // p = new Point2d((le2.C * le1.B - le1.C * le2.B) / d, (le1.C * le2.A - le1.A * le2.C) / d); // return true; // } // return false; //} ///// <summary> ///// 90.两直线交点 ///// </summary> ///// <param name="l1"></param> ///// <param name="l2"></param> ///// <param name="p"></param> ///// <returns>false:平行或重合</returns> //internal static bool Pll(this LineSegment2d l1, LineSegment2d l2, out Point2d p) //{ // return l1.GetLe().Pll(l2.GetLe(), out p); //} ///// <summary> ///// 92.两直线段交点 ///// </summary> ///// <param name="l1"></param> ///// <param name="l2"></param> ///// <param name="p"></param> ///// <returns></returns> //internal static bool Plsls(this LineSegment2d l1, LineSegment2d l2, out Point2d p) //{ // p = Point2d.Origin; // // // if ((int)l1.StartPoint.Ipl(l2) * (int)l1.EndPoint.Ipl(l2) <= 0 // && (int)l2.StartPoint.Ipl(l1) * (int)l2.EndPoint.Ipl(l1) <= 0) // { // if (l2.Dls() > Eps) // l1.GetLe().Pll(l2.GetLe(), out p); // else if (l1.Dls() > Eps) // l2.GetLe().Pll(l1.GetLe(), out p); // else // p = l1.StartPoint; // return true; // } // return false; //} ///// <summary> ///// 94.方向直线与向量求交 ///// </summary> ///// <param name="le"></param> ///// <param name="l"></param> ///// <param name="p"></param> ///// <returns></returns> //internal static VTV Plv(LineEquation le, LineSegment2d l, out Point2d p) //{ // p = Point2d.Origin; // if (!l.Lel(out LineEquation le1)) // return VTV.Collinear; // if (!Pll(le, le1, out p)) // return Dpl(p, le) < Eps ? VTV.Coincide : VTV.Parallel; // if (Math.Abs(l.EndPoint.X - l.StartPoint.X) > Math.Abs(l.EndPoint.Y - l.StartPoint.Y)) // { // if (p.X < Math.Min(l.StartPoint.X, l.EndPoint.X) - Eps // || p.X > Math.Max(l.StartPoint.X, l.EndPoint.X) + Eps) // return VTV.Collinear; // } // else // { // if (p.Y < Math.Min(l.StartPoint.Y, l.EndPoint.Y) - Eps // || p.Y > Math.Max(l.StartPoint.Y, l.EndPoint.Y) + Eps) // return VTV.Collinear; // } // return Isign(le.A * le1.B - le1.A * le.B); //} /// <summary> /// 109.直线方程式 /// </summary> /// <param name="p1"></param> /// <param name="p2"></param> /// <param name="le"></param> /// <returns></returns> internal static bool Lpp(Point p1, Point p2, out LineEquation le) { le.A = p2.Y - p1.Y; le.B = p1.X - p2.X; le.C = Math.Sqrt(Math.Pow(le.A, 2) + Math.Pow(le.B, 2)); if (le.C > Eps) { le.A /= le.C; le.B /= le.C; le.C = -le.A * p1.X - le.B * p1.Y; return(true); } else { le.A = 1; le.B = 0; le.C = -p1.X; return(false); } }
public PointF?IntersectAtDistance(LineSegment segmentToCut, float width) { // always assuming other.A is the farthest end var distance = width * (line.IsAboveOrRightOf(segmentToCut.A) ? 1 : -1); var parallelLine = line.GetParallelLine(distance); var p = parallelLine.Intersect(segmentToCut.line); if (p.HasValue) { if (LineEquation.IsBetween(line.GetNormalAt(A), p.Value, line.GetNormalAt(B)) && segmentToCut.bindingRectangle.Contains(p.Value)) { return(p); } } List <PointF> points = new List <PointF>(); points.AddRange(segmentToCut.line.Intersect(new CircleEquation(width, A))); points.AddRange(segmentToCut.line.Intersect(new CircleEquation(width, B))); return(GetNearestPoint(segmentToCut.A, points)); }
public List <LineEquation> GenerateIntersecter(Node centerNode) { List <LineEquation> lineList = new List <LineEquation>(); int x = centerNode.GetXCoord(); int y = centerNode.GetYCoord(); Tuple <int, int> start = new Tuple <int, int>(x, y); for (int i = -2; i < 3; i++) { for (int j = -2; j < 3; j++) { Tuple <int, int> end = new Tuple <int, int>(x + i, y + j); LineEquation line = new LineEquation(start, end); if (i != 0 || j != 0) { lineList.Add(line); } } } return(lineList); }
//I am ommiting end points for intersection public bool IntersectsAtEdge(LineEquation otherLine) { Tuple <double, double> intersection = GetIntersectionWithLine(otherLine); if (intersection != null) { if (IsBetweenTwoPoints(intersection, Start, End) && End != otherLine.Start && End != otherLine.End && Start != otherLine.Start && Start != otherLine.End) { return(true); } } else { bool StartInBetween = IsBetweenTwoPoints(otherLine.Start, Start, End); bool EndInBetween = IsBetweenTwoPoints(otherLine.End, Start, End); bool StartSameNode = Start == otherLine.Start || Start == otherLine.End; bool EndSameNode = End == otherLine.Start || End == otherLine.End; if (EndInBetween == false && StartInBetween == false) { StartInBetween = IsBetweenTwoPoints(Start, otherLine.Start, otherLine.End); EndInBetween = IsBetweenTwoPoints(End, otherLine.Start, otherLine.End); return(!(EndInBetween == false && StartInBetween == false)); } else if (StartInBetween != EndInBetween && StartSameNode != EndSameNode) { return(false); } else { return(true); } } return(false); }
void renderTriangleOutline(Triangle T, Color C) { //Will fill in the lines between 2 points using //Equation y=mx+C; for (int i = 0; i < T.wireframeSegment.Count(); i++) { //Outbound.SetPixel((int)T.wireframeSegment[i].x, (int)T.wireframeSegment[i].y, Color.Aqua); int f = (i + 1) % T.wireframeSegment.Count(); LineEquation curLine = new LineEquation(T.wireframeSegment[i], T.wireframeSegment[f]); Dimension.data.Point curPoint = T.wireframeSegment[i]; for (int j = 0; j < curLine.length; j++) { if (curPoint.x >= stageWpx || curPoint.y >= stageHpx || curPoint.x < 0 || curPoint.y < 0) { curPoint = curLine.calculateNextPoint(curPoint); continue; } Outbound.SetPixel((int)curPoint.x, (int)curPoint.y, C); curPoint = curLine.calculateNextPoint(curPoint); } } }
public bool IntersectsWithLine(LineEquation otherLine, out Point intersectionPoint) { intersectionPoint = new Point(0, 0); if (IsVertical && otherLine.IsVertical) { return(false); } if (IsVertical || otherLine.IsVertical) { intersectionPoint = GetIntersectionPointIfOneIsVertical(otherLine, this); return(true); } double delta = A * otherLine.B - otherLine.A * B; bool hasIntersection = Math.Abs(delta - 0) > 0.0001f; if (hasIntersection) { double x = (otherLine.B * C - B * otherLine.C) / delta; double y = (A * otherLine.C - otherLine.A * C) / delta; intersectionPoint = new Point(x, y); } return(hasIntersection); }
public PointF?Intersect(LineEquation other) { if (isVertical && other.isVertical) { return(null); } if (a == other.a) { return(null); } if (isVertical) { return(other.Intersect(xConstForVertical)); } if (other.isVertical) { return(Intersect(other.xConstForVertical)); } // both have slopes and are not parallel var x = (b - other.b) / (other.a - a); return(Intersect(x)); }
public static bool IsBetween(LineEquation l1, PointF p, LineEquation l2) { return(l1.IsAboveOrRightOf(p) ^ l2.IsAboveOrRightOf(p)); }
public (bool Intersects, Vector Location) FindIntersect(LineEquation l) { var intersect = Line.Intersect(l); return(PointWithinBox(intersect), intersect); }
public void CreateIntersection(List <Vector3> roadA, List <Vector3> roadB, float width, int size) { debugPoints = new List <Vector3>(); var firstList = roadA; var secondList = roadB; int firstListIndex = 0; int secondListIndex = 0; float distance = float.MaxValue; //For a curve, need to fine intersection with brute force for (int i = 0; i < firstList.Count; i++) { for (int j = 0; j < secondList.Count; j++) { var d = Vector3.Distance(firstList[i], secondList[j]); if (d < distance) { firstListIndex = i; secondListIndex = j; distance = d; } } } connections = new List <List <Vector3> >(); var firstListA = firstList.GetRange(0, firstListIndex); firstListA.Reverse(); connections.Add(firstListA.Skip(size).ToList()); var firstListB = firstList.GetRange(firstListIndex - 1, firstList.Count() - firstListIndex); connections.Add(firstListB.Skip(size).ToList()); //var lineA1 = new Pair<Vector3>(firstListA.GetRoadPoint(size, -width / 2), firstListB.GetRoadPoint(size, width / 2)); //var lineA2 = new Pair<Vector3>(firstListA.GetRoadPoint(size, width / 2), firstListB.GetRoadPoint(size, -width / 2)); var secondListA = secondList.GetRange(0, secondListIndex); secondListA.Reverse(); connections.Add(secondListA.Skip(size).ToList()); var secondListB = secondList.GetRange(secondListIndex - 1, secondList.Count() - secondListIndex); connections.Add(secondListB.Skip(size).ToList()); //var lineB1 = new Pair<Vector3>(secondListA.GetRoadPoint(size, -width / 2), secondListB.GetRoadPoint(size, width / 2)); //var lineB2 = new Pair<Vector3>(secondListA.GetRoadPoint(size, width / 2), secondListB.GetRoadPoint(size, -width / 2)); var side1 = new Pair <Vector3>(firstListA.GetRoadPoint(size, width / 2), firstListA.GetRoadPoint(size, -width / 2)); var side3 = new Pair <Vector3>(firstListB.GetRoadPoint(size, width / 2), firstListB.GetRoadPoint(size, -width / 2)); var side4 = new Pair <Vector3>(secondListA.GetRoadPoint(size, width / 2), secondListA.GetRoadPoint(size, -width / 2)); var side2 = new Pair <Vector3>(secondListB.GetRoadPoint(size, width / 2), secondListB.GetRoadPoint(size, -width / 2)); var outerPoints = new List <Pair <Vector3> >(); outerPoints.Add(side1); //debugPoints.Add(side2.First); //debugPoints.Add(side4.Second); outerPoints.Add(side2); outerPoints.Add(side3); outerPoints.Add(side4); LineEquation line1 = new LineEquation(side1.First.To2D(), side3.Second.To2D()); LineEquation line2 = new LineEquation(side1.Second.To2D(), side3.First.To2D()); LineEquation line3 = new LineEquation(side2.First.To2D(), side4.Second.To2D()); LineEquation line4 = new LineEquation(side2.Second.To2D(), side4.First.To2D()); var innerPoints = new List <Vector3>(); innerPoints.Add(line1.GetIntersectionWithLine(line3).Value.To3D()); innerPoints.Add(line2.GetIntersectionWithLine(line3).Value.To3D()); innerPoints.Add(line2.GetIntersectionWithLine(line4).Value.To3D()); innerPoints.Add(line1.GetIntersectionWithLine(line4).Value.To3D()); //debugPoints.Add(line1.GetIntersectionWithLine(line3).Value.To3D()); //debugPoints.Add(line1.GetIntersectionWithLine(line4).Value.To3D()); //debugPoints.Add(line2.GetIntersectionWithLine(line3).Value.To3D()); //debugPoints.Add(line2.GetIntersectionWithLine(line4).Value.To3D()); var name = "fourwayintersection"; Mesh mesh = MeshMaker.FourWayIntersection(outerPoints, innerPoints, name); DestroyImmediate(GameObject.Find(name)); GameObject intersection = new GameObject(name); intersection.transform.SetParent(transform); intersection.transform.position = transform.position; intersection.AddComponent <MeshFilter>().sharedMesh = mesh; MeshRenderer rend = intersection.AddComponent <MeshRenderer>(); Material material = (Material)Resources.Load("Materials/test_mat", typeof(Material)); rend.sharedMaterial = material; rend.sharedMaterial.color = Color.white; }