// indices arrays are optional - if null is passed the index will be 0, 1, 2... up to values array length. // this is done to avoid allocating a separate array just to pass linear indices internal static UVTransform CalculateDelta(IList <Vector2> src, IList <int> srcIndices, IList <Vector2> dst, IList <int> dstIndices) { // rotate to match target points by comparing the angle between old UV and new auto projection Vector2 dstAngle = dst[GetIndex(dstIndices, 1)] - dst[GetIndex(dstIndices, 0)]; Vector2 srcAngle = src[GetIndex(srcIndices, 1)] - src[GetIndex(srcIndices, 0)]; float rotation = Vector2.Angle(dstAngle, srcAngle); if (Vector2.Dot(Vector2.Perpendicular(dstAngle), srcAngle) < 0) { rotation = 360f - rotation; } Vector2 dstCenter = dstIndices == null?PBBounds2D.Center(dst) : PBBounds2D.Center(dst, dstIndices); // inverse the rotation to get an axis-aligned scale Vector2 dstSize = GetRotatedSize(dst, dstIndices, dstCenter, -rotation); var srcBounds = srcIndices == null ? new PBBounds2D(src) : new PBBounds2D(src, srcIndices); return(new UVTransform() { translation = dstCenter - srcBounds.center, rotation = rotation, scale = dstSize.DivideBy(srcBounds.size) }); }
/// <summary> /// Returns true if any part of the line segment is contained within this bounding box. /// </summary> /// <param name="lineStart"></param> /// <param name="lineEnd"></param> /// <returns></returns> //public bool IntersectsLineSegment(Vector2 lineStart, Vector2 lineEnd) //{ // if (ContainsPoint(lineStart) || ContainsPoint(lineEnd)) // { // return true; // } // else // { // Vector2[] aabb = corners; // return (Math.GetLineSegmentIntersect(aabb[0], aabb[1], lineStart, lineEnd) || // Math.GetLineSegmentIntersect(aabb[1], aabb[3], lineStart, lineEnd) || // Math.GetLineSegmentIntersect(aabb[3], aabb[2], lineStart, lineEnd) || // Math.GetLineSegmentIntersect(aabb[2], aabb[0], lineStart, lineEnd)); // } //} /// <summary> /// Returns true if bounds overlap. /// </summary> /// <param name="bounds"></param> /// <returns></returns> public bool Intersects(PBBounds2D bounds) { Vector2 dist = this.center - bounds.center; Vector2 size = this.size + bounds.size; return(Mathf.Abs(dist.x) * 2f < size.x && Mathf.Abs(dist.y) * 2f < size.y); }