Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8969385
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:35:01+00:00 2026-06-15T17:35:01+00:00

I have a 2-dimmensional unit grid, and a bunch of line segments that start

  • 0

I have a 2-dimmensional unit grid, and a bunch of line segments that start and end at any rational number. I need an efficient way to calculate which grid cells the line passes through. For example, the line:

From (2.1, 3.9) to (3.8, 4.8) passes through the grid cells with lower left points (2, 3), (2, 4), and (3, 4).

Is there a quick, efficient way to calculate these quadrants from the line’s endpoints?

I’ll be working in R, but an answer in Python or pseudocode would work too. Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T17:35:02+00:00Added an answer on June 15, 2026 at 5:35 pm

    Folks who work with spatial data deal with this kind of question all the time, so it may be worth piggy-backing on their efforts. Here’s a solution that uses R’s raster package (and functions from the sp package on which it depends):

    library(raster)
    
    ## Create a SpatialLines object
    a <- c(2.1, 3.9) 
    b <- c(3.8, 4.8)
    ## Method #1 -- Uses functions from the sp package.
    SL <- SpatialLines(list(Lines(list(Line(rbind(a,b))), "ab")))
    ## Method #2 -- Uses readWKT() from the rgeos package. Easier to read.
    # library(rgeos)
    # string <- paste0("LINESTRING(", paste(a, b, collapse=", "), ")")
    # SL <- readWKT(string)
    
    ## Create a raster object
    m <- 10
    n <- 10
    mat <- matrix(seq_len(m*n), nrow = m, ncol = n)
    r <- raster(mat, xmn = 0, xmx = n, ymn = 0, ymx = m) 
    
    ## Find which cells are intersected & get coordinates of their lower-left corners
    ii <- extract(r, SL, cellnumbers=TRUE)[[1]][, "cell"]
    floor(xyFromCell(r, ii))
    #      x y
    # [1,] 2 4
    # [2,] 3 4
    # [3,] 2 3
    
    ## Confirm that this is correct with a plot
    image(r)
    plot(as(rasterize(SL, r), "SpatialPolygons"), 
         border = "darkgrey", lwd = 2, add = TRUE)
    lines(SL)
    

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple one dimmensional array of integer values that represent a physical
I have a two dimensional array that I need to load data into. I
I have an multi-dimensional array that I want to send to a PHP script
I have a 2 dimensional array which is like that; $results[$i][$j]->title; $results[$i][$j]->snippet; $results[$i][$j]->link; It
I have 2 dimensional list created at runtime (the number of entries in either
I have a multi-dimensional array right now that looks like this: function art_appreciation_feeds() {
I have a two dimensional grid of cells. In this simulation, cells may request
I have a multi-dimensional array that I'd like to use for building an xml
I have a 2-dimensional array of objects (predominantly, but not exclusively strings) that I
I have a program in which I need to apply a 2-dimensional texture (simple

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.