/// <summary> /// Trims a rect in axis units into the visible portion of it in axis units. returns false if the parameter rect is completely out of view , true otherwise /// </summary> /// <param name="rect"></param> /// <param name="trimmed"></param> /// <returns></returns> public bool TrimRect(DoubleRect rect, out DoubleRect trimmed) { DoubleVector3 min = rect.min; DoubleVector3 max = rect.max; trimmed = new DoubleRect(); min = PointToNormalized(min.x, min.y); max = PointToNormalized(max.x, max.y); if (min.x > 1f || min.y > 1f) { return(false); } if (max.x < 0f || max.y < 0f) { return(false); } double minX = ChartCommon.Clamp(Math.Min(min.x, max.x)); double minY = ChartCommon.Clamp(Math.Min(min.y, max.y)); double maxX = ChartCommon.Clamp(Math.Max(min.x, max.x)); double maxY = ChartCommon.Clamp(Math.Max(min.y, max.y)); min = NormalizedToPoint(minX, minY); max = NormalizedToPoint(maxX, maxY); trimmed = new DoubleRect(min.x, min.y, max.x - min.x, max.y - min.y); return(true); }
/// <summary> /// returns true if the axis unity rect is visible on the chart, even if it is only partially visible. false otherwise /// </summary> /// <param name="rect"></param> /// <returns></returns> public bool IsRectVisible(DoubleRect rect) { DoubleVector3 min = rect.min; DoubleVector3 max = rect.max; min = PointToNormalized(min.x, min.y); max = PointToNormalized(max.x, max.y); if (min.x > 1f || min.y > 1f) { return(false); } if (max.x < 0f || max.y < 0f) { return(false); } return(true); }
/// <summary> /// tranforms an axis unit rect into a recttransform. The rect transform must have a parent assigned to it. Also the chart and the rectTransform must share a common ancestor canvas /// </summary> /// <param name="assignTo">The rect tranform to which the result is assigned to</param> /// <param name="rect"></param> /// <param name="catgeory"></param> /// <returns></returns> public bool RectToCanvas(RectTransform assignTo, DoubleRect rect, string catgeory = null) { DoubleVector3 min = rect.min; DoubleVector3 max = rect.max; Vector3 worldMin, worldMax; if (PointToWorldSpace(out worldMin, min.x, min.y, catgeory) == false) { return(false); } if (PointToWorldSpace(out worldMax, max.x, max.y, catgeory) == false) { return(false); } Transform parent = assignTo.parent; if (parent == null) { return(false); } worldMin = parent.transform.InverseTransformPoint(worldMin); worldMax = parent.transform.InverseTransformPoint(worldMax); float minX = Math.Min(worldMin.x, worldMax.x); float minY = Math.Min(worldMin.y, worldMax.y); float sizeX = Math.Max(worldMin.x, worldMax.x) - minX; float sizeY = Math.Max(worldMin.y, worldMax.y) - minY; assignTo.anchorMin = new Vector2(0.5f, 0.5f); assignTo.anchorMax = new Vector2(0.5f, 0.5f); assignTo.pivot = new Vector2(0f, 0f); assignTo.anchoredPosition = new Vector2(minX, minY); assignTo.sizeDelta = new Vector2(sizeX, sizeY); return(true); }