Check if intersecting within a 1 "block" radius - c#

I have two positions on a 3D system, say [15, 32, 42] and [16, 32, 42]
Is there a easy way to check if they are within a 1 block radius from each other?
This is what I have, but is there a better way of doing it:
if (pos[0] == pos1[0] / 32 || pos[0] == pos1[0] + 1 || pos[0] == pos1[0] - 1)
{
if (pos[1] == pos1[1] || pos[1] == pos1[1] - 1 || pos[1] == pos1[1] + 1)
{
if (pos[2] == pos1[2] || pos[2] == pos1[2] + 1 || pos[2] == pos1[2] - 1)
{
Thanks,
David

You can use Math.abs(pos[0]-pos1[0]) <= 1 to check if two coordinates in the same plane are at most 1 apart.
So all in all, your code could look like this:
if( Math.abs(pos[0]-pos1[0]) <= 1
&& Math.abs(pos[1]-pos1[1]) <= 1
&& Math.abs(pos[2]-pos1[2]) <= 1 )
{
Within a 1 block radius
}
Note that I do not understand why you divided your first equation by 32. I did not include that in this answer.
Note also that this solution makes things a little more readable, but that yours is correct too.

I haven't done this in c# but in Java I use JTS. http://geoapi.codeplex.com/ seems to provice the same functionality in c#. Then you will represent your points as Point objects and have all sorts of useful geospatial functions to use.
But for this case, are you looking for the "as the crow flies" distance, which is just pythagoras, or the "walking distance", which would involve finding the shortest valid route in a directed graph of footpaths?
Julian

Related

IF function with && and || at the same time

I want to do IF under serveral condition AND && and OR || on it
i have two range of number
Top1 - Bottom1 and
Top2 - Bottom2
and only executed when Currentnumber is within these two range
am i doing it okay ?
if (OnlyAbove == True
&& (Top1 > Currentnumber || Top2 > Currentnumber)
&& (Bottom1 < Currentnumber || Bottom2 < Currentnumber)
)
{
Statement
}
I'm going to go out on a limb and say: no, you're not doing right on this one - mostly because what your code is currently doing doesn't make sense.
One thing that helps out with stuff like this is to phrase it in common english - so you go from:
if (OnlyAbove == True
&& (Top1 > Currentnumber || Top2 > Currentnumber)
&& (Bottom1 < Currentnumber || Bottom2 < Currentnumber)
)
{
Statement
}
... to ...
if OnlyAbove
AND (CurrentNumber above at least one of the two Tops)
AND (CurrentNumber below at least one of the bottoms)
After all, the stuff between those inner parenthesis is saying "Top1 > current OR Top2 > current" - which really isn't what you're looking for.
Something that'll help out - not only writing code like this, but reading it down the line, is to go a bit overboard with variables. Variables are awesome at helping you document logic, since you can name them very descriptively.
So imagine code that looks like this:
bool betweenRange1 = (Currentnumber > Bottom1) && (Currentnumber < Top1);
bool betweenRange2 = (Currentnumber > Bottom2) && (Currentnumber < Top2);
if (OnlyAbove && betweenRange1 && betweenRange2)
{
Statement
}
... that IF statement starts to look a lot more readable - it's obvious at a glance at what it's getting at. If you're having trouble parsing IF statements, it's not a bad habit to get into. Maybe a bit overkill... but overkill's a lot better than having code you're not sure of what it's doing :-)

Making an int be multiple values?

Is there any way to have a int value that is multiple values?
Under here you can see the code, im doing a minigame as a proof of concept.
currently i have x happen at loop number 4 and y happens at loop number 9.
Is there any way to repeat theese actions on other loops wihout maing it a huge mess like this: i == 4||i == 14 || i == 18
what im trying to ask is if its possible to just write i == a
and then have a be multiple values.
If you havent figured already, im quite the beginner at C#, so if you can explain it in a easy to understand way, i would be very thankfull.
If what i wrote is a bit hard to understand, then im sorry for not being a native english speaker, just ask if you're unsure of what i mean.
for (int i = 0; i < 50; i++)
{
string input;
input = Console.ReadKey().Key.ToString();
Console.Clear();
if (input == "A") Animation.frame1();
else Animation.fall();
if (i == 4) Animation.blocklow2();
if (i == 9) Animation.blockhigh2();
input = Console.ReadKey().Key.ToString();
Console.Clear();
if (input == "W" && i == 4) Animation.blockjump();
if (input == "S" && i == 9) Animation.blockduck();
if (input == "D" && i != 4 && i != 9) Animation.frame2();
else if (input != "W" && i == 4) Animation.fall();
else if (input != "S" && i == 9) Animation.smack();
if (i == 3) Animation.blocklow();
if (i == 8) Animation.blockhigh();
}
You could do if (new int[] { 4, 14, 18 }.Contains(i)) to check for multiple values.
Store a list of ints that you want to check against and then use .Contains
List<int> frame2ints = new List<int>{4,9};
if (input == "D" && !frame2ints.Contains(i)) Animation.frame2();
In certain situations you can perform bitwise tests to see whether some bits are set in your number:
if ((x & 4) != 0)
{
// This case covers numbers
// 4, 5, 6, 7, 12, 13, 14, and many others...
}
General answer to your question is NO, and it only makes sense to use additional knowledge about the values you are testing and then use that knowledge.
Another option is to test whether a number belongs to a sequence, like HHLV said in other answer.
So, how did you get to values 4, 14 and 18?

Or in IF statement not working properly

I have this Or condition in an IF statement (in a foreach loop) in a Windows Form C# program:
if ((splittedFile.Count() != 3) || (splittedFile.Count() != 4))
continue;
and it always does continue, even if splittedFile.Count() is 3 or 4.
The thing is that if I remove the Or condition:
if ((splittedFile.Count() != 4))
continue;
it works properly!! Any ideas why?
This is the correct behavior, you need to use &&.
The reason is that the count is fixed number, let's say n. Now the condition reads:
n is not 3 or n is not 4.
Given n is 4, this means it is not 3, thus the test succeeds and vice versa.
A compiler can't detect this is trivially true, because between the two if statements, the Count() might change (for instance in a multithreading setting where the second thread would add/remove something to/from the collection). I agree however some analysis tools could be capable in some conditions to detect such trivial behavior. In general however such analysis can't be implemented because of Rice's theorem.
If you use &&, the expression reads:
n is not 3 and n is not 4.
Thus both conditions should be true. In other words only if n is less than three and greater than 4, the condition holds.
Try:
if ((splittedFile.Count() != 3) && (splittedFile.Count() != 4))
continue;
I know || sound logical because : if splittedFile.count is not 3 OR it is not 4 then continue; But because there are 2 NOT ! operators in the if expression an AND && is needed.
put a real number into your expression:
if( 3 != 3 || 3 != 4)
which is
if( false || true )
your expression will always be true, as splittedFile.Count() is always (not 3) or (not 4)
you want to && your results together, which looks like
(x != 3) || (x != 4)
or
!( x == 3 || x == 4)
Because one of your expressions will be always true. That's why true || something always returns true.
Let's analyze your splittedFile.Count() is 3, 4 and other than these values.
For 3, your expression will be false || true and this returns true.
For 4, your expression will be true || false and this returns true.
For other than 3 or 4, your expression will be true || true and this returns true.
Strongly suspect you are looking for && operator which provides logical-AND.

More efficient boolean algebra for conditions comparing wild cards

In casino slot games you often have a Wild game piece. What would be a good way of including this mechanic into comparing with 2 other pieces? e.g. given 3 game pieces [Cherry][Cherry][Joker] would be a match.
The code I'm using right now seems really overweight, is there anything that can be done (think bitwise operators?) to make it easier to work with?
if ((box1.BoxRank == box2.BoxRank ||
box1.BoxRank == BoxGameObject.Ranks.Joker ||
box2.BoxRank == BoxGameObject.Ranks.Joker) &&
(box1.BoxRank == box3.BoxRank ||
box1.BoxRank == BoxGameObject.Ranks.Joker ||
box3.BoxRank == BoxGameObject.Ranks.Joker) &&
(box2.BoxRank == box3.BoxRank ||
box2.BoxRank == BoxGameObject.Ranks.Joker ||
box3.BoxRank == BoxGameObject.Ranks.Joker))
{
// has 3 of a kind, or 1 joker and 2 of a kind, or 2 jokers and 1 other
return true;
}
This is easier if you think of the operation in terms of the set of the value of all of the boxes. Just remove all of the jokers from that set and then verify that all of the values are identical:
var allRanks = new[]{box1.BoxRank, box2.BoxRank, box3.BoxRank};
var threeOfAKind = allRanks.Where(rank => rank != BoxGameObject.Ranks.Joker)
.Distinct()
.Count() < 2;
First just remove all of the jokers. If, after doing that, there are two or more distinct values then we do not have a three of a kind.
Yes, represent the Joker as an int with all binary 1's, like 7, 15 or 31.
Represent the cherry and others with an int with only singe binary 1 like 1,2,4,8 smaller than the Joker. Leave zero unused.
Then, using bitwise AND your condition is equivalent to:
(box1.BoxRank & box2.BoxRank & box3.BoxRank) > 0
Note that 3 Jokers will satisfy the condition too.

Array.copy two lambdas, difficult with using the second in linq

i have big problem with explanation code under, meanwhile i made two loops function which does the same thing. I send my code to friend to tell me if it is possible to make is simplier :) So i get something like that.
Array.Copy(
myImageData
.Select(
(b, index) =>
(
index > rooflimit && index < floorlimit && b == 252 &&
(myImageData[index + width] == 0 || (myImageData[index + width] > 168 && myImageData[index + width] < 173)) &&
myImageData[index - width] == 252 &&
myImageData[index - (2 * width)] == 159
) ? (byte)172 : b
).ToArray(),
rooflimit + 1,
myImageData,
rooflimit + 1,
floorlimit - rooflimit - 1
);
My loops was doing something like that (above do the same thing):
when you get all pixelse, copy them to array of bytes
find all pixels which have value 255, pixel under has 0 or it is from range 168-173
pixel above has value 255 and pixel 2 times above has value 159
if i found that pixel change that value to 172
* pixel checking starts from the second row [0][1][2], and finish before last row, to be able check pixels above and under of current pixel *
I get almost about that code above, however i don't understand that part which starts with:
rooflimit + 1,
myImageData,
rooflimit + 1,
floorlimit - rooflimit - 1);
So i ask you for a help, thanks!
PS. please change topic if it is not good specified.
The last four parameters are the last parameters to Array.Copy. Your code would be clearer if you split it up:
byte[] tmp = myImageData.Select([... big lambda expression ...])
.ToArray();
Array.Copy(tmp, rooflimit + 1,
myImageData, rooflimit + 1,
floorlimit - rooflimit - 1);
I would also be very tempted to use a separate method instead of a lambda expression here - it's too complicated to be readable, really.
I think it's better see MSDN Array.Copy for this:
Copies a range of elements from an Array starting at the specified
source index and pastes them to another Array starting at the
specified destination index. The length and the indexes are specified
as 32-bit integers.

Categories