Jump to content

Querying a MySQL database for "lines" that have specified end and start points


yanjchan

Recommended Posts

Hi!

I wasn't able to fit the whole question in the title, because it is pretty confusing. (to me - hopefully, not to you guys.  ;))

 

Pretend that there are a bunch of "lines" stored in a database as having starting and ending point coordinates.

 

For example,

 

ID / startx / starty / endx / endy

0 / 1 / 1 / 10 / 10

 

However, the screen only shows an are which extends from 2, 2 to 7, 7.

 

I could run calculations on every single row in the "line" database and determine which of those intersect with the viewport area, but that would take a long time, yes?

 

My question is whether there is an efficient and practical way to accomplish this task.

 

Thanks in advance!

Do the rows in the database have to be fully within the viewing area to be returned, or are they returned if any part of their area should be displayed?

 

For the former (only show objects fully within the area), and assuming your columns are ints:

 

$query = "SELECT * FROM coordinates_table WHERE startx <= $startx AND starty <= $starty AND endx >= $endx AND endy >= $endy";

To do the 2nd option, show every object that at least partially appears in the viewing area, then change the AND's to OR's (you may also want to remove the ='s from all the segments. This prevents a row that appears on the edge of the view or on the corner of the view - in other words, sharing a side but no area - from being displayed).

 

Edit: few corrections to the SQL.

No, I meant objects whose starting and ending coordinates are completely off the display.

 

Unless I misread your SQL, either the starting or ending coordinate has to be on the display.

 

Thanks for the continued help!

Alright, now I'm a bit confused as to what you're asking. You say starting an ending points, as in, making a rectangle correct? And you want anything that is at least partly inside that rectangle to display, correct? And the rows you're looking for are themselves rectangles, correct?

 

If so, you need to change the AND's to OR's and remove the = signs (otherwise, rows that simply share a border, but whose area isn't actually in the display will also get pulled).

 

If not, you need to more clearly explain the problem.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.