Register to get access to free programming courses with interactive exercises

Points on the coordinate plane JS: Building abstractions with data

One of the most fitting topics to practice modeling skills is geometry. Unlike other areas of mathematics, it's visually conceivable and directly accessible for everyone. First, let's recall the basic concepts that we'll be dealing with.

A Coordinate (Cartesian) Plane is the plane on which a coordinate system is defined. The coordinates are given on two right-angle intersecting lines (numerical axes) x and y.

                     6 | y
                     5 |
                     4 |
                     3 |     . (2, 3)
                     2 |
                     1 |
                       |
----------------------------------------------------
                       |  1  2  3  4  5  6         x
                       |
                       |
                       |
                       |
                       |
                       |

The simplest primitive that can be placed on the plane is a point. Its position is defined by two coordinates, and in mathematics it's written as (2, 3), where the first number is the x-axis, coordinate, and the second is the y-axis coordinate. In code, it can be represented as an array of two elements.

const point = [2, 3];

This is already enough to perform some useful geometric actions. For example, to find a symmetric point relative to the x-axis, it's sufficient to invert the second number (i.e., turn a + to a - and vice versa).

                     6 | y
                     5 |
                     4 |
                     3 |     . (2, 3)
                     2 |
                     1 |
                       |
----------------------------------------------------
                       |  1  2  3  4  5  6         x
                    -1 |
                    -2 |
                    -3 |     . (2, -3)
                    -4 |
                    -5 |
                    -6 |
const point = [2, 3];

const [x, y] = point;
// x = 2, y = 3

const symmetricalPoint = [x, -y];
// x = 2, y = -3

Occasionally, you need to find a point that's directly in between two points (they may also say that you need to find the middle of a segment). This point is calculated by finding the arithmetic mean of each of the coordinates. I.e., the x coordinate of the midpoint is (x1 + x2) / 2, and the y coordinate is y(y1 + y2) / 2.

const getMiddlePoint = (p1, p2) => {
  const x = (p1[0] + p2[0]) / 2;
  const y = (p1[1] + p2[1]) / 2;

  return [x, y];
};

const point1 = [2, 3];
const point2 = [-4, 0];

console.log(getMiddlePoint(point1, point2)); // => [-1, 1.5]

There are many such operations in geometry. It makes sense to put all functions related to working with points in the points module from the code architecture viewpoint.

In turn, the points are combined into segments. Each segment is defined by a pair of points — the beginning and the end. We can define a segment with code just like a point - an array of two elements where each element is a point.

const point1 = [3, 4];
const point2 = [-8, 10];
const segment = [point1, point2];

Hexlet Experts

Are there any more questions? Ask them in the Discussion section.

The Hexlet support team or other students will answer you.

About Hexlet learning process

For full access to the course you need a professional subscription.

A professional subscription will give you full access to all Hexlet courses, projects and lifetime access to the theory of lessons learned. You can cancel your subscription at any time.

Get access
130
courses
1000
exercises
2000+
hours of theory
3200
tests

Sign up

Programming courses for beginners and experienced developers. Start training for free

  • 130 courses, 2000+ hours of theory
  • 1000 practical tasks in a browser
  • 360 000 students
By sending this form, you agree to our Personal Policy and Service Conditions

Our graduates work in companies:

<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.bookmate">Bookmate</span>
<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.healthsamurai">Healthsamurai</span>
<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.dualboot">Dualboot</span>
<span class="translation_missing" title="translation missing: en.web.courses.lessons.registration.abbyy">Abbyy</span>
Suggested learning programs

From a novice to a developer. Get a job or your money back!

Frontend Developer icon
Profession
beginner
Development of front-end components for web applications
start anytime 10 months

Use Hexlet to the fullest extent!

  • Ask questions about the lesson
  • Test your knowledge in quizzes
  • Practice in your browser
  • Track your progress

Sign up or sign in

By sending this form, you agree to our Personal Policy and Service Conditions

Toto Image

Ask questions if you want to discuss a theory or an exercise. Hexlet Support Team and experienced community members can help find answers and solve a problem.