I can't figure out how to reverse my function

In summary, the conversation discusses a function that needs to be reversed and the difficulty in doing so. The goal is to calculate the distance from a camera to a point on the floor, and the conversation includes a proposed equation and the use of Wolfram Alpha to solve it. The expert summarizer suggests multiplying the equation by d to get a standard linear equation and provides a code snippet for a potential solution.
  • #1
newjerseyrunner
1,533
637
I have this function that I need to reverse.

int getYPosition(float height, float dist) const {
const float hh = renderer -> viewPortHalfH / dist;
return renderer -> viewPortHalfH - hh + hh * ((height + renderer -> player -> verticalAngle * dist) / 128.0f);
}

float getDistYPosition(float height, int yPosition) const {
//wtf, I can't figure out what this should be
return 1;
}

I can't figure out how to reverse it though, I should be able to take the resulting int, put it through a function with the height value and get back distance. Normally, when I get stuck, I just ask wolfram alpha to solve it for me, but that doesn't seem to be giving me an answer.

y = k - (k/d) + (k/d) * ((h + v * d) / 128) solve d
Is there something with my equation that makes it fundamentally irreversible?

I feel like wolfram alpha is having trouble because the general equation has some quirks that the actual code won't? Like the fact that both renderer -> viewPortHalfH and dist are always positive and non-zero. How would I tell wolfram alpha about this (would it even help?)
 
Mathematics news on Phys.org
  • #2
Are you trying to implement the Pythagorean distance here?
 
  • #3
y = k - (k/d) + (k/d) * ((h + v * d) / 128)
Multiply the equation by d and you get a standard linear equation which you can solve for d.
 
  • Like
Likes newjerseyrunner
  • #4
jedishrfu said:
Are you trying to implement the Pythagorean distance here?
No. I'm not sure what it's called. The first function projects a 2.5D point to a 2D screen, I'm trying to reverse the projection. I don't know x or y position, in fact, I need this distance calculation to figure that out in order to texture it.
Screen Shot 2020-09-22 at 10.28.55 PM.pngIt's for determining distance from a camera across the floor. I knew the distance of the bottom of the wall, and I knew what pixel height it started at. I then need to iterate along each pixel going down and figure out it's distance from the camera.

mfb said:
Multiply the equation by d and you get a standard linear equation which you can solve for d.
That was the step I needed, I was able to figure out the algorithm from there

float yPositionToDist(float height, int pixel) const {
float top = (height - 128.0f) * renderer -> viewPortHalfH;
float bottom = (renderer -> player -> verticalAngle + 128.0f) * renderer -> viewPortHalfH - 128.0f * (float)pixel;
return -top / bottom;
 
  • Like
Likes jedishrfu

1. How do I reverse a function in my code?

To reverse a function, you can use the reverse() method in most programming languages. This method takes the elements of an array or string and reverses their order. You can then use this method on your function to reverse it.

2. Can I use recursion to reverse a function?

Yes, you can use recursion to reverse a function. Recursion is a programming technique where a function calls itself until a specific condition is met. In this case, you can use recursion to reverse the function by calling it on the function's input until you reach the base case, which is the original input.

3. How can I test if my function is properly reversed?

You can test if your function is properly reversed by using test cases. These are predetermined inputs and expected outputs that you can use to check if your function is working correctly. You can also use debugging tools to step through your code and see the output at each step.

4. Are there any specific data types that cannot be reversed?

No, there are no specific data types that cannot be reversed. However, some data types may require different methods or algorithms to reverse them. For example, reversing a string is different from reversing an array, so you may need to use different techniques for each data type.

5. What are some common mistakes to avoid when reversing a function?

Some common mistakes to avoid when reversing a function include not properly defining the base case in a recursive function, not using the correct method for the data type, and not testing the function with different inputs. It is also important to make sure your function is properly reversed and does not have any logical errors.

Similar threads

  • Programming and Computer Science
2
Replies
55
Views
4K
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
891
  • Atomic and Condensed Matter
Replies
5
Views
2K
  • Advanced Physics Homework Help
Replies
5
Views
1K
  • Programming and Computer Science
Replies
20
Views
1K
  • General Math
Replies
4
Views
3K
  • Advanced Physics Homework Help
Replies
12
Views
1K
  • Quantum Physics
Replies
1
Views
963
Replies
2
Views
1K
Back
Top