GameDev - Intersection of Two Lines (intersection of two lines)

Video GameDev - Intersection of Two Lines (intersection of two lines)

From two straight lines (or vector) in a 2D plane, we can find their intersections to calculate the angle of reflection and direction of travel.

View Demo.

Create equations straight line from the segment

I have two points A (x1, y1) and B (x2, y2) form a straight line, to create a linear equation from these two points, we need to calculate the inclination of the line (slope) by the formula:

a = (y2 - y1) / (x2 - x1)

Replace x2, y2 with two variables x, y:

a = (y - y1) / (x - x1)
=> y - y1 = a (x - x1)

Or:
y = ax + (y1 - AX1)
Set b = y1 - AX1, we have:

y = ax + b

Calculate the intersection of two lines

We have two equations straight line

(1): y = a1x + b1
(2): y = a2x + b2

We can calculate the intersection of them by finding the intersection of x before:

a1x + b1 + b2 = a2x;

=> X (a1 - a2) = b2 - b1
=> x = (b2 - b1) / (a1 - a2)

Once you have x, we would like to (1) or (2) to calculate y.
To check two intersecting straight lines can not we just check point {x, y} of both line segment or not .
Also we need to rule out cases of two parallel lines or overlap, then their value will equal slope.

Illustrations with HTML5 Canvas

GameDev - Intersection of Two Lines (intersection of two lines)
<Html>
<Head>
  <Script>
 / ***************** *************** Circle /
 
function Circle (x, y) {
    this .x = x;
    this .y = y;
    this .radius = 5;
}
 
Circle.prototype.draw = function (context) {
    context.beginPath ();
    context.fillStyle = "black" ;
    context.arc ( this .x, this .y, this .radius, 0, Math.PI * 2, true );
    context.closePath ();
    context.fill ();
}
Circle.prototype.setPosition = function (x, y) {
    this .x = x + this .radius;
    this .y = y + this .radius;
}
Circle.prototype.contains = function (x, y) {
    var dx = this .x - x;
    var dy = this .y - y;
 
    return Math.sqrt (dx * dx + dy * dy) <= this .radius;
}
 
/ ***************** *************** Line /
 
function Line (x1, y1, x2, y2, color, hasHandles) {
    this .color = color;
    this .p1 = new Circle (x1, y1,5);
    this .p2 = new Circle (x2, y2,5);
    this .hasHandles = hasHandles;
}
Line.prototype.captureMouse = function (x, y) {
    if ( this .p1.contains (x, y))
        return this .p1;
    if ( this .p2.contains (x, y))
        return this .p2;
}
Line.prototype.getVector = function () {
    var x = this .p2.x- this .p1.x;
    var y = this .p2.y- this .p1.y;
 
    return {
        x: x,
        y: y,
        root: this .p1,
        length: Math.sqrt (x * x + y * y)
    };
}
Line.prototype.draw = function (context) {
 
    context.beginPath ();
    context.strokeStyle = this .color;
    context.lineWidth = this .width;
 
    context.moveTo ( this .p1.x, this .p1.y);
    context.lineTo ( this .p2.x, this .p2.y);
    context.closePath ();
    context.stroke ();
 
    if ( this .hasHandles) {
        this .p1.draw (context);
        this .p2.draw (context);
    }
}
 / Main ************** ************* /
 
var _canvas;
var _context;
var _movingShape = false ;
var _line1;
var _line2;
 
function canvas_mousedown (e) {
 
    var x = e.pageX - _canvas.offsetLeft;
    var y = e.pageY - _canvas.offsetTop;
 
    _movingShape = _line1.captureMouse (x, y);
    if (! _movingShape)
        _movingShape = _line2.captureMouse (x, y);
}
 
function canvas_mousemove (e) {
    var x = e.pageX - _canvas.offsetLeft;
    var y = e.pageY - _canvas.offsetTop;
 
    if (_movingShape) {
        _movingShape.setPosition (x, y);
        update ();
    }
}
 
function canvas_mouseup (e) {
    _movingShape = null ;
}
 
function clear () {
    _context.clearRect (0, 0, _canvas.width, _canvas.height);
}
function update () {
 
    var a1 = (_line1.p2.y-_line1.p1.y) / (_ line1.p2.x-_line1.p1.x);
    var b1 = _line1.p2.y - a1 * _line1.p2.x;
 
    var a2 = (_line2.p2.y-_line2.p1.y) / (_ line2.p2.x-_line2.p1.x);
    var b2 = _line2.p2.y - a2 * _line2.p2.x;
 
    // Find intersection point
    // Y = a1x + b1
    // Y = a2x + b2
    // A1x + b1 + b2 = a2x
    // (A1 - a2) x = b2 - b1
    var x = (b2 - b1) / (a1 - a2);
    var y = a1 * x + b1;
 
    // Draw
 
    clear ();
 
    _line1.draw (_context);
    _line2.draw (_context);
 
    _context.fillText (genFormula (a1, b1), 10,20);
    _context.fillText (genFormula (a2, b2), 10.40);
 
    _context.beginPath ();
    _context.fillStyle = "black" ;
    _context.arc (x, y, 10, 0, Math.PI * 2, true );
    _context.closePath ();
    _context.fill ();
}
function genFormula (a, b)
{
    a = floor (a);
    b = floor (b);
    var s = "y =" ;
    if (a! = 0)
        s + = (a == 1? '' : a) + "x" ;
    if (b! = 0)
        s + = (b> 0? (a == 0? "" : "+" ): "" ) + b;
    return s;
}
function floor (number)
{
    return Math.floor (number * 10) / 10;
}
 
function distance (x, y, xx, yy) {
    var dx = xx-x;
    var dy = yy-y;
    return Math.sqrt (dx * dx + dy * dy);
}
 
window.onload = function () {
    _canvas = document.getElementById ( "canvas" );
    _context = _canvas.getContext ( "2d" );
    _context.font = "16px Arial" ;
    _context.lineWidth = 3;
 
    _line1 = new Line (100, 100, 150, 150, "red" , true );
    _line2 = new Line (100, 200, 300, 200, "green" , true );
    _canvas.onmousedown = canvas_mousedown;
    _canvas.onmousemove = canvas_mousemove;
    _canvas.onmouseup = canvas_mouseup;
 
    update ();
 
}
  </ Script>
</ Head>
<Body>
<Canvas id = "canvas" width = "400px" height = "400px" style = "border: 1px solid" > </ canvas>
</ Body>
</ Html>

YinYangIt's Blog

The hidden indicators of players in FIFA Online 4

The hidden indicators of players in FIFA Online 4

The hidden traits of players in FIFA Online are quite important factors, not only contributing to the perfection of the players but also the factors that help create a perfect squad.

MFC - Lesson1: Hello World!

MFC - Lesson1: Hello World!

This is the first article in the series about programming guide VC ++ 6 with MFC for beginners. Through simple examples, each article will give you the basics so you can get to work on application development using VC ++.

Mission and target system in Temple Run 2

Mission and target system in Temple Run 2

To master this game, you need to understand the system of tasks and goals that you need to complete in Temple Run 2.

Summary of weapons in Garena Free Fire

Summary of weapons in Garena Free Fire

Weapons are always something that gamers care about when playing shooting games or survival games. The same goes for Garena Free Fire.

Html5-Canvas: Zoom in / zoom out the game with the mouse wheel

Html5-Canvas: Zoom in / zoom out the game with the mouse wheel

This article shall comply with your request xtranfor55  in article Scroll background image and map (Map Scrolling) . I'll describe how to do this by 2 ways: using the Canvas API and processed manually in code.

How to avoid detection when using the PUBG Mobile emulator on PC

How to avoid detection when using the PUBG Mobile emulator on PC

Recently, PUBG Mobile announced to gamers that PUBG Mobile players using Android emulator on computer will only be able to play with each other without those who play with phone. After the PUBG Mobile gaming community complained those people

Laplace M: Summary of 6x gold weapons and specific stats

Laplace M: Summary of 6x gold weapons and specific stats

Images, information and indexes on the level 60 golden weapons of each class in Laplace M.

Instructions for loading Yong Heroes game card

Instructions for loading Yong Heroes game card

If you are a beginner to playing Yong Heroes and do not know how to deposit money into this game, the following article will guide us to load Yong Heroes game card, with just a few simple steps.

How to download and install FIFA Online 3 game

How to download and install FIFA Online 3 game

FIFA Online 3 must be a name that is no stranger to gamers, especially football lovers. FO3 allows you to participate in intense online matches with friends. To play FO3, you need to download and install additional software Garena P

How to earn recovery items in BTS World

How to earn recovery items in BTS World

Help the members of BTS recover after completing a series of missions from the agency by earning valuable restoration items in the game BTS World.

Information about all Quests in EverWing

Information about all Quests in EverWing

In EverWing, you have many ways to earn gold, items, trophies and experience. However, there is another way for you to plow items, money and experience. It is to perform a task (Quest) in EverWing, so your accurate information

How to create a WTFast account on your computer

How to create a WTFast account on your computer

To use WTFast to speed up gaming, gamers need to ensure two basic and mandatory conditions, that is, to install WTFast on the computer and have a WTFast account activated.

PUBG Mobile: Time slots and locations for Flare Gun appear on Sanhok

PUBG Mobile: Time slots and locations for Flare Gun appear on Sanhok

If you want to earn Flare Gun when playing PUBG Mobile, especially on Sanhok, you need to pay attention to the following.

PUBG Mobile: Some useful tips when playing on Sanhok map

PUBG Mobile: Some useful tips when playing on Sanhok map

Here are useful tips and information that will help you spend TOP 1 easily when participating in Sanhok map

PUBG: The location of the roller coaster map Erangel and Miramar

PUBG: The location of the roller coaster map Erangel and Miramar

Summary of the locations of gliders in PUBG in Erangel and Miramar maps for gamers who love to fly.

How to reduce lag effectively when playing PUBG Mobile

How to reduce lag effectively when playing PUBG Mobile

With the phone configuration weak when playing games often occurs jerky, lag. In particular, PUBG Mobile developed on relatively high graphics platform, requiring the device to be strong enough for a smooth new experience.

PUBG Mobile: 5 tips to track enemies from a distance for newbies

PUBG Mobile: 5 tips to track enemies from a distance for newbies

Tracking enemies from afar probably is not easy for new players joining PUBG Mobile. We must have keen eyes and constantly grasp the position of the opponents who are near us to take control of the situation.

PUBG Mobile: The most mapable and battleable locations in Sanhok

PUBG Mobile: The most mapable and battleable locations in Sanhok

These are the locations with lots of items and many players parachuting into the Sanhok map of PUBG Mobile

Simple and easy ways to create mind maps

Simple and easy ways to create mind maps

Mind map is one of the ways to help you create, remember and plan for all effective ideas and projects. The following article will guide you on how to create mind maps easily.

PUBG: Synthesize the hottest skydiving locations in Sanhok map

PUBG: Synthesize the hottest skydiving locations in Sanhok map

The area of ​​Sanhok is 4x4km, only 1/4 compared to Erangel and Miramar, with such a small area, but PUBG still does not reduce the number of players in the game, so the game will be very fast and will happen. painting continuously, so the weapon will appear