It looks like you're using an Ad Blocker.

Thank you.

Some features of ATS will be disabled while you continue to use an ad-blocker.

Need 4 Integers That Give Unique Totals

page: 2
0
share:

posted on Feb, 23 2017 @ 03:04 PM

But explaining Mr Boole's maths is easy, working it out so its that logical is a problem.

posted on Feb, 23 2017 @ 06:54 PM

You could do it with ridiculous magnitudes or very large integers (so answers could never overlap), but primes are probably the first thing that comes to mind as an easy solution.

posted on Feb, 23 2017 @ 08:26 PM

The OP really didn't explain much about what his is doing. The problem might sound less complicated than it is.

edit:

Seems like using bits is a good solution. Efficient and a common practice.

edit on 2/23/2017 by roadgravel because: (no reason given)

posted on Feb, 23 2017 @ 08:59 PM

If you are using this for a software I would probably do something like this:

Use 4 bits for a piece
North 0001
East 0011
South 0111
West 1111

So every joint would be a full byte (8 bits), for example:

North Joint : South of another piece
Piece 1 + Piece 2
0001 0111 = 16 + 4 + 2 + 1 = 23

East Joint : West of another piece
Piece 1 + Piece 2
0011 1111 = 32 + 16 + 8 + 4 + 2 + 1 = 63

South Joint : North of another piece
Piece 1 + Piece 2
0111 0001 = 64 + 32 +16 + 1 = 113

West Joint : East of another piece
Piece 1 + Piece 2
1111 0011 = 128 + 64 + 32 + 16 + 2 + 1 = 243

Something like (ATS editor mess up the code):

class Map
[
enum Cardinal [ North, East, South, West ];

static void Main(String[] args)
[
byte[,] piece = new byte[100,4]; // Assuming a 100 pieces map

piece[0, (int) Cardinal.North] = 23;
piece[0, (int) Cardinal.East] = 63;
piece[0, (int) Cardinal.South] = 113;
piece[0, (int) Cardinal.West] = 243;

Console.Out.WriteLine(piece[0, (int)Cardinal.North]);
Console.Out.WriteLine(piece[0, (int)Cardinal.East]);
Console.Out.WriteLine(piece[0, (int)Cardinal.South]);
Console.Out.WriteLine(piece[0, (int)Cardinal.West]);

]
]

edit on 23-2-2017 by kazike because: (no reason given)

edit on 23-2-2017 by kazike because: (no reason given)

edit on 23-2-2017 by kazike because: (no reason given)

posted on Feb, 23 2017 @ 09:40 PM

I would think only 4, 4 bit maps or one 16 bit map is need for an intersection.

For instance, the N direction has bits set for the intersection to the N. No bits at current N means no road to the N. The N at the current intersection is either a separate 4 bit map or the first 4 bits of a 16 bit map. E could be the 2nd map or the 2nd group of 4 bits, etc.

That would allow for a process to know what directions can the taken once the new intersection is reached.

posted on Feb, 23 2017 @ 10:11 PM

originally posted by: DeReK DaRkLy
This is not for a school project or anything important, I'm just an amateur video game hobbyist. So here's the deal...

I'm trying to design a top-down view of a city (think Grand Theft Auto 1) and I want to connect road pieces based on randomly generated road maps.

I only need the roads to connect at right angles, so they will only be going in the 4 cardinal directions.

In order for my program to know which piece (curves, straights, intersections, etc) to attach to its neighbor, I need to assign one of 4 identifying numbers - one for a neighboring piece of road to the North, one for South, one for East and one for West.

However, in order for this to work, no 2 totals (ranging fom 2 to four digits) can repeat against another total.

For example 10, 20, 30 and 40 don't work because 10+40 as well as 20+30 both equal 50.

So what are 4 magical digits that won't give repeating totals?
Any takers? =)

Haven't read the rest of this thread yet so someone may have already given you a solution. I'm a game dev myself.

I would suggest thinking about this in terms of number bases. In base 10 you could accomplish your task with 1, 10, 100, and 1000, just shift your numbers by the base. In base 2 you can do this with 1, 2, 4, and 8. Or in binary 1111. Each position can represent a direction flag. So if those 4 bits are reserved for north/south/east/west you can make it 1001 for northwest, 1000 for north, 1 for west, or 0110 for southeast for example.

Is that what you're looking for?

Edit: Several people provided the same solution.

I don't know what language you're writing this in, but the general code for it can be tested with a logical AND. There's a few ways to go about this. Lets say you apply the bits to these columns: north=8, east = 4, south=2, west=1. The simplest way would be to convert base 2 to base 10 and use an AND with that value. So if you want to check northeast it would be something along the lines of
if val && 12 == true

This works because 12 is represented in binary as 1100 which is the same as your north and east flags. Alternatively you could test by if val && 0b0110 == true to keep your value in binary or 0xc to specify your value in hex.

With this particular arrangement you could also use bitshifting to rotate. If you want to rotate by 90 degrees you could bitshift left by 1 and then %8 (modulo by 8) to rotate counterclockwise or bitshift right by 1 and then add 8 if < 0. Similarly a rotation by 180 degrees would be a bitshift by 2, or 270 degrees by 3.
edit on 23-2-2017 by Aazadan because: (no reason given)

posted on Feb, 23 2017 @ 11:11 PM
Set a bit with logical OR and the bit value
Check a bit with logical AND and the bit value

edit on 2/23/2017 by roadgravel because: (no reason given)

posted on Feb, 24 2017 @ 12:46 AM
OP

Is the intent to navigate from point to point?

posted on Feb, 24 2017 @ 08:49 AM
Just woke up, rereading the OP I think that what you're looking for is purely graphical? You just want to know which sprite to draw on which tile after the road is created?

If that's the case, the bit flags mentioned in this thread will work. Make your road, then for each piece of your road look to see what other directions have road adjacent to it. Assign those flags to that piece. Then give it a sprite based on those flags.

If you want to add in a feature like navigation, you'll have to build a graph, with each piece of road being a node with branches that go to the next node (the distance on each branch can be 1 in this case). From there you can do something like Dijkstra's or A* (either algorithm is easily found online) to traverse from node to node, and then move your actors along that path.

posted on Feb, 24 2017 @ 09:37 AM
I'll post another idea here again later today.

posted on Feb, 24 2017 @ 09:42 AM
Are you using this to generate coordinates?

posted on Feb, 24 2017 @ 10:23 AM
Since this is a game like GTA 1 (which i've never played but imagine its a top down sort of thing) we need to factor in the cityscape so residential/business/parks/rivers and whatever the else the OP wants in the game.

Its a lot more complex I think than just a few lines of code I suspect.

The greatest tools around quite often are pen and paper (or digital equiv) and with quiet mind and perhaps a few brewskis (not too many) start to sketch out the idea of the project as it'll be the bedrock you always come back to for clarity as only when you know what you want can you do it.

new topics

top topics

0