Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (808)
Games in Android Showcase (239)
games submitted by our members
Games in WIP (872)
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  
  Calculating Line Intersections without looping  (Read 3737 times)
0 Members and 1 Guest are viewing this topic.
Offline monopolyman900

Senior Newbie


Medals: 1
Exp: 4 years



« Posted 2016-03-09 01:39:57 »

I've got a game I'm working on that's tile based. I have a bunch of tiles on the map and a line and I'm trying to figure out how to determine what tiles the line is passing through without looping through all tiles or all of the points on the line. I'm sure there's a way to do with just using math - but I haven't figured it out.

I know the starting point of the line and the ending point of the line so I also know the slope, and I know the size of each tile. The tiles are stored in a 2D array that corresponds with their x/y position in the map.

Does anybody have any suggestions on how I might figure this out using math?
Offline chrislo27
« Reply #1 - Posted 2016-03-09 02:01:16 »

Do you mean something like Bresenham's line algorithm which is commonly used in graphics to draw lines? LibGDX has a Bresenham2 implementation here and I think the concept should stay the same even if you're not using the engine.
Offline monopolyman900

Senior Newbie


Medals: 1
Exp: 4 years



« Reply #2 - Posted 2016-03-09 02:20:46 »

Thank you, this is exactly what I meant.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Archive
« Reply #3 - Posted 2016-03-09 02:23:02 »

Thank you, this is exactly what I meant.
You were looking for a line drawing algorithm?

Offline monopolyman900

Senior Newbie


Medals: 1
Exp: 4 years



« Reply #4 - Posted 2016-03-09 02:54:08 »

I guess I was, I was trying to figure out what tiles the line was passing through and prevent it from passing through certain ones. I didn't know how to label what I was looking for though.
Offline Riven
Administrator

« JGO Overlord »


Medals: 1371
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #5 - Posted 2016-03-09 09:00:40 »

Bresenham's does not give you ALL the tiles the line intersects. It's actually quite a challenge. I solved it with a neat and fast algorithm some years ago, posted it on JGO, you might be able to find it - I'm in my phone, so you have to do the digging yourself Smiley

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

JGO Kernel


Medals: 196



« Reply #6 - Posted 2016-03-09 10:14:45 »

http://www.java-gaming.org/topics/line-grid-intersection-code-all-plotted-cells-directly-connected/28988/view.html

Mathias - I Know What [you] Did Last Summer!
Pages: [1]
  ignore  |  Print  
 
 

 
Riven (846 views)
2019-09-04 15:33:17

hadezbladez (5789 views)
2018-11-16 13:46:03

hadezbladez (2602 views)
2018-11-16 13:41:33

hadezbladez (6205 views)
2018-11-16 13:35:35

hadezbladez (1498 views)
2018-11-16 13:32:03

EgonOlsen (4733 views)
2018-06-10 19:43:48

EgonOlsen (5791 views)
2018-06-10 19:43:44

EgonOlsen (3275 views)
2018-06-10 19:43:20

DesertCoockie (4174 views)
2018-05-13 18:23:11

nelsongames (5500 views)
2018-04-24 18:15:36
A NON-ideal modular configuration for Eclipse with JavaFX
by philfrei
2019-12-19 19:35:12

Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08
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!