Java-Gaming.org    
Featured games (91)
games approved by the League of Dukes
Games in Showcase (576)
games submitted by our members
Games in WIP (497)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Line -> Grid intersection code (all plotted cells directly connected)  (Read 1539 times)
0 Members and 1 Guest are viewing this topic.
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 605
Projects: 4
Exp: 16 years


Hand over your head.


« Posted 2013-03-14 14:08:15 »

Usage:
1  
2  
3  
4  
5  
6  
7  
int[] xy = new int[...];
int len = LineGridIntersection.plot(xSrc, ySrc, xDest, yDest, TILE_SIZE, TILE_SIZE, xy];
for(int i=0; i<len; i++) {
   int x = xy[i*2+0];
   int y = xy[i*2+1];
   // ...
}


Implementation:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
   public static int plot(int x1, int y1, int x2, int y2, int wCell, int hCell, int[] xy) {
      // support negative input values
     int xOffset = (-Math.min(0, Math.min(x1, x2)) + wCell - 1) / -wCell;
      int yOffset = (-Math.min(0, Math.min(y1, y2)) + hCell - 1) / -hCell;
      x1 -= xOffset * wCell;
      y1 -= yOffset * hCell;
      x2 -= xOffset * wCell;
      y2 -= yOffset * hCell;
      assert (x1 | y1 | x2 | y2) >= 0;

      // ensure x1 <= x2
     if (x1 > x2) {
         int t;

         t = x1;
         x1 = x2;
         x2 = t;

         t = y1;
         y1 = y2;
         y2 = t;
      }

      int p = 0;

      int xCell1 = (int) ((x1 + 0.5f) / wCell);
      int yCell1 = (int) ((y1 + 0.5f) / hCell);
      int xCell2 = (int) ((x2 + 0.5f) / wCell);
      int yCell2 = (int) ((y2 + 0.5f) / hCell);

      int yMin = Math.min(y1, y2);
      int yMax = Math.max(y1, y2);
      int yCellMin = Math.min(yCell1, yCell2);
      int yCellMax = Math.max(yCell1, yCell2);

      if (xCell1 == xCell2) {
         // handle vertical line
        for (int yCell = yCellMin; yCell <= yCellMax; yCell++) {
            xy[p++] = xOffset + xCell1;
            xy[p++] = yOffset + yCell;
         }
      } else if (yCell1 == yCell2) {
         // handle horizontal line
        for (int xCell = xCell1; xCell <= xCell2; xCell++) {
            xy[p++] = xOffset + xCell;
            xy[p++] = yOffset + yCell1;
         }
      } else {
         // handle diagonal line
        int xCell = xCell1;
         int yCell = yCell1;

         // calculate normal of path
        double nx, ny;
         {
            double dx = x2 - x1;
            double dy = y2 - y1;
            double dist = Math.sqrt(dx * dx + dy * dy);
            nx = dx / dist;
            ny = dy / dist;
         }

         final double advancePerCell = wCell / nx;

         double firstStepRatio = (((xCell + 1) * wCell) - (double) x1) / wCell;
         double x = x1 + nx * advancePerCell * firstStepRatio;
         double y = y1 + ny * advancePerCell * firstStepRatio;

         while (true) {
            // handle cases where adjecent cells are also touched
           //  - this happens when we advanced a cell and the Y-cell doesn't have the expected value
           if (ny > 0.0f) {
               for (int yEnd = Math.min((int) y / hCell - 1, yCell2); yCell <= yEnd; yCell++) {
                  xy[p++] = xOffset + xCell;
                  xy[p++] = yOffset + yCell;
               }
            } else {
               for (int yOff = Math.max((int) y / hCell - 0, yCell2); yCell > yOff; yCell--) {
                  xy[p++] = xOffset + xCell;
                  xy[p++] = yOffset + yCell;
               }
            }

            if (x >= x2 || y <= yMin || y >= yMax) {
               break;
            }

            // advance with 1 cell
           x += nx * advancePerCell;
            y += ny * advancePerCell;

            xy[p++] = xOffset + xCell++;
            xy[p++] = yOffset + yCell;
         }

         // handle last cell
        if (p == 0 || xy[p - 2] != xOffset + xCell2 || xy[p - 1] != yOffset + yCell2) {
            xy[p++] = xOffset + xCell2;
            xy[p++] = yOffset + yCell2;
         }
      }

      return p >> 1;
   }

   public static final int plotReferenceImpl(int x1, int y1, int x2, int y2, int wCell, int hCell, int[] xy) {
      // support negative input values
     int xOffset = (-Math.min(0, Math.min(x1, x2)) + wCell - 1) / -wCell;
      int yOffset = (-Math.min(0, Math.min(y1, y2)) + hCell - 1) / -hCell;
      x1 -= xOffset * wCell;
      y1 -= yOffset * hCell;
      x2 -= xOffset * wCell;
      y2 -= yOffset * hCell;
      assert (x1 | y1 | x2 | y2) >= 0;

      int p = 0;

      int dist = Math.max(Math.abs(x2 - x1), Math.abs(y2 - y1));
      if (dist == 0) {
         xy[p++] = xOffset + x1;
         xy[p++] = yOffset + x2;
      } else {
         float dx = (float) (x2 - x1) / dist;
         float dy = (float) (y2 - y1) / dist;
         int lastRegX = (int) (x1 + dx + 0.5f) / wCell;
         int lastRegY = (int) (y1 + dy + 0.5f) / hCell;

         xy[p++] = xOffset + lastRegX;
         xy[p++] = yOffset + lastRegY;

         for (int d = 2; d <= dist; d++) {
            int x = (int) (x1 + 0.5f + dx * d);
            int y = (int) (y1 + 0.5f + dy * d);
            int regX = x / wCell;
            int regY = y / hCell;
            if (regX != lastRegX || regY != lastRegY) {
               xy[p++] = xOffset + regX;
               xy[p++] = yOffset + regY;
            }
            lastRegX = regX;
            lastRegY = regY;
         }
      }

      return p >> 1;
   }


1  
2  
plotting 1048576 lines in 256x256 grid (array) took:      485ms
plotting 1048576 lines in 256x256 grid (ref.impl) took: 10348ms

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline ClickerMonkey

JGO Coder


Medals: 20


Game Engineer


« Reply #1 - Posted 2013-04-12 04:59:15 »

I have some code to do this, except the start and end points don't need to be on the grid.. and I don't pass a start and end point, I have a start point, direction vector, and maximum number of cells to traverse.

http://pastebin.com/35ZiZQnx

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 605
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #2 - Posted 2013-04-12 06:25:43 »

I have a start point, direction vector, and maximum number of cells to traverse.
Could you tell me how this yields useful results?

If you travel in a straight line on one axis, your algorithm would travel three times as far than when traveling diagonally in 3D space. Even very slight changes in your direction vector will strongly influence its reach.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline ClickerMonkey

JGO Coder


Medals: 20


Game Engineer


« Reply #3 - Posted 2013-04-14 05:21:25 »

You can set the limit to some large number, then easily keep track of distance squared yourself to ensure you don't go outside some sphere. Like you mentioned, the limit doesn't mean length at all... it's not supposed to be used in that sense. You should decide when to stop it based on distance or some other factor (hitting a certain type of cell).

Offline Riven
« League of Dukes »

JGO Overlord


Medals: 605
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #4 - Posted 2013-04-14 10:28:26 »

Then why is that limit configurable at all...?

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline ClickerMonkey

JGO Coder


Medals: 20


Game Engineer


« Reply #5 - Posted 2013-04-15 03:40:51 »

Maybe the person who uses it cares about how many blocks are iterated over, and want to stop at a specific number vs. distance.

It's also faster to just check number of blocks iterated versus calculate distance squared for each block.

In my engine I don't really care about distance, I just want the code to be quick since I could be doing tonnnnns of these every frame... not that that would affect performance =P

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

xsi3rr4x (12 views)
2014-04-15 18:08:23

BurntPizza (11 views)
2014-04-15 03:46:01

UprightPath (24 views)
2014-04-14 17:39:50

UprightPath (10 views)
2014-04-14 17:35:47

Porlus (27 views)
2014-04-14 15:48:38

tom_mai78101 (49 views)
2014-04-10 04:04:31

BurntPizza (108 views)
2014-04-08 23:06:04

tom_mai78101 (207 views)
2014-04-05 13:34:39

trollwarrior1 (176 views)
2014-04-04 12:06:45

CJLetsGame (182 views)
2014-04-01 02:16:10
List of Learning Resources
by Longarmx
2014-04-08 03:14:44

Good Examples
by matheus23
2014-04-05 13:51:37

Good Examples
by Grunnt
2014-04-03 15:48:46

Good Examples
by Grunnt
2014-04-03 15:48:37

Good Examples
by matheus23
2014-04-01 18:40:51

Good Examples
by matheus23
2014-04-01 18:40:34

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:22:30

Anonymous/Local/Inner class gotchas
by Roquen
2014-03-11 15:05:20
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!