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 4041928
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:56:30+00:00 2026-05-20T12:56:30+00:00

I’ve been banging my head on the keyboard for about one week now and

  • 0

I’ve been banging my head on the keyboard for about one week now and I can’t figure a proper solution for my problem. I think it’s more Math related than HTML Canvas… hopefully someone can point me into the right direction.

I’m having an HTML Canvas where users can draw lines using they mouse and the very simple moveTo() and lineTo() functions. When the user is done I save the coords in a MongoDB. When the user hits the page later again I want to display his drawing BUT I don’t want to load the entire picture with all stored coordinates at once, I want to return it in tiles (for better performance by caching each tile).

The tiles are 200×200 pixels (fixed offsets and width, starting at 0 -> 200-> 400 ->…).
Now, when the user draws a line from let’s say 50,50(x/y) to 250,250(x/y) there’s only one dot in each bounding box (tile). I need to split the lines and calculate the start and ending points of each line in each bounding box (tile). Otherwise I can’t draw the image partially (in tiles). It get’s even more complicated when a single line crosses multiple bounding boxes (tiles). For instance: 100,100 (x/y) -> -1234,-300 (x/y).
The lines can go from any point (+/-) to ANY direction for ANY distance.

Of course I looked at Bresenham’s good old algorithm and it worked – partially, but it seems like the longest and most resource-hungry solution to me.

So, the reason I’m here is that I hope someone can point me into the right direction with (perhaps) another approach of calculating the start/ending points of my lines for each bounding box.

Code examples are very welcome in JavaScript or PHP.

Thank you for reading and thinking about it 🙂

  • 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-05-20T12:56:31+00:00Added an answer on May 20, 2026 at 12:56 pm

    tl;dr: Use planes, maths explained below. There’s a canvas example at the bottom.

    Given that all of your cells are axis-aligned bounding boxes, you could use the plane equation to find the intersection of your line with the edges.

    Planes

    You can think of your box as a set of four geometric planes. Each plane has a normal, or a vector of length one, indicating which direction is the "front" of the plane. The normals for the planes that make up your cell’s sides would be:

       top = {x:  0, y: -1};
    bottom = {x:  0, y:  1};
      left = {x: -1, y:  0};
     right = {x:  1, y:  0};
    

    Given a point on the plane, the plane has the equation:

    distance = (normal.x * point.x) + (normal.y * point.y)
    

    You can use this equation to calculate the distance of the plane. In this case, you know the top-left corner of your box (let’s say x is 10 and y is 100) is on the top plane, so you can do:

    distance = (0 * 10) + (-1 * 100)
    distance = -100
    

    Checking a point against a plane

    Once you have the distance, you can reuse the equation to check where any point is, relative to the plane. For a random point p (where x is -50 and y is 90), you can do:

    result = (normal.x * p.x) + (normal.y * p.y) - distance
    result = (0 * -50) + (-1 * 90) - (-100)
    result = 0 + (-90) - (-100)
    result = -90 + 100
    result = 10
    

    There are two possible results:

    if (result >= 0) {
        // point is in front of the plane, or coplanar.
        // zero means it is coplanar, but we don't need to distinguish.
    } else {
        // point is behind the plane
    }
    

    Checking a line against a plane

    You can check both endpoints of a line from a to b in this way:

    result1 = (normal.x * a.x) + (normal.y * a.y) - distance
    result2 = (normal.x * b.x) + (normal.y * b.y) - distance
    

    There are four possible results:

    if (result1 >= 0 && result2 >= 0) {
      // the line is completely in front of the plane
    } else if (result1 < 0 && result2 < 0) {
      // the line is completely behind the plane
    } else if (result1 >= 0 && result2 < 0) {
      // a is in front, but b is behind, line is entering the plane
    } else if (result1 < 0 && result2 >= 0) {
      // a is behind, but b is in front, line is exiting the plane
    }
    

    When the line intersects the plane, you want to find the point of intersection. It helps to think of a line in vector terms:

    a + t * (b - a)
    

    If t == 0, you are at the start of the line, and t == 1 is the end of the line. In this context, you can calculate the point of intersection as:

    time = result1 / (result1 - result2)
    

    And the point of intersection as:

    hit.x = a.x + (b.x - a.x) * time
    hit.y = a.y + (b.y - a.y) * time
    

    Checking a line against the box

    With that math, you can figure out the lines of intersection with your box. You just need to test the endpoints of your line against each plane, and find the minimum and maximum values of time.

    Because your box is a convex polygon, there is an early out in this check: if the line is completely in front of any one plane in your box, it cannot intersect with your box. You can skip checking the rest of the planes.

    In JavaScript, your result might look something like this:

    /**
     * Find the points where a line intersects a box.
     *
     * @param a Start point for the line.
     * @param b End point for the line.
     * @param tl Top left of the box.
     * @param br Bottom right of the box.
     * @return Object {nearTime, farTime, nearHit, farHit}, or false.
     */
    function intersectLineBox(a, b, tl, br) {
      var nearestTime = -Infinity;
      var furthestTime = Infinity;
      var planes = [
        {nx:  0, ny: -1, dist: -tl.y},  // top
        {nx:  0, ny:  1, dist:  br.y},  // bottom
        {nx: -1, ny:  0, dist: -tl.x},  // left
        {nx:  1, ny:  0, dist:  br.x}   // right
      ];
      for (var i = 0; i < 4; ++i) {
        var plane = planes[i];
        var nearDist = (plane.nx * a.x + plane.ny * a.y) - plane.dist;
        var farDist = (plane.nx * b.x + plane.ny * b.y) - plane.dist;
        if (nearDist >= 0 && farDist >= 0) {
          // both are in front of the plane, line doesn't hit box
          return false;
        } else if (nearDist < 0 && farDist < 0) {
          // both are behind the plane
          continue;
        } else {
          var time = nearDist / (nearDist - farDist);
          if (nearDist >= farDist) {
            // entering the plane
            if (time > nearestTime) {
              nearestTime = time;
            }
          } else {
            // exiting the plane
            if (time < furthestTime) {
              furthestTime = time;
            }
          }
        }
      }
      if (furthestTime < nearestTime) {
        return false;
      }
      return {
        nearTime: nearestTime,
        farTime: furthestTime,
        nearHit: {
          x: a.x + (b.x - a.x) * nearestTime,
          y: a.y + (b.y - a.y) * nearestTime
        },
        farHit: {
          x: a.x + (b.x - a.x) * furthestTime,
          y: a.y + (b.y - a.y) * furthestTime
        }
      };
    }
    

    If this is still too slow, you can also do broadphase culling by dividing the world up into big rects, and assigning lines to those rects. If your line and cell aren’t in the same rect, they don’t collide.

    I’ve uploaded a canvas example of this.

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

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I am reading a book about Javascript and jQuery and using one of the
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I don't have much knowledge about the IPv6 protocol, so sorry if the question
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am currently running into a problem where an element is coming back from

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.