-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Simplify getNextPositionInDimension #402
Comments
…o calculate 'getting larger', see #402
OK, I added documentation to getNextPositionInDimension and reorganized some of the logic for readability. I think it is much clearer to see what it is doing. I did not pursue the patch in #402 (comment). It looks like that patch is trying to set both x and y from this function and it is important that only one dimension of movement be set at a time. That might be why it wasn't working well when we last looked at this. Anyway, I am happy with where this function is now and ready for re-review or close. |
Assigning for async re-review but we might discuss this again together soon. |
I appreciate the clear and elaborate documentation, thanks! I see opportunity for simplification and improvement. I tried this implementation, with a comparison to see if it behaves exactly as the prior implementation. Every now and then fuzzing with keyboard fuzzing catches a difference, but I have not been able to catch it in a debugger. The idea is that it rounds to the nearest interval, then adds the delta. Does it seem correct to you? Subject: [PATCH] Standardize author annotations
---
Index: js/quadrilateral/model/QuadrilateralModel.ts
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/js/quadrilateral/model/QuadrilateralModel.ts b/js/quadrilateral/model/QuadrilateralModel.ts
--- a/js/quadrilateral/model/QuadrilateralModel.ts (revision 3bd1b43b624a4b364c5e6275f3e4268f3a88ff07)
+++ b/js/quadrilateral/model/QuadrilateralModel.ts (date 1679496701156)
@@ -199,10 +199,31 @@
return new Vector2( nextX, nextY );
}
- /**
- * Returns a new position in x or y dimensions, used by getClosestGridPositionInDirection.
- */
private getNextPositionInDimension( currentPosition: Vector2, directionVector: Vector2, dimension: 'x' | 'y' ): number {
+ const a = this.getNextPositionInDimensionA( currentPosition, directionVector, dimension );
+ const b = this.getNextPositionInDimensionB( currentPosition, directionVector, dimension );
+ console.log( a === b, a, b );
+ if ( a !== b ) {
+ debugger;
+ }
+ return a;
+ }
+
+ private getNextPositionInDimensionB( currentPosition: Vector2, directionVector: Vector2, dimension: 'x' | 'y' ): number {
+ const currentValue = currentPosition[ dimension ];
+ const gettingLarger = directionVector[ dimension ] > 0;
+ const interval = this.vertexIntervalProperty.value;
+
+ const currentValueOnInterval = Utils.roundToInterval( currentValue, interval );
+ const delta = gettingLarger ? interval : -interval;
+
+ return currentValueOnInterval + delta;
+ }
+
+ /**
+ * Returns a new position in x or y dimensions, used by getClosestGridPositionInDirection.
+ */
+ private getNextPositionInDimensionA( currentPosition: Vector2, directionVector: Vector2, dimension: 'x' | 'y' ): number {
const currentValue = currentPosition[ dimension ];
const gettingLarger = directionVector[ dimension ] > 0;
|
Oh, I discovered you have to click in the sim to get keyboard fuzzing to start. @jessegreenberg and I discussed and identified that the main idea is that unless you are within 0.01 of the next interval, you should move to the next interval (if you are below an interval, and within 0.01, then you need to jump 2x intervals). So I feel there is probably a way to simplify the logic, but the proposed implementation above is not correct. |
This seems to be getting closer: private getNextPositionInDimensionB( currentPosition: Vector2, directionVector: Vector2, dimension: 'x' | 'y' ): number {
const currentValue = currentPosition[ dimension ];
const gettingLarger = directionVector[ dimension ] > 0;
const interval = this.vertexIntervalProperty.value;
if ( gettingLarger ) {
return Utils.roundToInterval( currentValue + 0.01 - interval / 2 + interval, interval );
}
else {
return Utils.roundToInterval( currentValue - 0.01 + interval / 2 - interval, interval );
}
} |
I simplified it a bit and it seems to give the same behavior as the existing method. Here is a patch that compares values: Subject: [PATCH] Standardize author annotations
---
Index: js/quadrilateral/model/QuadrilateralModel.ts
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/js/quadrilateral/model/QuadrilateralModel.ts b/js/quadrilateral/model/QuadrilateralModel.ts
--- a/js/quadrilateral/model/QuadrilateralModel.ts (revision 3bd1b43b624a4b364c5e6275f3e4268f3a88ff07)
+++ b/js/quadrilateral/model/QuadrilateralModel.ts (date 1679500880414)
@@ -199,10 +199,30 @@
return new Vector2( nextX, nextY );
}
- /**
- * Returns a new position in x or y dimensions, used by getClosestGridPositionInDirection.
- */
private getNextPositionInDimension( currentPosition: Vector2, directionVector: Vector2, dimension: 'x' | 'y' ): number {
+ const a = this.getNextPositionInDimensionA( currentPosition, directionVector, dimension );
+ const b = this.getNextPositionInDimensionB( currentPosition, directionVector, dimension );
+ console.log( a === b, a, b );
+ if ( a !== b ) {
+ debugger;
+ }
+ return a;
+ }
+
+ private getNextPositionInDimensionB( currentPosition: Vector2, directionVector: Vector2, dimension: 'x' | 'y' ): number {
+ const currentValue = currentPosition[ dimension ];
+ const gettingLarger = directionVector[ dimension ] > 0;
+ const interval = this.vertexIntervalProperty.value;
+
+ const shift = +0.01 - interval / 2 + interval;
+ const sign = gettingLarger ? 1 : -1;
+ return Utils.roundToInterval( currentValue + sign * shift, interval );
+ }
+
+ /**
+ * Returns a new position in x or y dimensions, used by getClosestGridPositionInDirection.
+ */
+ private getNextPositionInDimensionA( currentPosition: Vector2, directionVector: Vector2, dimension: 'x' | 'y' ): number {
const currentValue = currentPosition[ dimension ];
const gettingLarger = directionVector[ dimension ] > 0;
The main idea is that everything in the curly brace maps to the next interval: I'm getting this exception, but it seems unrelated:
|
Thank you @samreid, that is looking great! Confirmed about that issue, looks like it started showing up on CT yesterday morning, Ill look at that. |
I did not attempt to add good code comments, since I was relying on the diagram above. But would be good to comment this strategy and why it works. |
OK, thanks @samreid. Here is what I added based on my understanding of how this is working:
I did a lot of testing and confirmed that this new method always produces the same values as before. Closing. |
From #398, we are interested in improving
getNextPositionInDimension
. We almost got this patch kind of working, but something like this:The text was updated successfully, but these errors were encountered: