Quantcast
Channel: Questions in topic: "loops"
Viewing all articles
Browse latest Browse all 184

trouble with loops - the impossible is happening

$
0
0
Every now and then I come across some problem like this that is just.. completely intractable. I have no explanation for it. I feel like there can be no explanation for it, like it can't be happening. Right now I have the following code: /// /// Determines if the given point is inside the polygon /// /// the vertices of polygon /// the given point /// true if the point is inside the polygon; otherwise, false public static bool IsPointInPolygon(Vector2[] polygon, Vector2 testPoint) { bool result = false; try { Debug.Log(string.Format("TestPoint: {0},{1} polygon.Length: {2}", testPoint.x, testPoint.y, polygon.Length)); Debug.Log("check a"); int j = polygon.Length - 1; Debug.Log("check b"); Debug.Log(string.Format(" j: {0}", j)); for (int i = 0; i < polygon.Length; i++) { Debug.Log(" Inside loop"); Debug.Log(" boundary point: " + string.Format("{0},{1} ", polygon[i].x, polygon[i].y)); if (polygon[i].y <= testPoint.y && polygon[j].y >= testPoint.y || polygon[j].y <= testPoint.y && polygon[i].y >= testPoint.y) { if (polygon[i].x + (testPoint.y - polygon[i].y) / (polygon[j].y - polygon[i].y) * (polygon[j].x - polygon[i].x) <= testPoint.x) { result = !result; } } j = i; } Debug.Log("Result: " + string.Format("{0}", result)); } catch (Exception exc){ Debug.Log("Exception: " + string.Format("{0} {1}", exc.Message, exc.StackTrace)); throw exc; } return result; } In my log, I get the TestPoint message twice, with nothing intervening (except the UnityEngine.Debug:Log message that comes with every log message), and the TestPoint message says the polygon length is 4. So I should get the "Inside loop" message. For sure. There's no possible way that I shouldn't. But I don't. It's as though there were a 2nd thread running, and the 2nd thread is entering that function just after the main thread is or something. But.. I'm not running multiple threads or anything asynchronous, and there's no other evidence of any 2nd thread or anything asynchronous going on. Ah, I've been pounding my head against this wall for days, now. Usually when I come across some intractable problem like this, I just throw out that part of the code and start over. But in this case.. I don't have any other way I would write it! I would just rewrite it the same way it is written! I might as copy and paste it. Ah, so infuriating. It's like I've been cursed or something. Some supernatural barrier is in my way. "You will not complete this project!" God has spoken. I know there's a potential issue with the >=.. I've rounded the values to the nearest 2nd decimal, and I'm trying to get the algorithm to include the corner points. Which seems to be working except that it's not, for some of them, because it doesn't enter the loop at all. Even though the boundary length is 4. @#$ #$% &*@%. (btw, looking at the preview, I guess you have to scroll right a bit to see the part of the log message code I'm talking about that says what the boundary length is.) I'm completely blocked. I can't move forward on this project, because of this unexplainable problem. And, I mean, it feels like I can't move forward with Unity in general - how can I write any code if I can't have confidence that.. the fundamental principles of coding hold. Oh, and the try/except code was put in because.. I had some code in there that was in error, and should've caused an error in the log, and from the log messages appeared to be causing the function to exit prematurely, but no errors were showing up in the log. It's like.. Unity is generating some spurious code for me or something. Some code that is causing my functions to exit prematurely under some very mysterious conditions. Running out of memory? But there's no other evidence of that, the resource monitoring tools aren't showing that. Some problem with my computer's memory? Some kind of processor exception issue? I'm grasping at straws, I don't understand it at all. But I've confirmed that a deliberate run time error does trigger that exception log message, so. /aarrgh. Maybe it's some problem with MonoDevelop under Windows. I guess I'll install Visual Studio Express, and try the unity plugin for Visual Studio. I've resisted doing that, but God is forcing me to do it, lol. "The aliens have landed and we'll tell you where they are! Midnight Star.."

Viewing all articles
Browse latest Browse all 184

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>