Adventure Game Studio

Community => General Discussion => Topic started by: abstauber on Fri 09/04/2010 09:22:06

Title: Solved math problem of the day ;) Hooray!
Post by: abstauber on Fri 09/04/2010 09:22:06
If just somebody would have told me back then, that I'd actually need this stuff.  :-[

Anyhow: I need to calculate points on the diagonal of this rectangle. I know rectangle's size and coordinates, I even found a formula to calculate the length of the diagonal, but nothing for finding points on it.

So can anyone help me to find e.g. this marked blue cross at the given arrow?
(http://shatten.sonores.de/wp-content/uploads/2010/04/rectangle.png)

edit: y = [(x - x1)(y2 - y1)]/(x2 - x1) + y1

Pleeeasse?? :)
Title: Re: Math problem of the day :)
Post by: Gilbert on Fri 09/04/2010 09:54:20
Depends on how you want the formula to be.

As you already know the coordinates of the two opposite vertices of the rectangle it's quite easy.

Say, the two vertices are (x1, y1) and (x2, y2). Then, the equation of the diagonal can be easily derived as:
(y-y1)/(x-x1)=(y2-y1)/(x2-x1)
1. If you say, know the x-coordinate of the point on the diagonal, you can derive a formula to find the respective y coordinate by making y the subject:
y = [(x - x1)(y2 - y1)]/(x2 - x1) + y1
If you want to find x from y instead you can make x the subject in a similar way.
(and for obvious reasons, if the diagonal is vertical or horizontal you may bring special attention)
2. If you know the ratio of how the the point cuts the diagoal you don't even need the equation. Just do a linear interpolation. Say, if the point (x, y) on the diagonal divides the diagonal in a ratio a:b just do:
x = (b x1 + a x2)/(a + b)
y = (b y1 + a y2)/(a + b)



Title: Re: Math problem of the day :)
Post by: on Fri 09/04/2010 10:06:49
What Gilbet said.

I think that probably calculating the m (i.e. the slope of the diagonal), storing it somewhere and repeatedly using it with method 2 would be the better option.

From a computational point of view, I wonder if there are cases where an arc sin/cos would be faster than interpolation.
Title: Re: Math problem of the day :)
Post by: Gilbert on Fri 09/04/2010 10:30:41
Depends on how frequently the thing is done. Unless for intensive usage it doesn't really matter using either method (even with trigonometric functions as mentioned). :)

Note that simple algebraic methods work even without floating point/decimal support. If you only need integer computations (in such case trigonometric functions are not practical) just make sure that you arrange the order of the calculations to make the division the final step to minimise the rounding errors.
Title: Re: Math problem of the day :)
Post by: abstauber on Fri 09/04/2010 10:39:29
Yay, thanks guys!

I completely understand method 1... (or better I'm able to recreate it :D )

But as for method 2, which seems to be the better solution, I'm still a bit confused.
The rectangles always have the same size, so I'm able to calculate the slope (m), as bici advised.

Quoterepeatedly using it with method 2 would be the better
???

I know I have to talent for this ;)
Title: Re: Math problem of the day :)
Post by: Gilbert on Fri 09/04/2010 10:54:21
I think he just meant using the equation   y = mx + b (where m = (y2 - y1)/(x2 - x1)  and you can find the y-intercept  b  by substituting (x1, x2) into the equation), and if you know  x  you can find  y (and vice versa), though I'll say this is actually method 1.
Title: Re: Math problem of the day :)
Post by: abstauber on Fri 09/04/2010 10:58:18
You are a life saver ;D

Thanks a lot!
Title: Re: UNSolved math problem of the day ;) Hooray!
Post by: InCreator on Fri 09/04/2010 15:38:25
/edit/ drats, I'll better make new thread
Glad you got help!