Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations MikeeOK on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Identify which cells a line travels through

Status
Not open for further replies.

Griffyn

Programmer
Jul 11, 2002
1,077
AU
Hi all,

I asked this same question a month back or so, but my use has expanded and the work-arounds suggested in the previous thread don't apply any more.

Imagine a grid of, say, 10 x 10 = 100 cells. Pick any two cells (a and b) and draw a line between them. I need an algorithm that will tell me not only which cells the line travels through, but also what proportion of each cell the line divides it up into (ie. if the line went from a corner to the opposite corner, the proportion would be 50/50, whereas if the line 'cut' off a small corner of the cell, it would be more like 95/5).

I imagine solving this by writing a function that handles an individual cell and calling it repeatedly with it processing further and further along the line each time until the cell containing point b is reached. The function would be told the entry point of the line (at the cell edge) and the slope of the line, and having the function calculate the exit point of the line (at the cell edge). This is where I'm stumped. Presuming this problem is solved, I can the find the hypotenuse and the area of the triangle created and therefore find the proportion of the triangle to the rest of the cell.

I'll be using this function to determine line-of-sight between two entities in the grid.

Thanks in advance.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top