Verify if points are collinear in a three-dimensional setting using JavaScript

Can you determine if two lines lie on the same straight line? For instance:

line1Start [0,0,0]
line1End [10, 0, 0]
line2Start [12, 0, 0]
line2End [15, 0, 0]

I am in need of a reliable method to ascertain whether these two lines are on the same level.

I have already tried using threejs methods to check their direction, but it did not provide a conclusive answer.

Answer №1

function checkCollinearity(a, b, p) {
  const riseOverRun = (coor1, coor2) => (coor2[1] - coor1[1]) / (coor2[0] - coor1[0]);
  const slopeXY = riseOverRun(a, b);
  const slopeXZ = riseOverRun(a, p);
  const slopeYZ = riseOverRun(b, p);
  
  return (slopeXY === slopeXZ && slopeXZ === slopeYZ);
}
checkCollinearity([0, 3, 5], [10, 3, 5], [12, 3, 5]); // true
checkCollinearity([0, 3, 1], [10, 3, 1], [12, 3, 7]); // false

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

The efficiency of the Angular app in production is hindered by a file that takes a whopping 5 seconds to load

After successfully compiling my project for production using the command ng build --prod, I made sure to implement certain production settings in angular.json: "production": { "fileReplacements": [ { "replace": "src/environments/e ...

Having trouble accessing the property 'top' of an undefined object in your Ruby on Rails project?

After thoroughly reviewing all the similar threads on SO regarding this error, none of them provided a solution that worked for me. Here is the flow of events: Go to the new Customer page, fill out the required fields, and click save Invoke a method in ...

Tips for accessing data in a child component that is not passed from a parent component (Angularjs versions 1.5 or 1.6)

To showcase the code snippets below: var app = angular.module('app', []) // navigation template app.component('onChanges', { bindings: { name: '<' }, template: '<h1>{{$ctrl.greeting} ...

Angular - Implementing selective binding in a function

In my current Angular project, I am utilizing Datatables and buttons to manipulate data. The buttons available are "View", "Mark as completed", and "Mark as incomplete" which trigger specific functions when clicked. To achieve this functionality, the follo ...

An example of using quotes within quotes is an HTML tag embedded within JavaScript code

Currently, I'm working on a JavaScript code where clicking assigns the function getImage the source of an image to be displayed later on the page. The issue I'm facing revolves around dealing with quotation marks. <img src="bill.jpg" class=" ...

Guide to creating an Image slider using javascript/jQuery with setInterval animation

I'm looking to add a slide effect to the sudden image changes happening. Here's the code I have: HTML <div><img src="1.jpg" id="sliderImage"></div> This is the script I'm using: var myImage = document.getElementById("sl ...

An interactive family tree display feature on our family history website

Currently, I am working on creating a family tree for a project. I am seeking advice on the most effective method to showcase the hierarchical relationships among family members in a visually appealing way. ...

Having trouble with my browser freezing whenever I use PhoneGap in conjunction with jQuery Mobile

While developing my phoneGap app with jQuery Mobile integration, I encountered some issues. Before importing jQuery Mobile, my page was running normally. However, upon importing jQuery Mobile and running index.html, I started getting unexpected popups. Af ...

Display information within the textbox utilizing javascript

I have been struggling to display my filtered data from arrays in the textboxes. I have searched Google extensively for a solution that matches my program but haven't found any. Please help me out. I suspect that the issue lies with the script not fu ...

The 'xxx' type does not have an index signature, so the element is implicitly assigned an 'any' type

I'm currently facing an issue with TypeScript. The error message I'm encountering is related to the following section of code: The Interface: export default interface IUser { username: string; email?: string; isActive: boolean; group: s ...

Loading a page via AJAX without triggering a reload of the entire website

I am experimenting with loading content from a different page using AJAX. The website I am currently testing on is dev.dog-company.com. Here's the code snippet that I have been working on: $('a[rel="load"]').click(function(){ //var sit ...

The proper way to define an event delegator's syntax

Typically, when you want to handle a button click event, you would do so like this: $(document).ready(function() { $("button").click(function() { doSomething(); }); }); However, in the scenario of an event delegator, you may need to respon ...

Error 404 occurred when trying to access the webpack file at my website address through next js

Check out my website at https://i.stack.imgur.com/i5Rjs.png I'm facing an error here and can't seem to figure it out. Interestingly, everything works fine on Vercel but not on Plesk VDS server. Visit emirpreview.vercel.app for comparison. ...

NodeJS - Assertion failure in callback function does not cause Mocha unit test to fail

I am currently facing an issue while running Mocha unit tests on a NodeJS application, specifically with handling assertions within callback functions. As an example, consider the following unit test: describe('PDF manipulation', () => { ...

Generating a tag next to an entry field using material-ui's TextField and getInputProps

In my project, I am utilizing a material-ui TextField to design an input alongside a label for a typeahead picker component using downshift. After exploring the demos, I have implemented the following code snippet: <FormControl fullWidth className={cl ...

Having difficulty retrieving array elements when using setTimeout

Currently, I'm attempting to achieve the following task. I have an array structured as such: var my_array = ['1', '2', '3' ... ,'1000000000000000']; My objective is to generate multiple HTML elements for each ...

AngularJS Skype URI Button Problem

Implementing a Skype button in my project using AngularJS has been challenging. Here is the code I am currently working with: HTML: <script type="text/javascript" src="http://www.skypeassets.com/i/scom/js/skype-uri.js"></script> <skype-ui ...

Even with employing Cors alongside Axios, I continue to encounter the following issue: The requested resource does not have the 'Access-Control-Allow-Origin' header

When working with a MEAN stack app, I had no issues with CORS. However, upon transitioning to the MERN stack, I encountered an error related to CORS despite having it implemented in the backend: Access to XMLHttpRequest at 'http://localhost:5000/api/ ...

"Enhance user experience with AJAX for selecting multiple checkboxes or performing mult

Hey there! So, I've got a question about handling multiple checkboxes with the same name in a form. Here's an example of what I'm working with: <input type="checkbox" name="myname[]" value="1" /> <input type="checkbox" name="myname ...

Guide to handling multiple forms within a single template using Express

If I have an index.html file containing two tables - "Customers" and "Items", along with two forms labeled "Add customer" and "Add item", how can I ensure that submitting these forms results in a new entry being added to the respective table as well as t ...