Help !. Maths wizard needed !

Buzby

Senior Member
Hi All,

Inspired by laserhawk's sub hunter game project, I started to code an engine to do the bearing and range calculations.

The 08M2 is a bit sparse on trigonometry functions , but I have got half of the necessary code working.

FindQ.png

In the diagram the original bearing is P and range is x.
R is the heading and y the distance of the target move.
C is the angle related to bearing and heading, calculated with 90' and 180' relationships from P and R.
z is the new range, calculated from x, y and cosine of C.

The problem now is to calculate the new bearing, P + Q, so I need to find Q.

I could calculate Q with another trig function, maybe sinQ=(y sinC)/z, but this needs both a sin and inverse sin table, and I've no room for two.

Are there any maths wizards out there who could tell me an easier way, or am I barking up completely the wrong tree doing it this way ?

Cheers,

Buzby

EDIT : Don't say use an X2, that's too easy !

View attachment SubHunter_1.bas
 
Last edited:

SAborn

Senior Member
I to had pondered the math to do the subhunter location and came up with a much different way, of using a binary matrix grid, where the square was made up of 0 to 15 on X and 0 to 15 on Y giving a 16 x 16 matrix square.

Y would be the LSB (4 bits) and X the MSB (8 bits) making up the full byte, in the same way a LCD font table is calculated.
This would give a decimal value for each location from 0 to 255.

The sub would move up, down, left or right one space for each button press, if it moved up 1 row then subtract 16 from the last location value, if it moves down add 16 to the last value, if it moves left, subtract 1 from the last value, and for a right move add 1 to the last value.

As for the target hit that would be equal to 1 square up,down,left, right of the target location, a location equal to the target would be a collision and both subs would sink.

The program would select a random number from 1 to 255, that would be the target location, you would start at location 0 (zero) and move 1 square at a time.

That was as far as i got and then gave up on the idea.

You might say a bit like playing checkers.
 

Buzby

Senior Member
I did start with a cartesian coordinate system first.

The origin was the hunter, with the target at a range of up to 255 squares from it in both directions.
A bit of maths limited the range to 255 when the diagonal was longer than 255.

Both this idea and the polar coordinate system allow the subs to roam over an infinite sea, but with a limit of 255 for the maximum separation.

The problem with cartesian is that at very close ranges the bearing is limited to X+/-1 and Y+/-1, giving just 8 directions.

There are pros and cons for both.

Cartesian has easy maths but crappy resolution, polar has difficult maths but more realistic simulation.

I'll give polar another bash !.
 

alband

Senior Member
With two provisos I have a solution:

1: you can use your cosine table backwards to get arcos (possibly using a loop where it "searches" for the correct value).
2: you can read my writing!

Capture.JPG

Hope it helps
 

Buzby

Senior Member
Nice diagram btw, what did you use to make it. Almost got fooled into thinking a = b though!
Believe it or not, the diagram was done with MS Excel 2007 !.

The grid is just borders with dotted lines, the lines are just 'lines', the text is in invisible text boxes, and the angles are badly drawn curves.

I'll go through your maths soon, just got to take the dog for a walk.

I do like the reverse scan of the cosine though, might investigate that.

Cheers,

Buzby
 

Buzby

Senior Member
Just checked the maths.

Maybe I didn't make it clear enough in the diagram, I don't know the values of a,b,c and d !.

I only know, or have calculated, angles P, R, and C, and lines x, y, and z.

I'm fairly certain that it's going to need either the 'Law of Sines', or something with an inverse TAN in it.
( This kind of stuff I last looked at when I did either 'O' or 'A' level, both of which seem like a lifetime ago now. Google is my teacher today ! )

Cheers,

Buzby
 

alband

Senior Member
Well I just did A-level :p

Check the bottom of my diagram my scribbles indicate how to calculate a b c and d (or whichever I used) using just the angles and lengths x y and z.
 

Buzby

Senior Member
Sorry, I misread your notes. ( It's very cold here, maybe my brain is like my car in the cold - difficult to start. )

You are quite right, a and b can be calculated with inverse cosines. That looks like a definite solution.

Also, I've got an idea that there may be an algebraic method, with no trig functions.

Try this : Extend x and y into the empty spaces above C, to make an X shape.
We know that the sum of angles around a point is 360', and opposite angles are equal.
We have the values of R and C, and these angles overlap somewhat.
P is also in there, because of the symmetry around the diagonal of a rectangle.
There is now an unknown angle between y and the right horizontal through C, with it's 'opposite' above the left horizontal.

Can this angle be determined by addition and subtraction of the R, C, and P angles ?

Too late for me to try tonight, it will have to wait till the morning.

Cheers,

Buzby
 

srnet

Senior Member
There is an example of the code for this sort of thing in the PONGSAT code I put on the projects area.

The code calculates the direct distance and angle between two points based on the NS and EW distances, i.e the two shorter sides of the right angle triangle.

The full calculation code was around 2000 bytes, but most of that is taken up working out the NS and EW distances and scaling. The Atanx, COSX and DIV32by16 routines used to calculate the angle and direct distance (hypotenuse) were not that large.
 

alband

Senior Member
Not sure I understand your proposed method but with some reasoning, you would never be able to find Q by extending lines x and y and examining the angles around the cross created. Q isn't 'involved' in that cross in anyway. The angle Q is also at the mercy of how long the lines x and y are. You could keep all the angles the same in the cross (thus no change in information there) but if you changed the lengths of the lines x and y, the value of Q would change.

You'd only need to use inverse cos once in the method I proposed. Calculating a and b uses normal cosine.

I don't think there will be a non-trig way of doing this, however you can always fiddle your trig to only use the functions you want.
 

Buzby

Senior Member
Yes, you are quite right. It was too late last night, and I got my angles mixed up !.

The solution is going to involve trig, so I'll try coding your cosine method. As you say, only one trig table needed.
I'm not sure how it will cope with all possible x,y,z length combinations, and when R > 180', but I'll experiment.

It just seems too complicated, not your specific idea, just the whole problem of finding the new bearing.
Navigators sailing across the oceans 200 years ago must have needed to solve problems like this. How did they do it ?.

Cheers,

Buzby
 

alband

Senior Member
Hi,

Had another though, which is that the information given by the angle "R" doesn't really help, it just gives the whole thing an orientation to a particular coordinate system. I've created a slightly simpler solution by rotating the coordinate system used, making R = (180 - C).

srnet has a point though, it might be easier to give yourself more trig functions to play with by writing them yourself. The binomial expansions shouldn't be too hard to replicate on an 08M2. The sine and cosine expansions are easy and will be accurate enough with only a few terms (make a factorial subroutine!). The arctan expansion is even easier, no factorials: https://ccrma.stanford.edu/~jos/pasp/Arctangent_Series_Expansion.html
You'd just have to watch your negatives.

If you had sine, cosine and arctan functions:
Picture1.jpg
 

alband

Senior Member
Yes, you are quite right. It was too late last night, and I got my angles mixed up !.

The solution is going to involve trig, so I'll try coding your cosine method. As you say, only one trig table needed.
I'm not sure how it will cope with all possible x,y,z length combinations, and when R > 180', but I'll experiment.

It just seems too complicated, not your specific idea, just the whole problem of finding the new bearing.
Navigators sailing across the oceans 200 years ago must have needed to solve problems like this. How did they do it ?.

Cheers,

Buzby
In fairness, they didn't have to cope with integer maths, with no negatives! I would imagine they drew it and measured? Remeber anything geometrically you can calculate, you can also draw (and visa versa).
 

BeanieBots

Moderator
Navigators sailing across the oceans 200 years ago must have needed to solve problems like this. How did they do it ?.
We had a book of lookup tables and floating point maths. (typically to four decimal places which equates to about 14-bit digitally)
Also what Alband says about drawing it.
 

PaulRB

Senior Member
Yes, all done by plotting positions and courses on a chart using rulers, dividers, protractors etc. No trig tables or calculators required. Most charts would have lines of latitude & longitude drawn on, and a compass rose showing the bearings and the magnetic deviation if known. If you study basic navigation today, for example if you do day skipper with the RYA, you are taught to do it all this way, in case your GPS equipment breaks down!
 
Last edited:
Top