Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

A little schoolboy math (but my schooldays were a long time ago).

by BrowserUk (Pope)
on Feb 01, 2004 at 13:25 UTC ( #325693=perlquestion: print w/replies, xml ) Need Help??
BrowserUk has asked for the wisdom of the Perl Monks concerning the following question:

I need to fill in the code at the comment in solve(). I know I will get there, it only requires the sine rule and some pythagoras, but it was a (long) while since I did any of this. I'd appreciate some help.

Given a unit square and the positions of A, B, C & D, I need to calculate the X & Y of P.

+-----------------------------------------------------------------+ | | | | A x | | x | | x | | x | | x o +C | x o | | x o | | x o | | x o | | x o | | x o | | x o | | x o | Y .......................................* P | | o . x | | o . x | | o . x | | o . x | | o . x | | o . x | | o . x | | o . x | | o . x +D | o . | | o . | | o . | B o . | | . | | . | +--------------------------------------.--------------------------+ X sub solve{ my( $a, $b, $c, $d ) = @_; my( $x, $y ); # Some trig :) return( $x, $y ); }

TIA, BrowserUk.

Examine what is said, not who speaks.
"Efficiency is intelligent laziness." -David Dunham
"Think for yourself!" - Abigail
Timing (and a little luck) are everything!

Replies are listed 'Best First'.
Re: A little schoolboy math (but my schooldays were a long time ago).
by Roger (Parson) on Feb 01, 2004 at 13:41 UTC
    sub solve{ my( $a, $b, $c, $d ) = @_; my( $x, $y ); $x = ($b - $a) / ($d - $a - $c + $b); $y = ($d - $a) * ($b - $a) / ($d - $a - $c + $b) + $a; return( $x, $y ); }

    Ok, perhaps more efficiently...
    sub solve{ my( $a, $b, $c, $d ) = @_; my $x = ($b - $a) / ($d - $a - $c + $b); my $y = ($d - $a) * $x + $a; return( $x, $y ); }

    formula for line 1: Y = (D - A) * X + A formula for line 2: Y = (C - B) * X + B intersection: X and Y coordinates of line1 and line2 are equal ... (D - A) * X + A = (C - B) * X + B reduces to => X = (B - A) / (D - A - C + B) plug X into equation for line 1 gives ... Y = (D - A) * (B - A) / (D - A - C + B) + A
      I spend my free time sailing. And despite the fact that GPS's make navigation push-n-click simple, I took an interest in reading through Dutton's Piloting and Navigation cover to cover a couple years ago. I read the 1968 edition, as it came out before GPS, and devotes most of its pages to either Dead Reckoning (deductive reckoning), and Celestial Navigation. BrowserUK's question ignited a lightbulb for me and I flipped open Duttons again to see.

      The short answer is that Roger's answer is accurate. And in fact it employs techniques similar to those used in Dead Reckoning. With Dead Reckoning, one takes a bearing to two known points, and triangulates a position. The cool thing about Dead Reckoning is that if you know your direction and rate of travel, you can take a bearing to only one object of known position, twice at a known interval. Thus, if you get a bearing of 45deg at 10:00am, and 70deg at 10:15am, and you know how far you travelled in that time, and in what direction, you can use your previous position as one of the points of the triangle, the known object as one point, and your current position as one point. It's called a running fix.

      Sorry for delving into this tangent (a pun?) but the subject just kind of got me thinking again. If this followup has a point it's to confirm Roger's methodology.

      Back to your regular scheduled programming, now in progress.


Re: A little schoolboy math (but my schooldays were a long time ago).
by Zaxo (Archbishop) on Feb 01, 2004 at 13:58 UTC

    No trig needed, this is linear algebra. The two lines have the form y = mAx + bA and y = mBx + bB. The point where both are satisfied is the intersection you want.

    Solving for x, we get x = (bB - bA)/(mA - mB). Similarly, y = (mBbA - mAbB) / (mB - mA).

    That is with the numeric values

    1. mA = (Dy - A) / Dx
    2. mB = (Cy - B) / Cx
    3. bA = A
    4. bB = B
    I hope I haven't made a mess of this :) It will simplify things a bit that Dx and Cx are equal.

    After Compline,

Re: A little schoolboy math (but my schooldays were a long time ago).
by kal (Hermit) on Feb 01, 2004 at 15:43 UTC

    The other answers seem right, so I will just pedant the point a little bit :)

    Can you assume that the solution of P is a single point? If not (for example, A=B=C=D=0.5), your solution might be a line. Your calculation may fail with a divide by zero.

    Can you assume that the lines definitely do cross? If they don't, at best you will get an answer of (x>1). At worst, they will be parallel lines and you'll get some lovely division by zero error (depending how you calculate the answer).

    Can you assume that both lines intersect the same two sides? If not, y=mx+c based calculations will fall apart somewhat with vertical lines, but then I don't think any of the answers given here will hold anyway.

      Another gotcha is that the lines might not be parallel, but their intersection might fall outside the unit square. Beware :-) Haroldo
        I had that already if you look closely ;o)
Re: A little schoolboy math (but my schooldays were a long time ago).
by Anonymous Monk on Feb 01, 2004 at 19:42 UTC
    I just love the artwork. BrowserUK, how many times did you have to hit the Preview button?
Re: A little schoolboy math (but my schooldays were a long time ago).
by flyingmoose (Priest) on Feb 02, 2004 at 18:29 UTC
    Anybody care to guess how ugly the 'trig' solution is? Also, BrowserUK, why did you decide to go the trig route? Are you (as guessed below) coming from a navigator-type background? Most of us, as you will see, attacked this using basic algebra. Not that there is anything wrong with that :)

    I will say, though, I do tend to make problems too hard myself. for instance, the US S.A.T. (that easy test was long long long ago) isn't supposed to require Calculus -- only basic Trig, but I used a few Integrals in random places just because. It's whatever tools that are sharpest that get used, I guess. My trig is kind of rusty. When I try to write OpenGL every now and then, I end up with some very weird physics :)


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://325693]
Approved by Roger
Front-paged by gellyfish
and dust plays in a shaft of sunlight...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2018-05-25 02:27 GMT
Find Nodes?
    Voting Booth?