Calculate the grid passing by a straight line in a grid

Source: Internet
Author: User

For more information about the algorithm used to draw a straight line on a In-Place graph, see http://free.pages.at/easyfilter/bresenham.html. However, unlike creating a straight line on the in-place graph, you need to determine the grids through which a straight line goes.

  1. Assume that there are P0 and P1 points, and the position is as follows:
  2. We can easily obtain the linear equation. First, we take an integer increment on X, and it is easy to calculate x = 1, 2, 3, 4... The value of Y.
  3. Then, the values of Y are rounded up to get (x1, Y1), (X2, Y2), (X3, Y3 )... It must be a grid that has been connected.
  4. However, we have omitted this situation. The upper left lattice is also the grid that goes through the link. However, since we only perform the integer-incrementing operation on the X axis, this is not taken into account.
  5. At (2), we can know the coordinate of point A. We can use the slope to determine whether the line passes through the above grid. If "the slope of the blue line is less than the slope of the p0-p1", it indicates that the line goes through the above grid.
  6. Using this method, you can easily calculate all the grids passing through the line.

Demo of as3 implementation:

Source code:


/**
* Returns the two vertices in the grid, and the grids passing through the line.
* @ See http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
*/
Public static function determinetouchedtiles (P0: Point, P1: Point): vector.
{
VaR touched: vector. = new vector .();

var x0:Number=p0.x;
var y0:Number=p0.y;
var x1:Number=p1.x;
var y1:Number=p1.y;

VaR steep: Boolean = math. Abs (Y1-y0)> math. Abs (x1-X0 );
If (steep)
{
X0 = policy;
Y0 = running X;
X1 = p1.y;
Y1 = p1.x;
}

If (x0> X1)
{
VaR x0_old: Number = x0;
VaR y0_old: Number = y0;
X0 = x1;
X1 = x0_old;
Y0 = Y1;
Y1 = y0_old;
}

VaR ratio: Number = math. Abs (Y1-y0)/(x1-X0 ));
VaR mirror: Int = Y1> y0? 1:-1;

For (VAR Col: Int = math. Floor (x0); Col <math. Ceil (X1); Col ++)
{
VaR curry: Number = y0 + mirror * Ratio * (Col-X0 );

// The first grid is not used for Yanbian Calculation
VaR SKIP: Boolean = false;
If (COL = math. Floor (x0 )){
Skip = (INT (curry )! = Int (y0 ));
}

If (! Skip ){
If (! Steep)
{
Touched. Push (new point (COL, math. Floor (curry )));
}
Else
{
Touched. Push (new point (math. Floor (curry), col ));
}
}

// Calculate whether there is a cross grid based on the slope.
If (mirror> 0? (Math. Ceil (curry)-curry): (curry-math. Floor (curry) <ratio)
{
VaR crossy: Int = math. Floor (curry) + mirror;

// Determine if the data exceeds the specified range
If (crossy> math. Max (INT (y0), INT (Y1) | crossy

// Cross-line grid
If (! Steep)
{
Touched. Push (new point (COL, crossy ));
}
Else
{
Touched. Push (new point (crossy, col ));
}
}
}

return touched;

}

Http://www.itamt.com/2011/04/line-touched-tile/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.