增加曲线图和区域图

This commit is contained in:
monitor1394
2018-09-19 20:33:40 +08:00
parent 37cc8ea63a
commit c5b939c127
4 changed files with 2598 additions and 21 deletions

View File

@@ -179,5 +179,92 @@ namespace xcharts
DrawTriangle(vh, vertexs, p, p2, p3, color); DrawTriangle(vh, vertexs, p, p2, p3, color);
} }
public static List<Vector3> GetBezierList(Vector3 sp, Vector3 ep, int k = 2)
{
Vector3 dir = (ep - sp).normalized;
float dist = Vector3.Distance(sp, ep);
Vector3 cp1 = sp + dist / k * dir * 1;
Vector3 cp2 = sp + dist / k * dir * (k - 1);
cp1.y = sp.y;
cp2.y = ep.y;
int segment = (int)(dist / 0.1f);
return GetBezierList2(sp, ep, segment, cp1, cp2);
}
public static List<Vector3> GetBezierList(Vector3 sp, Vector3 ep, int segment, Vector3 cp)
{
List<Vector3> list = new List<Vector3>();
for (int i = 0; i < segment; i++)
{
list.Add(GetBezier(i / (float)segment, sp, cp, ep));
}
list.Add(ep);
return list;
}
public static List<Vector3> GetBezierList2(Vector3 sp, Vector3 ep, int segment, Vector3 cp, Vector3 cp2)
{
List<Vector3> list = new List<Vector3>();
for (int i = 0; i < segment; i++)
{
list.Add(GetBezier2(i / (float)segment, sp, cp, cp2, ep));
}
list.Add(ep);
return list;
}
public static Vector3 GetBezier(float t, Vector3 sp, Vector3 cp, Vector3 ep)
{
Vector3 aa = sp + (cp - sp) * t;
Vector3 bb = cp + (ep - cp) * t;
return aa + (bb - aa) * t;
}
public static Vector3 GetBezier2(float t, Vector3 sp, Vector3 p1, Vector3 p2, Vector3 ep)
{
t = Mathf.Clamp01(t);
var oneMinusT = 1f - t;
return oneMinusT * oneMinusT * oneMinusT * sp +
3f * oneMinusT * oneMinusT * t * p1 +
3f * oneMinusT * t * t * p2 +
t * t * t * ep;
}
public static List<Vector3> GetBezierN(List<Vector3> arrayToCurve, float smoothness = 1.0f)
{
List<Vector3> points;
List<Vector3> curvedPoints;
int pointsLength = 0;
int curvedLength = 0;
if (smoothness < 1.0f) smoothness = 1.0f;
pointsLength = arrayToCurve.Count;
curvedLength = (pointsLength * Mathf.RoundToInt(smoothness)) - 1;
curvedPoints = new List<Vector3>(curvedLength);
float t = 0.0f;
for (int pointInTimeOnCurve = 0; pointInTimeOnCurve < curvedLength + 1; pointInTimeOnCurve++)
{
t = Mathf.InverseLerp(0, curvedLength, pointInTimeOnCurve);
points = new List<Vector3>(arrayToCurve);
for (int j = pointsLength - 1; j > 0; j--)
{
for (int i = 0; i < j; i++)
{
points[i] = (1 - t) * points[i] + t * points[i + 1];
}
}
curvedPoints.Add(points[0]);
}
return curvedPoints;
}
} }
} }

View File

@@ -8,6 +8,8 @@ namespace xcharts
[System.Serializable] [System.Serializable]
public class LineData public class LineData
{ {
public bool smooth = false;
public bool area = false;
public float pointWid = 1; public float pointWid = 1;
public float tickness = 0.8f; public float tickness = 0.8f;
} }
@@ -47,8 +49,28 @@ namespace xcharts
np = new Vector3(startX + i * scaleWid, zeroY + data.value * coordinateHig / max); np = new Vector3(startX + i * scaleWid, zeroY + data.value * coordinateHig / max);
if (i > 0) if (i > 0)
{
if (lineData.smooth)
{
var list = ChartUtils.GetBezierList(lp, np);
Vector3 start, to;
start = list[0];
for(int k = 1; k < list.Count; k++)
{
to = list[k];
ChartUtils.DrawLine(vh, start, to, lineData.tickness, color);
start = to;
}
}
else
{ {
ChartUtils.DrawLine(vh, lp, np, lineData.tickness, color); ChartUtils.DrawLine(vh, lp, np, lineData.tickness, color);
if (lineData.area)
{
ChartUtils.DrawPolygon(vh, lp, np, new Vector3(np.x, zeroY), new Vector3(lp.x, zeroY), color);
}
}
} }
lp = np; lp = np;
} }

File diff suppressed because it is too large Load Diff

BIN
demo.png

Binary file not shown.

Before

Width:  |  Height:  |  Size: 110 KiB

After

Width:  |  Height:  |  Size: 74 KiB