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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T10:43:09+00:00 2026-06-16T10:43:09+00:00

I have dynamically generated lines that animate and I want to detect when a

  • 0

I have dynamically generated lines that animate and I want to detect when a lines hits another. I’m trying to implement some basic linear algebra to obtain the equation of the lines and then solving for x,y, but the results are erratic. At this point I’m testing only with two lines which means I should be getting one point of intersection, but I get two. I just want to make sure my math is ok and that I should be looking elsewhere for the problem.

function collision(boid1, boid2) {
  var x1 = boid1.initialX, y1 = boid1.initialY, x2 = boid1.x, y2 = boid1.y, x3 = boid2.initialX, y3 = boid2.initialY, x4 = boid2.x, y4 = boid2.y;
  slope1 = (y1 - y2)/(x1 - x2);
  slope2 = (y3 - y4)/(x3- x4);

  if(slope1 != slope2){
    var b1 = getB(slope1,x1,y1);
    var b2 = getB(slope2,x3,y3);

    if(slope2 >= 0){
      u = slope1 - slope2;
    }else{
      u = slope1 + slope2;
    }

    if(b1 >= 0){
      z = b2 - b1;
    }else{
      z = b2 + b1;
    }
    pointX = z / u;
    pointY = (slope1*pointX)+b1;
    pointYOther = (slope2*pointX)+b2;
    console.log("pointx:"+pointX+" pointy:"+pointY+" othery:"+pointYOther);
    context.beginPath();
    context.arc(pointX, pointY, 2, 0, 2 * Math.PI, false);
    context.fillStyle = 'green';
    context.fill();
    context.lineWidth = 1;
    context.strokeStyle = '#003300';
    context.stroke();
  }
  return false;
}

function getB(slope,x,y){
  var y = y, x = x, m = slope;
  a = m*x;
  if(a>=0){
    b = y - a;
  }else{
    b = y + a;
  }
  return b;
}

The problem is that I’m getting two different values for the point of intersection. There should only be one, which leads me to believe my calculations are wrong. Yes, x2,y2,x4,y4 are all moving, but they have a set angle and the consistent slopes confirm that.

  • 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-16T10:43:10+00:00Added an answer on June 16, 2026 at 10:43 am

    You don’t need to alternate between adding/subtracting y-intersects when plugging ‘found-x’ back into one of the equations:

    (function () {
        window.linear = {
            slope: function (x1, y1, x2, y2) {
                if (x1 == x2) return false;
                return (y1 - y2) / (x1 - x2);
            },
            yInt: function (x1, y1, x2, y2) {
                if (x1 === x2) return y1 === 0 ? 0 : false;
                if (y1 === y2) return y1;
                return y1 - this.slope(x1, y1, x2, y2) * x1 ;
            },
            getXInt: function (x1, y1, x2, y2) {
                var slope;
                if (y1 === y2) return x1 == 0 ? 0 : false;
                if (x1 === x2) return x1;
                return (-1 * ((slope = this.slope(x1, y1, x2, y2)) * x1 - y1)) / slope;
            },
            getIntersection: function (x11, y11, x12, y12, x21, y21, x22, y22) {
                var slope1, slope2, yint1, yint2, intx, inty;
                if (x11 == x21 && y11 == y21) return [x11, y11];
                if (x12 == x22 && y12 == y22) return [x12, y22];
    
                slope1 = this.slope(x11, y11, x12, y12);
                slope2 = this.slope(x21, y21, x22, y22);
                if (slope1 === slope2) return false;
    
                yint1 = this.yInt(x11, y11, x12, y12);
                yint2 = this.yInt(x21, y21, x22, y22);
                if (yint1 === yint2) return yint1 === false ? false : [0, yint1];
    
                if (slope1 === false) return [y21, slope2 * y21 + yint2];
                if (slope2 === false) return [y11, slope1 * y11 + yint1];
                intx = (slope1 * x11 + yint1 - yint2)/ slope2;
                return [intx, slope1 * intx + yint1];
            }
        }
    }());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a trivia page that is dynamically generated by some javascript code. It
I have a select box that gets generated dynamically. I want to allow users
I have dynamically generated strings like @#@!efq@!#! , and I want to remove specific
I have a dynamically generated rss feed that is about 150M in size (don't
Fiddle: http://jsfiddle.net/MhWm5/16/ I have some dynamically generated table rows/values with dynamic IDs <td class=control-group>
I have a series of dynamically generated inputs that I need to have ajax
I have content that is being dynamically generated. How to I listen to a
I have a page that is generated dynamically via the URL (GET Method). This
I have a ul list that is dynamically generated upon page load via JavaScript.
I have a web site that gets links generated dynamically. I would like to

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.