Jump to content
  • Advertisement
Sign in to follow this  
Taha Ansari

A little mathematical problem to be resolved using programming

This topic is 4168 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Greetings! I have a little mathematical problem that I need to resolve writing appropriate code. Considering the following figure: *************a(0,0) **************|*\* **************|**\* **************|***\* **************|****\b(50,100) **********c(0,50)\***| ****************\**| *****************\*| ******************d(50,100) It is a parallelogram. at point 'a' and 'b', there is some value 0. At point 'c' and 'd', there is some value 500. When I position mouse pointer at 'a', I get 0, and as I move down (straight) towards 'c', there is linear increment till maximum value 500 is displayed at 'c'. Same is the case when I move from point 'b' to 'd'. I could easily put a linear slope formula to acheive what has been stated above. Now comes the fun part: I want to be able to get some value when I position mouse any where in between parallelogram 'abcd'. i.e. while inside 'abcd', I should always be able to get some value in range 0-500 based on above basic criteria (i.e. if I'm near line 'ab' I should be getting value close to 0, and as I move towards line 'cd' I should be getting value closer to 500 no matter where mouse is positioned inside 'abcd' (and not just moving straight from point 'a' or 'b') ). For this I think I also need to consider the slope of line 'ab' (or of line 'cd'). Can some one give me direction in this regard? I would really appretiate any help! Thanks in advance... [Edited by - Taha Ansari on April 19, 2007 1:12:22 AM]

Share this post


Link to post
Share on other sites
Advertisement
I would do something like this:
1) Given a point (x, y) in the parallelogram first find its vertical distance to line ab. You can do this like so:

Let m = the slow of ab, let d = the vertical distance of (x, y) from ab. a.x is the x-coordinate of a, a.y is the y-coordinate of a.

d = (y - a.y) - m*(x - a.x)

2) The value at this point should be the same value as if you were that vertical distance from a on line ac. That is, let's say the value function for line ac is

f(x) = (x - a) * (c - a) / 500

Then the value of (x, y) should be f(d)

3) Profit!

Basically you have a constant gradient from 0 to 500, but it's just been skewed in an easy-to-deal with fashion.

Share this post


Link to post
Share on other sites
I assume 'b' is actually at (50, 50) :)

Think of your value as a function in two variables, being the x and y coordinates of the point:

f(0, 0) = 0
f(50, 50) = 0
f(0, 50) = 500
f(50, 100) = 500

If we want the values to increase linearly, then we want a 'linear combination' of the variables. That is, f should be some combination of a multiple of x and a multiple of y. If we use m and n as the coefficients, we turn our function-equations into plain equations:

0m + 0n = 0
50m + 50n = 0
0m + 50n = 500
50m + 100n = 500

The first gives us no information, of course. The third tells us that n = 10. The second places a constraint that m = -n, so m = -10. This is consistent with the fourth equation, so we find that a linear combination will indeed satisfy the constraints.

Therefore we simply calculate f = -10x + 10y.

(BTW, the y axis normally goes *up* in mathematics.)

Share this post


Link to post
Share on other sites
OK! I get the 'feel' for your solutions, let me try them out so I develop better understanding...I'll let you all know when I get success..

Thanks!

p.s. Zahlman, thanks for pointing out the mistake in the illustrated figure - and I now notice about the y-axis that should be going up..just too much involvement with the graphics API and I seem to have forgotten that!

Share this post


Link to post
Share on other sites
For completeness, what is being described is a planar generalisation of Barycentric Coordinates (or, arguably, a specialisation of Homogeneous Coordinates). The general case, for an arbitrary polytope is more complicated than you'd probably guess, so I won't describe it. But since you're working only with parallelograms, which skew nicely into rectangles, things are far simpler and the proposed answers do the job nicely. If at any point you are compelled to use arbitrary quadrilaterals or the likes, then now you know what terms to search with.

Admiral

Share this post


Link to post
Share on other sites
OK!

The problem has now been solved using linear interpolation, due to which there was no need to calculate slope or the y-intercept.

Thanks for all the help + baycentric and Homogenious links...[smile]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!