Jump to content

I am working on a program and am in need of some assistance. What I am trying to do is essentially take two locations, draw a straight line between them, and color any part of a grid that the line touches. The problem is that I can't properly figure out how to do diagonals. I had it working for a straight line, but if the two locations were diagonal of each other I would just end up with a filled in rectangle. I can't share that code because it was modified by another guy working with me and now it is basically broken. I made a very crude drawing in paint to try and illustrate what I am trying to do.

post-262337-0-79051200-1454036422.png

Link to comment
https://linustechtips.com/topic/536867-java-help/
Share on other sites

Link to post
Share on other sites

Explain what you are trying to do?

Ok, so there are two points in the world. I need to take their coordinates and change the color of any blocks that fall in a straight line between them. As I said above, I was able to do a straight line and a big rectangle by using nested for loops. However, I am having trouble figuring out what to do if the line would be diagonal.

Link to comment
https://linustechtips.com/topic/536867-java-help/#findComment-7120715
Share on other sites

Link to post
Share on other sites

Ok, so there are two points in the world. I need to take their coordinates and change the color of any blocks that fall in a straight line between them. As I said above, I was able to do a straight line and a big rectangle by using nested for loops. However, I am having trouble figuring out what to do if the line would be diagonal.

Well, I have never been on that level, but getting their coordinates, looping them while checking it the last block was 1-2 blocks away and then you add the selected block from checks into an array

Link to comment
https://linustechtips.com/topic/536867-java-help/#findComment-7120743
Share on other sites

Link to post
Share on other sites

Well, I have never been on that level, but getting their coordinates, looping them while checking it the last block was 1-2 blocks away and then you add the selected block from checks into an array

Wouldn't the number of blocks skipped not be constant depending on the diagonal? For example a straight line wouldn't skip any, but one where the second position is one over and 8 above would be skip one 3 times then skip 2 then skip one 3 more times.

Link to comment
https://linustechtips.com/topic/536867-java-help/#findComment-7120798
Share on other sites

Link to post
Share on other sites

Wouldn't the number of blocks skipped not be constant depending on the diagonal? For example a straight line wouldn't skip any, but one where the second position is one over and 8 above would be skip one 3 times then skip 2 then skip one 3 more times.

That is why you find the closest 1-2 blocks so it doesn't skip. I can't try this since I stopped coding for a while. It's just a theory. Getting 3D Area using the coords then skipping it vertically/horizonally while keeping the 1-2 block range from last block until you have your diagonal line

Link to comment
https://linustechtips.com/topic/536867-java-help/#findComment-7120823
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×