diff --git a/Runtime/Coord/Grid/GridCoord.cs b/Runtime/Coord/Grid/GridCoord.cs index f716f09b..52982881 100644 --- a/Runtime/Coord/Grid/GridCoord.cs +++ b/Runtime/Coord/Grid/GridCoord.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using UnityEngine; using XUGL; @@ -174,5 +175,31 @@ namespace XCharts.Runtime return true; return false; } + + /// + /// 给定的线段和Grid边界的交点 + /// + /// + /// + /// + public bool BoundaryPoint(Vector3 sp, Vector3 ep, ref List point) + { + if (Contains(sp) && Contains(ep)) + return false; + var lb = new Vector3(context.x, context.y); + var lt = new Vector3(context.x, context.y + context.height); + var rt = new Vector3(context.x + context.width, context.y + context.height); + var rb = new Vector3(context.x + context.width, context.y); + var flag = false; + if (UGLHelper.GetIntersection(sp, ep, lb, lt, ref point)) + flag = true; + if (UGLHelper.GetIntersection(sp, ep, lt, rt, ref point)) + flag = true; + if (UGLHelper.GetIntersection(sp, ep, lb, rb, ref point)) + flag = true; + if (UGLHelper.GetIntersection(sp, ep, rb, rt, ref point)) + flag = true; + return flag; + } } } \ No newline at end of file diff --git a/Runtime/Serie/Line/LineHelper.cs b/Runtime/Serie/Line/LineHelper.cs index 1dc5fa96..dcbe6957 100644 --- a/Runtime/Serie/Line/LineHelper.cs +++ b/Runtime/Serie/Line/LineHelper.cs @@ -282,6 +282,7 @@ namespace XCharts.Runtime var lastDataIsIgnore = datas[0].isIgnoreBreak; var smooth = serie.lineType == LineType.Smooth; var firstInGridPointIndex = smooth && serie.clip ? -1 : 1; + var interactPoints = new List(); for (int i = 1; i < dataCount; i++) { var cdata = datas[i]; @@ -313,19 +314,26 @@ namespace XCharts.Runtime if (firstInGridPointIndex > 0 && !grid.Contains(np)) isClip = true; } - else if (i == 1 || i == dataCount - 1) + else { var isLpInGrid = grid.Contains(lp); var isCpInGrid = grid.Contains(cp); - if (i == 1 && !isLpInGrid) + if (!isLpInGrid || !isCpInGrid) { - grid.BoundaryPoint(lp, cp, ref lp); - } - if (i == dataCount -1 && !isCpInGrid) - { - if (grid.BoundaryPoint(lp, cp, ref cp)) + interactPoints.Clear(); + if (grid.BoundaryPoint(lp, cp, ref interactPoints)) { - np = cp; + if (interactPoints.Count >= 2) + { + lp = interactPoints[0]; + cp = interactPoints[1]; + } + else if (isLpInGrid) + cp = interactPoints[0]; + else + lp = interactPoints[0]; + if (i == dataCount - 1) + np = cp; } } } diff --git a/Runtime/XUGL/UGLHelper.cs b/Runtime/XUGL/UGLHelper.cs index d394d2e0..0ec9158d 100644 --- a/Runtime/XUGL/UGLHelper.cs +++ b/Runtime/XUGL/UGLHelper.cs @@ -133,8 +133,8 @@ namespace XUGL cp2 = ep - (nep - sp).normalized * diff; if (limit) cp2.y = ep.y; } - int segment = (int) (dist / (smoothness <= 0 ? 2f : smoothness)); - if (segment < 1) segment = (int) (dist / 0.5f); + int segment = (int)(dist / (smoothness <= 0 ? 2f : smoothness)); + if (segment < 1) segment = (int)(dist / 0.5f); if (segment < 4) segment = 4; GetBezierList2(ref posList, sp, ep, segment, cp1, cp2); if (posList.Count < 2) @@ -154,7 +154,7 @@ namespace XUGL Vector3 cp2 = sp + dist / k * dir * (k - 1); cp1.x = sp.x; cp2.x = ep.x; - int segment = (int) (dist / (smoothness <= 0 ? 2f : smoothness)); + int segment = (int)(dist / (smoothness <= 0 ? 2f : smoothness)); GetBezierList2(ref posList, sp, ep, segment, cp1, cp2); if (posList.Count < 2) { @@ -169,7 +169,7 @@ namespace XUGL List list = new List(); for (int i = 0; i < segment; i++) { - list.Add(GetBezier(i / (float) segment, sp, cp, ep)); + list.Add(GetBezier(i / (float)segment, sp, cp, ep)); } list.Add(ep); return list; @@ -185,7 +185,7 @@ namespace XUGL } for (int i = 0; i < segment; i++) { - posList.Add((GetBezier2(i / (float) segment, sp, cp, cp2, ep))); + posList.Add((GetBezier2(i / (float)segment, sp, cp, cp2, ep))); } posList.Add(ep); } @@ -273,6 +273,30 @@ namespace XUGL return true; } + /// + /// 获得两直线的交点 + /// + /// 线段1起点 + /// 线段1终点 + /// 线段2起点 + /// 线段2终点 + /// 相交点。当不相交时为初始值 + /// 相交则返回 true, 否则返回 false + public static bool GetIntersection(Vector3 p1, Vector3 p2, Vector3 p3, Vector3 p4, ref List intersection) + { + var d = (p2.x - p1.x) * (p4.y - p3.y) - (p2.y - p1.y) * (p4.x - p3.x); + if (d == 0) + return false; + + var u = ((p3.x - p1.x) * (p4.y - p3.y) - (p3.y - p1.y) * (p4.x - p3.x)) / d; + var v = ((p3.x - p1.x) * (p2.y - p1.y) - (p3.y - p1.y) * (p2.x - p1.x)) / d; + if (u < 0 || u > 1 || v < 0 || v > 1) + return false; + + intersection.Add(new Vector3(p1.x + u * (p2.x - p1.x), p1.y + u * (p2.y - p1.y))); + return true; + } + /// /// 三个点画线段所需要的六个关键点 ///