Skip to content
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

Use variables and Mathf.Abs() for checking position between source and target tangrams #141

Open
jespirit opened this issue Nov 11, 2018 · 0 comments

Comments

@jespirit
Copy link
Contributor

jespirit commented Nov 11, 2018

Use xPositionDelta and yPositionDelta variables as well as a little bit of algebra for checking if the source (moveable) tangram lies within the x-axis delta or y-axis delta of the target (outline) tangram.

Before:

//Check if x coordinate is in the tolerance range
if (this.myTanPosition.x * 10 < (xInTan * 10 + 3) && this.myTanPosition.x * 10 > (xInTan * 10 - 3)) {
    //Check if y coordinate is in the tolerance range
    if (this.myTanPosition.y * 10 < (yInTan * 10 + 3) && this.myTanPosition.y * 10 > (yInTan * 10 - 3)) {

Update:

private float xPositionDelta = 3.0f;
private float yPositionDelta = 3.0f;

//Check if x coordinate is in the tolerance range
if (Mathf.Abs(this.myTanPosition.x * 10 - xInTan * 10) < xPositionDelta) {
    //Check if y coordinate is in the tolerance range
    if (Mathf.Abs(this.myTanPosition.y * 10 - yInTan * 10) < yPositionDelta) {

Math:

s = this.myTanPosition.x  // source x
d = xInTan  // destination x

= 10s > (10d - 3) && 10s < (10d + 3)
// rearrange 10s to be in the middle of an inequality
= 10d - 3 < 10s < 10d + 3
// subtract -10d from all expressions
= -3 < 10s - 10d < 3
// use absolute
= |10s - 10d| < 3

= |x| < 2
= -2 < x < 2

Also, why do you multiply by 10?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant