In this beautiful illustration I made with my vast graphical skills, there are 4 regions (rightly labeled) and a line that passes through 3 of them (regions 2-4).

What is the best way to check which regions the line pass through. Do I have to manually loop through the 4 regions to see if the line lies in its boundaries? Can I take the starting and end point of the line to see which regions it lies in? Is there example code? I'm a bit too tired to think logically.

haha thanks everyone. Great advice Riven sorry for hijacking the line logic thread counterp.p.s. I slept in my car last night Yay for work computer!

counterp, I didn't write these but compare speeds of these 2 methods. 1 is from the internals of the java's rect2d

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

Line2D.Doubleline = newLine2D.Double(20,40,600,400); Ichosethisatrandomnumber//should use a custom linetype to reduce casting for intintrX=20; //how many X rectintrY=20; //how many Y rectintspacing=25; //you can separate this into xSpace and ySpace for non squares

I also could imagine another one, that doesn't need to blunt force your way through ALL the rectangles(as clearly some have 0 chance of ever being in it, but by using the slope calculation, directly hit numbers in simple boolean grid[][]; So you wouldnt need a nested for loop(this should be the fastest) for detection? but a direct grid[pointY/rectHeight] = HIT;

The missing squares of my implementation are due to only sampling when entering a col/row. To match all the squares the code has to be modified to also sample when leaving the col/row. This then can be optimized to only being done when the sampled row/col differs from the last one. Maybe I'll find some time this eve.

Yeah, the float to int casting always rounds down, while the drawLine() rounds more accurate. I simply added a 0.5f bias before casting - that should do the trick. I also fixed an error where the missed cells don't get detected correctly on negative direction:

arghh cylab! hate you! I was thinking last night this morning, it finally came to me to use a slope with a xSpacingStepping or for y and do it that way. I am all excited coming into work, and then boom. You come out of no where and present exactly what I had been all excited to write! arghhh!

java-gaming.org is not responsible for the content posted by its members, including references to external websites,
and other references that may or may not have a relation with our primarily
gaming and game production oriented community.
inquiries and complaints can be sent via email to the info‑account of the
company managing the website of java‑gaming.org