Line支持Clip

This commit is contained in:
monitor1394
2023-05-09 13:25:42 +08:00
parent c29a22c576
commit 29c9f59762
3 changed files with 72 additions and 13 deletions

View File

@@ -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;
}
/// <summary>
/// 给定的线段和Grid边界的交点
/// </summary>
/// <param name="sp"></param>
/// <param name="ep"></param>
/// <returns></returns>
public bool BoundaryPoint(Vector3 sp, Vector3 ep, ref List<Vector3> 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;
}
}
}

View File

@@ -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<Vector3>();
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;
}
}
}

View File

@@ -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<Vector3> list = new List<Vector3>();
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;
}
/// <summary>
/// 获得两直线的交点
/// </summary>
/// <param name="p1">线段1起点</param>
/// <param name="p2">线段1终点</param>
/// <param name="p3">线段2起点</param>
/// <param name="p4">线段2终点</param>
/// <param name="intersection">相交点。当不相交时为初始值</param>
/// <returns>相交则返回 true, 否则返回 false</returns>
public static bool GetIntersection(Vector3 p1, Vector3 p2, Vector3 p3, Vector3 p4, ref List<Vector3> 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;
}
/// <summary>
/// 三个点画线段所需要的六个关键点
/// </summary>