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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.