- Home /
Accidental breach of apparent rules.
Check distance
How can I iterate through a Vector3 array, and for every Vector3, check to see whether there is a neighbouring one?
So for example if my array is (0,0,0), how can I check if (1,0,0) is there?
By neighbouring, I mean if they were 1,1,1 blocks, they would be touching. So an increase in 1 co-ordinate, but not the others.
To check, I'm using this code. However, I think there is a mistake somewhere, since the if statement does not return true for blocks on there own.
function march(){
var px = false;
var nx = false;
var py = false;
var ny = false;
var pz = false;
var nz = false;
// for (var xvalue : int in xcoords) {
for(var i : Vector3 in blocks)
{
// no need to compare twice agains smaller indices, so start at i+1
for(var j : Vector3 in blocks) {
if(j.x-i.x == -1){
nx= true;
}
if(j.x-i.x == 1){
px = true;
}
if(j.y-i.y == -1){
ny = true;
}
if(j.y-i.y == 1){
nx = true;
}
if(j.z-i.z == -1){
nz = true;
}
if(j.z-i.z == 1){
pz = true;
}
if(nx == false && px == false && ny == false && py == false && nz == false && pz == false){
print ("i is " + i + "j is " + j);
}
}
}
}
-1. :( This is a shameless copy-paste duplicate question from this one:
http://answers.unity3d.com/questions/186662/iterate-throgu-han-array.html
And you didn't upvote any of the answers provided there, or even participate in the discussion. :(
It's not innocent to repost your own questions just because you don't feel like you got a sufficient answer last time around. Ins$$anonymous$$d, you need to return to your previous question and participate in the discussion provided there, and maybe reward those who tried to help you for a change.
There were some interesting solutions on the previous thread... no reason to start a new one, think about how to solve the problem with using the information provided by the community and discussing it.
Oops! I didn't know that and posted one more answer...
Answer by aldonaletto · Nov 22, 2011 at 10:52 AM
Comparing equality between floats is a treacherous thing: it only returns true in some special cases. If you really need to compare to 1, use Mathf.Approximately instead:
for(var i : Vector3 in blocks){ // no need to compare twice agains smaller indices, so start at i+1 for(var j : Vector3 in blocks) { var dif = j - i; nx = Mathf.Approximately(dif.x, -1); px = Mathf.Approximately(dif.x, 1); ny = Mathf.Approximately(dif.y, -1); py = Mathf.Approximately(dif.y, 1); nz = Mathf.Approximately(dif.z, -1); pz = Mathf.Approximately(dif.z, 1); if(!nx && !px && !ny && !py && !nz && !pz){ print ("i is " + i + "j is " + j); } } }
Interestingly, your reply has the same comment in it just before the inner loop as Wolfram's does in the other thread the OP copied from. Today is copy-paste day, and no one told me? ;)
I edited the question text to create my answer, and the comment just was there. But that's a curious thing: this comment was already in the original question, but only makes sense in Wolfram's answer.
Ah, I see. :) Yeah, you're right. I don't think the OP considered what that comment was for when he grabbed Wolfram's answer back then.