// This attaches a linedef and returns the listitem internal LinkedListNode <Linedef> AttachLinedefP(Linedef l) { return(linedefs.AddLast(l)); }
// This puts a single linedef in all blocks it crosses public virtual void AddLinedef(Linedef line) { Vector2D v1, v2; float deltax, deltay; float posx, posy; Point pos, end; int dirx, diry; // Get coordinates v1 = line.Start.Position; v2 = line.End.Position; // Find start and end block pos = GetBlockCoordinates(v1); end = GetBlockCoordinates(v2); v1 -= rangelefttop; v2 -= rangelefttop; // Horizontal straight line? if (pos.Y == end.Y) { // Simple loop pos.X = CropToRangeX(pos.X); end.X = CropToRangeX(end.X); if (IsInRange(new Point(pos.X, pos.Y))) { dirx = Math.Sign(v2.x - v1.x); if (dirx != 0) { for (int x = pos.X; x != end.X; x += dirx) { blockmap[x, pos.Y].Lines.Add(line); } } blockmap[end.X, end.Y].Lines.Add(line); } } // Vertical straight line? else if (pos.X == end.X) { // Simple loop pos.Y = CropToRangeY(pos.Y); end.Y = CropToRangeY(end.Y); if (IsInRange(new Point(pos.X, pos.Y))) { diry = Math.Sign(v2.y - v1.y); if (diry != 0) { for (int y = pos.Y; y != end.Y; y += diry) { blockmap[pos.X, y].Lines.Add(line); } } blockmap[end.X, end.Y].Lines.Add(line); } } else { // Add lines to this block if (IsInRange(pos)) { blockmap[pos.X, pos.Y].Lines.Add(line); } // Moving outside the block? if (pos != end) { // Calculate current block edges float cl = pos.X * blocksize; float cr = (pos.X + 1) * blocksize; float ct = pos.Y * blocksize; float cb = (pos.Y + 1) * blocksize; // Line directions dirx = Math.Sign(v2.x - v1.x); diry = Math.Sign(v2.y - v1.y); // Calculate offset and delta movement over x if (dirx == 0) { posx = float.MaxValue; deltax = float.MaxValue; } else if (dirx > 0) { posx = (cr - v1.x) / (v2.x - v1.x); deltax = blocksize / (v2.x - v1.x); } else { // Calculate offset and delta movement over x posx = (v1.x - cl) / (v1.x - v2.x); deltax = blocksize / (v1.x - v2.x); } // Calculate offset and delta movement over y if (diry == 0) { posy = float.MaxValue; deltay = float.MaxValue; } else if (diry > 0) { posy = (cb - v1.y) / (v2.y - v1.y); deltay = blocksize / (v2.y - v1.y); } else { posy = (v1.y - ct) / (v1.y - v2.y); deltay = blocksize / (v1.y - v2.y); } // Continue while not reached the end while (pos != end) { // Check in which direction to move if (posx < posy) { // Move horizontally posx += deltax; if (pos.X != end.X) { pos.X += dirx; } } else { // Move vertically posy += deltay; if (pos.Y != end.Y) { pos.Y += diry; } } // Add lines to this block if (IsInRange(pos)) { blockmap[pos.X, pos.Y].Lines.Add(line); } } } } }