@@ -119,36 +119,66 @@ class BoxTransformer {
119
119
static Vector2 stopRectAtClampingRect ({
120
120
required Box rect,
121
121
required Box clampingRect,
122
+ required double rotation,
122
123
}) {
123
- final (: side, : amount, : singleIntersection) =
124
- getLargestIntersectionDelta (rect, clampingRect);
125
-
126
- if (singleIntersection) {
127
- return switch (side) {
128
- Side .top || Side .bottom => Vector2 (0 , - amount),
129
- Side .left || Side .right => Vector2 (- amount, 0 ),
130
- };
124
+ final Map <Quadrant , Vector2 > rotatedPoints = {
125
+ for (final MapEntry (key: quadrant, value: point)
126
+ in rect.sidedPoints.entries)
127
+ quadrant: rotatePointAroundVec (rect.center, rotation, point)
128
+ };
129
+
130
+ // Check if any rotated point is outside the clamping rect.
131
+ (
132
+ Side side,
133
+ Quadrant quadrant,
134
+ Vector2 point,
135
+ double dist
136
+ )? biggestOutOfBounds;
137
+ for (final MapEntry (key: quadrant, value: point) in rotatedPoints.entries) {
138
+ if (biggestOutOfBounds == null ) {
139
+ final (side, dist) = clampingRect.distanceOfPoint (point);
140
+ biggestOutOfBounds = (side, quadrant, point, dist);
141
+ } else {
142
+ final (side, dist) = clampingRect.distanceOfPoint (point);
143
+ final (_, biggestDist) =
144
+ clampingRect.distanceOfPoint (biggestOutOfBounds.$3);
145
+ if (dist < biggestDist) {
146
+ biggestOutOfBounds = (side, quadrant, point, dist);
147
+ }
148
+ }
131
149
}
132
150
133
- if (side.isVertical) {
134
- final correctedHeight = rect.height - amount;
135
- final aspectRatio = rect.width / correctedHeight;
136
- final correctedWidth = rect.width * aspectRatio;
151
+ assert (biggestOutOfBounds != null );
137
152
138
- final verticalDelta = rect.height - correctedHeight;
139
- final horizontalDelta = rect.width - correctedWidth;
153
+ final side = biggestOutOfBounds! .$1;
154
+ final quadrant = biggestOutOfBounds.$2;
155
+ final point = biggestOutOfBounds.$3;
156
+ final dist = biggestOutOfBounds.$4;
140
157
141
- return Vector2 (horizontalDelta, verticalDelta) * - 1 ;
142
- } else {
143
- final correctedWidth = rect.width - amount;
144
- final aspectRatio = rect.height / correctedWidth;
145
- final correctedHeight = rect.height * aspectRatio;
158
+ // Move the out of bounds vector by the perpendicular vector of the side
159
+ // that was hit.
160
+ final cardinalCorrection = switch (side) {
161
+ Side .left => Vector2 (- dist, 0 ),
162
+ Side .right => Vector2 (dist, 0 ),
163
+ Side .top => Vector2 (0 , - dist),
164
+ Side .bottom => Vector2 (0 , dist),
165
+ };
146
166
147
- final horizontalDelta = rect.width - correctedWidth;
148
- final verticalDelta = rect.height - correctedHeight ;
167
+ final correctedRotatedPoint =
168
+ Vector2 (point.x + cardinalCorrection.x, point.y + cardinalCorrection.y) ;
149
169
150
- return Vector2 (horizontalDelta, verticalDelta) * - 1 ;
151
- }
170
+ // Rotate back
171
+ final unrotated =
172
+ rotatePointAroundVec (rect.center, - rotation, correctedRotatedPoint);
173
+ final delta = unrotated - rect.pointFromQuadrant (quadrant);
174
+
175
+ print (' quad: ${rect .pointFromQuadrant (quadrant )..round ()}' );
176
+ print ('unrotated: ${unrotated ..round ()}' );
177
+
178
+ // Matrix2.rotation(rotation).transform(delta);
179
+ print ('delta: $delta ' );
180
+
181
+ return delta;
152
182
}
153
183
154
184
/// Resizes the given [initialRect] with given [initialLocalPosition] of
@@ -233,17 +263,17 @@ class BoxTransformer {
233
263
);
234
264
}
235
265
236
- final Box bindingRect = switch (bindingStrategy) {
266
+ final Box initialBindingRect = switch (bindingStrategy) {
237
267
BindingStrategy .originalBox => initialRect,
238
268
BindingStrategy .boundingBox => initialBoundingRect,
239
269
};
240
- final double bindingWidth = bindingRect .width;
241
- final double bindingHeight = bindingRect .height;
270
+ final double initialBindingWidth = initialBindingRect .width;
271
+ final double initialBindingHeight = initialBindingRect .height;
242
272
243
273
// Check if clampingRect is smaller than initialRect.
244
274
// If it is, then we return the initialRect and not resize it.
245
- if (clampingRect.width < bindingWidth ||
246
- clampingRect.height < bindingHeight ) {
275
+ if (clampingRect.width < initialBindingWidth ||
276
+ clampingRect.height < initialBindingHeight ) {
247
277
return ResizeResult (
248
278
rect: initialRect,
249
279
oldRect: initialRect,
@@ -569,40 +599,40 @@ class BoxTransformer {
569
599
);
570
600
}
571
601
572
- static Dimension calculateBoundingSize ({
573
- required double rotation,
574
- required Dimension unrotatedSize,
575
- }) {
576
- final double sinA = sin (rotation);
577
- final double cosA = cos (rotation);
578
-
579
- final double width = unrotatedSize.width;
580
- final double height = unrotatedSize.height;
581
- final double boundingWidth = (width * cosA).abs () + (height * sinA).abs ();
582
- final double boundingHeight = (width * sinA).abs () + (height * cosA).abs ();
583
-
584
- return Dimension (boundingWidth, boundingHeight);
585
- }
586
-
587
- static Box calculateUnrotatedRect ({
588
- required Box boundingBox,
589
- required double rotation,
590
- required double aspectRatio,
591
- }) {
592
- final Vector2 center = boundingBox.center;
593
-
594
- final double width = boundingBox.width * cos (- rotation) +
595
- boundingBox.height * sin (- rotation);
596
-
597
- // derive from aspect ratio.
598
- final double height = width / aspectRatio;
599
-
600
- final Box unrotatedRect = Box .fromCenter (
601
- center: center,
602
- width: width,
603
- height: height,
604
- );
605
-
606
- return unrotatedRect;
607
- }
602
+ // static Dimension calculateBoundingSize({
603
+ // required double rotation,
604
+ // required Dimension unrotatedSize,
605
+ // }) {
606
+ // final double sinA = sin(rotation);
607
+ // final double cosA = cos(rotation);
608
+ //
609
+ // final double width = unrotatedSize.width;
610
+ // final double height = unrotatedSize.height;
611
+ // final double boundingWidth = (width * cosA).abs() + (height * sinA).abs();
612
+ // final double boundingHeight = (width * sinA).abs() + (height * cosA).abs();
613
+ //
614
+ // return Dimension(boundingWidth, boundingHeight);
615
+ // }
616
+
617
+ // static Box calculateUnrotatedRect({
618
+ // required Box boundingBox,
619
+ // required double rotation,
620
+ // required double aspectRatio,
621
+ // }) {
622
+ // final Vector2 center = boundingBox.center;
623
+ //
624
+ // final double width = boundingBox.width * cos(-rotation) +
625
+ // boundingBox.height * sin(-rotation);
626
+ //
627
+ // // derive from aspect ratio.
628
+ // final double height = width / aspectRatio;
629
+ //
630
+ // final Box unrotatedRect = Box.fromCenter(
631
+ // center: center,
632
+ // width: width,
633
+ // height: height,
634
+ // );
635
+ //
636
+ // return unrotatedRect;
637
+ // }
608
638
}
0 commit comments