JavaScript fun: point, line, and surface

Source: Internet
Author: User
The description is very simple. Given the coordinates of some vertices, return true if they are in the same line. The topic description is very simple. Given the coordinates of some vertices, return TrueIf they are in the same line.

For example[7, 4], Which indicates a point.


onLine([[1,2], [7, 4], [22, 9]]);                 // returns trueonLine([[1,2], [-3, -14], [22, 9]]);              // returns false

Seeing this question, I first came up with a plane in my mind, followed by a Cartesian coordinate system.

In the coordinate system, I used the methods taught by the mathematics teacher in the middle school to draw these points one by one in the coordinate system.

Then, link the vertex with a line segment.

So how can we determine that they are on the same straight line?

I quickly thought of the slope.

If any two points on a straight line are(X1, y1),(X2, y2)Then the straight line slopeK = (y2-y1)/(x2-x1).

If the slope of these line segments is the same, they must be in the same line!

In the program, I only need to judge the slope between line segments and return different results.False.

Write the Code as follows:


Function onLine (points) {var slope; for (var I = 0; I
 
  


The above are JavaScript interesting questions: point, line, and surface content. For more information, see PHP Chinese website (www.php1.cn )!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.